Finding root vertices in largeish dataset in igraph on R

前端 未结 2 608
一整个雨季
一整个雨季 2021-01-16 12:10

Suppose you have an graph that you\'ve made from an edgelist, and there are a couple hundred vertices. What I\'m looking to do is to identify the initial set of vertices fro

2条回答
  •  遥遥无期
    2021-01-16 12:33

    enter code hereThe solution of G5W fails if the graph contains a self-loop. An alternative approach:

    library(igraph)
    set.seed(2017)
    g = erdos.renyi.game(12, 20, type="gnm", directed=TRUE)
    rts <- V(g)[degree(g, mode=c("in"), loops = F) == 0]        # find roots, that is, in-degree is zero
    paste(c("Roots: ", rts), collapse=" ")
    plot(g, layout=layout_with_sugiyama(g)$layout)              # plot graph in layers
    
    g2 <- simplify(g, remove.loops = T, remove.multiple = T)    # reduce to simple graph without loops
    stopifnot(max(clusters(g2, mode=c("strong"))$csize) == 1)   # stop when cycles
    
    E(g2)$weight <- -1                                          # shortest path is longest negative
    dis <- (-distances(g2, mode="in") )                         # matrix VxV of depth of layers, depth from top to bottom
    lay = as.matrix(apply(dis, 1, max))                         # maximum of distances in successors
    as.matrix(apply(-distances(g2, mode="out"), 1, max))        # or reverse from bottom to top
    
    plot(g, layout=layout_with_sugiyama(g, layer=lay)$layout)
    

提交回复
热议问题