代码之家  ›  专栏  ›  技术社区  ›  Mel

如何使用R将大型igraph子集为一个节点及其连接的节点?

  •  5
  • Mel  · 技术社区  · 7 年前

    我在R中有一个非常大的igraph(称为g)。

    我只对一个节点(NodeA)以及任何直接连接到NodeA的节点感兴趣。我想以一个非常简单的图表结束,如下图所示。

    我尝试过子图(g,“nodeA”),但我最终只得到了nodeA本身。

    我想我需要将图g的边子集为那些连接到nodeA的边,然后使用子图。边()。我不知道如何根据边连接到的节点来划分边的子集。。。

    NodeA and two directly connected nodes

    2 回复  |  直到 7 年前
        1
  •  1
  •   Marcus Campbell Artem Sokolov    7 年前

    尝试使用 neighbors()

    # Example graph
    g1 <- graph_from_literal(1:2:3---3:4:5)
    
    # Let's take a look at it
    plot(g1)
    

    enter image description here

    # Say we are interested in the subgraph consisting of vertex 1 and all
    # other vertices it is connected to
    
    g2 <- induced_subgraph(g1, c(1, neighbors(g1,1)))
    plot(g2)
    

    enter image description here

        2
  •  0
  •   thelatemail    7 年前

    ?make_ego_graph 应该这样做,如果 order=1 :

    g <- make_graph(c(1, 2, 2, 3, 3, 4, 5, 6), directed = FALSE)
    V(g)$name <- letters[1:6]
    g
    #IGRAPH  UN-- 6 4 -- 
    #+ attr: name (v/c)
    #+ edges from 657f0a0 (vertex names):
    #[1] a--b b--c c--d e--f
    
    make_ego_graph(g, order=1, nodes=2)
    #[[1]]
    #IGRAPH UN-- 3 2 -- 
    #+ attr: name (v/c)
    #+ edges from 69681da (vertex names):
    #[1] a--b b--c