我想从一个图中获得一个子图,它由所有具有从一些顶点开始的入射边的顶点组成,并沿着这些边直到没有更多的入射边。使用以下代码,我只得到第一个邻居
g <- graph_from_literal( 1 -+ 4 -+ 5 -+ 8,2 -+ 5 , 3-+6-+7, 4+-3, 4-+8, 5 -+9, simplify = FALSE)
adjacent_vertices(g, V(g)[c("7","9")], mode="in")
我知道我应该进行某种循环,但adjacent_vertices
返回了一个列表,我不知道如何进行。
对于这个例子,结果应该是
graph_from_literal( 1 -+ 4 -+ 5 ,2 -+ 5 , 3-+6-+7, 4+-3, 5 -+9, simplify = FALSE)
make_ego_graph
可用于查找特定节点邻域中的子图。您可以通过在中设置order
参数来搜索整个图形函数CCD_ 4。
示例
library(igraph)
# Your graph
g = graph_from_literal( 1 -+ 4 -+ 5 -+ 8, 2 -+ 5 , 3-+6-+7, 4+-3, 4-+8, 5 -+9,
simplify = FALSE)
# Set the order equal to the number of nodes in the graph
sg = make_ego_graph(g, nodes = V(g)[c("7","9")], order=length(V(g)), mode="in")
# This returns two subgraphs as node 3 has no inward edges and so the graph 3->6->7
# is unconnected to the other nodes. You can join the subgraphs by using
do.call(union, sg)