【问题标题】:Dependency level structure from the adjacency matrix来自邻接矩阵的依赖级别结构
【发布时间】:2020-02-12 04:50:44
【问题描述】:

我有一个邻接矩阵,例如:

        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

有没有什么方法可以从邻接矩阵中获取 objetc 的依赖级别,以 1 或 0 显示每个级别中的对象?例如:

       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

我正在使用graph_from_adjacency_matrixget.adjedgelist(network, mode = "out") 方法从包igraph 中获取边缘。

我可以通过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

可重现的例子:

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")

【问题讨论】:

  • degree(network, mode="in") 你在找什么?
  • @chinsoon12 不行,使用 degree 方法只能获取其相邻边的数量,我需要一种反映所有节点之间依赖关系的树结构。
  • 只是一个快速的问题,为什么 r09 在 level2 而 r05 在 level4?
  • @chinsoon12 因为 r09 取决于 r08-r02 又取决于 I3,是到达节点的最大边数,所以 2 条边,r05 最多有 4 条边,如图像.抱歉,我的英语不太好。

标签: r igraph adjacency-matrix adjacency-list


【解决方案1】:

这是一种可能的方法:

#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

输出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

【讨论】:

  • 如果有深度结构就好了,但你已经做到了,非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-07
  • 1970-01-01
  • 1970-01-01
  • 2021-03-02
  • 2014-09-21
  • 2015-06-21
相关资源
最近更新 更多