【问题标题】:Get a graph from all vertex with incident edges R igraph从所有具有事件边的顶点获取图 R igraph
【发布时间】:2020-05-05 20:10:12
【问题描述】:

我想从一个图中获得一个子图,该子图由所有具有从一些顶点开始的入射边的顶点组成,并沿着这些边直到没有更多的入射边。使用以下代码,我只得到第一个邻居

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)

【问题讨论】:

  • 你确定你的预期结果吗?鉴于节点 3 没有内边,您不会期望图 3->6->7 未连接到其他节点吗?
  • 查看使用sg = make_ego_graph(g, nodes = V(g)[c("7","9")], order=length(V(g)), mode="in");您可以使用do.call(union, sg) 加入他们
  • 成功了,我唯一要添加的是do.call(igraph::union, sg)你可以添加它作为答案

标签: r igraph


【解决方案1】:

make_ego_graph 可用于查找特定节点附近的子图。 您可以通过设置 order 参数来搜索整个图形 函数make_ego_graph

例子

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-28
    • 1970-01-01
    • 2020-04-06
    • 2017-03-02
    • 2019-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多