代码之家  ›  专栏  ›  技术社区  ›  tommy chheng

如何使用igraph和R找到顶点的边?

  •  23
  • tommy chheng  · 技术社区  · 14 年前

    假设我有这个示例图,我想找到连接到顶点“a”的边

     d <- data.frame(p1=c('a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'd'),
                     p2=c('b', 'c', 'd', 'c', 'd', 'e', 'd', 'e', 'e'))
    
    library(igraph)
    g <- graph.data.frame(d, directed=FALSE)
    print(g, e=TRUE, v=TRUE)
    

     V(g)[V(g)$name == 'a' ]
    

    但是我需要引用所有连接到顶点“a”的边。

    4 回复  |  直到 14 年前
        1
  •  32
  •   neilfws    14 年前

    igraph iterators ; 尤其是from()和to()函数。

    在您的示例中,“a”是V(g)[0],因此要查找连接到“a”的所有边:

    E(g) [ from(0) ]
    

    [0] b -- a
    [1] c -- a
    [2] d -- a
    
        2
  •  5
  •   cannin    9 年前

    如果不知道顶点的索引,可以在使用from()函数之前使用match()找到它。

    idx <- match("a", V(g)$name)
    E(g) [ from(idx) ]
    
        3
  •  5
  •   jtclaypool    8 年前

    E(g)[from(V(g)["name"])]
    
        4
  •  1
  •   Majid    7 年前

    我使用此函数获取所有节点的边数:

    sapply(V(g)$name, function(x) length(E(g)[from(V(g)[x])]))