Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

How to obtain the tree from igraph object in R?

$
0
0

I have a random directed weighted graph gg, it has the next structure:

gg <-
structure(list(10, TRUE, c(0, 0, 1, 2, 2, 5, 5, 6, 6, 6, 6, 9,
9, 9, 9, 9), c(6, 9, 3, 0, 5, 3, 7, 1, 3, 5, 8, 2, 4, 6, 7, 8
), c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), c(3,
7, 11, 2, 5, 8, 12, 4, 9, 0, 13, 6, 14, 10, 15, 1), c(0, 2, 3,
5, 5, 5, 7, 11, 11, 11, 16), c(0, 1, 2, 3, 6, 7, 9, 11, 13, 15,
16), list(c(1, 0, 1), structure(list(), .Names = character(0)),
    structure(list(name = c("C", "D", "I", "J", "K", "N", "O",
    "Q", "S", "T"), color = c("yellow", "red", "red", "red",
    "red", "red", "green", "red", "red", "green")), .Names = c("name",
    "color")), structure(list(weight = c(0.5, 0.5, 1, 0.333333333333333,
    0.333333333333333, 0.333333333333333, 0.333333333333333,
    0.25, 0.25, 0.25, 0.25, 0.2, 0.2, 0.2, 0.2, 0.2)), .Names = "weight")),
    <environment>), class = "igraph")

I need to find all walks from the root (yellow node) to leaves (red nodes). Leaves defined by (a) edge direction and (b) the distance -- from the root to the leave should be two edges only.

In my case, the root is C and leaves should be D, J, N, S, I, K, Q. I tried to define the (a) condition only.

root <- "C"
leaves = which(degree(gg, v = V(gg), mode = "out")==0, useNames = T)
leaves 
# J K Q S 
# 4 5 8 9 
plot(gg, layout = layout.reingold.tilford(gg, root=root),
        edge.arrow.size=0.2, edge.curved=T,
        edge.label = round(E(gg)$weight,2))

enter image description here

Question. How to define the (b) condition and add to leaves set D, N, I, K nodes?


Viewing all articles
Browse latest Browse all 201894

Trending Articles