Search code examples
rigraphadjacency-matrixadjacency-list

Dependency level structure from the adjacency matrix


I have an adjacency matrix like:

        r01-r07 r03 r04 r05 r06 r08-r02 r09 r10 I1 I2 I3 I4
r01-r07       0   0   0   1   0       0   0   0  0  0  0  0
r03           1   0   0   0   0       0   0   0  0  0  0  0
r04           1   0   0   0   0       0   0   0  0  0  0  0
r05           0   0   0   0   0       0   0   0  0  0  0  0
r06           0   1   0   0   0       0   0   0  0  0  0  0
r08-r02       0   1   0   0   0       0   1   0  0  0  0  0
r09           0   0   0   0   0       0   0   0  0  0  0  0
r10           0   0   0   1   0       0   0   0  0  0  0  0
I1            1   0   0   0   0       0   0   0  0  0  0  0
I2            0   0   1   0   0       0   0   0  0  0  0  0
I3            0   0   0   0   0       1   0   0  0  0  0  0
I4            0   0   0   0   0       0   1   0  0  0  0  0

Is there any way to get dependency level of a objetc from a adjacency matrix that show what objects are in each level by 1 or 0?, like:

       r01-r07 r03 r04 r05 r06 r08-r02 r09 r10 I1 I2 I3 I4
root         0   0   0   0   1       0   0   1  1  1  1  1
level1       0   0   1   0   0       1   0   0  0  0  0  0
level2       0   1   0   0   0       0   1   0  0  0  0  0
level3       1   0   0   0   0       0   0   0  0  0  0  0
level4       0   0   0   1   0       0   0   0  0  0  0  0

The drawn model

I'm using the graph_from_adjacency_matrix and get.adjedgelist(network, mode = "out") methods to get the edges from package igraph.

I can obtain the order by topo_sort(network, mode = "out")

+ 12/12 vertices, named, from c00e2ba:
 [1] r06     r10     I1      I2      I3      I4      r04     r08-r02 r03     r09     r01-r07 r05

Reproducible example:

library(igraph)
# Adjacency matrix
x <- matrix(c(0,0,0,1,0,0,0,0,0,0,0,0,
               1,0,0,0,0,0,0,0,0,0,0,0,
               1,0,0,0,0,0,0,0,0,0,0,0,
               0,0,0,0,0,0,0,0,0,0,0,0,
               0,1,0,0,0,0,0,0,0,0,0,0,
               0,1,0,0,0,0,1,0,0,0,0,0,
               0,0,0,0,0,0,0,0,0,0,0,0,
               0,0,0,1,0,0,0,0,0,0,0,0,
               1,0,0,0,0,0,0,0,0,0,0,0,
               0,0,1,0,0,0,0,0,0,0,0,0,
               0,0,0,0,0,1,0,0,0,0,0,0,
               0,0,0,0,0,0,1,0,0,0,0,0), ncol = 12, byrow = TRUE)
colnames(x) <- c("r01-r07", "r03", "r04", "r05", "r06", "r08-r02",
                 "r09", "r10", "I1", "I2", "I3", "I4")
row.names(x) <- c("r01-r07", "r03", "r04", "r05", "r06", "r08-r02",
                 "r09", "r10", "I1", "I2", "I3", "I4")

# Create the network
network <- graph_from_adjacency_matrix(as.matrix(x), mode = "directed")
# Edge list
print(get.adjedgelist(network,mode = "out"))
# Sorted order
print(topo_sort(network, mode = "out"))
# Visualization
plot.igraph(network, vertex.size = 15, edge.arrow.size = 0.5, vertex.label.dist=3,
   layout=layout.kamada.kawai, vertex.label.color="blue", edge.color="black")

Solution

  • Here is one possible approach:

    #find the root nodes
    deg <- degree(network, mode="in")==0
    roots <- names(deg)[deg]
    
    #get all paths from root to every other nodes
    sp <- lapply(roots, all_simple_paths, graph=network)
    
    #get the last node in these paths and number of edges to reach this last node
    dat <- do.call(rbind, lapply(unlist(sp, recursive=FALSE), 
        function(x) data.frame(node=names(x)[length(x)], dist=length(x))))
    
    #find the max depth for each non-root node
    depth <- tapply(dat$dist, dat$node, max)
    
    #construct required result
    ans <- matrix(0L, ncol=length(V(network)), nrow=max(depth))
    rownames(ans) <- c("root", paste0("level", seq_len(max(depth)-1)))
    colnames(ans) <- names(V(network))
    ci <- match(c(roots, names(depth)), colnames(ans))
    ans[cbind(c(rep(1, length(roots)), depth), ci)] <- 1L
    

    output ans:

           r01-r07 r03 r04 r05 r06 r08-r02 r09 r10 I1 I2 I3 I4
    root         0   0   0   0   1       0   0   1  1  1  1  1
    level1       0   0   1   0   0       1   0   0  0  0  0  0
    level2       0   1   0   0   0       0   1   0  0  0  0  0
    level3       1   0   0   0   0       0   0   0  0  0  0  0
    level4       0   0   0   1   0       0   0   0  0  0  0  0