从具有关联边的所有顶点得到一个图R IGRAPHE
我想从一个图中得到一个子图,它由所有顶点组成,这些顶点的关联边从一些顶点开始,然后沿着这些边,直到没有更多的关联边为止。使用下面的代码,我只得到第一个邻居从具有关联边的所有顶点得到一个图R IGRAPHE,r,igraph,R,Igraph,我想从一个图中得到一个子图,它由所有顶点组成,这些顶点的关联边从一些顶点开始,然后沿着这些边,直到没有更多的关联边为止。使用下面的代码,我只得到第一个邻居 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") make\u ego\u graph
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")
make\u ego\u graph
可用于查找特定节点附近的子图。
您可以通过在中设置order
参数来搜索整个图形
函数生成图形
范例
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)
你确定你的预期结果;假设节点3没有向内的边,您不希望图3->6->7与其他节点断开连接吗?要查看使用sg=make_ego_图(g,nodes=V(g)[c(“7”,“9”)],order=length(V(g)),mode=“in”)
;你可以使用do.call(union,sg)
加入他们,它起作用了,我唯一要添加的就是do.call(igraph::union,sg)
你可以添加它作为答案
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)