Get contagion chain from adjacency matrix, r, igraph

我的梦境 提交于 2019-11-30 07:42:30

Finding a "contagion chain" like this is equivalent to a breadth-first search through the graph, e.g.:

library(igraph)
set.seed(50)
g = erdos.renyi.game(20, 0.1)
plot(g)
order = graph.bfs(g, root=14, order=TRUE, unreachable=FALSE)$order

Output:

> order
 [1]  14   1   2  11  16  18   4  19  12  17  20   7   8  15   5  13   9 NaN NaN NaN

It's not clear how you define the ordering of the rows, so... just a few hints:

You can select a permutation/combination of rows by passing an index vector:

> (m <- matrix(data=1:9, nrow=3))
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
> m[c(2,3,1),]
     [,1] [,2] [,3]
[1,]    2    5    8
[2,]    3    6    9
[3,]    1    4    7

The function t() transposes a matrix.

The matrix is stored in columns-first (or column-major) order:

> as.vector(m)
[1] 1 2 3 4 5 6 7 8 9

NA values can be removed by subsetting:

> qq <- c(1,2,NA,5,7,NA,3,NA,NA)
> qq[!is.na(qq)]
[1] 1 2 5 7 3

Also, graph algorithms are provided by Bioconductor's graph or CRAN's igraph packages.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!