简体   繁体   English

从邻接矩阵R,igraph获取传染链

[英]Get contagion chain from adjacency matrix, r, igraph

QI have a erdos.reyni graph. QI有一个erdos.reyni图。 I infect a vertex and want to see what sequence of vertices the disease would follow? 我感染了一个顶点,想知道该疾病会遵循哪些顶点顺序? igraph has helful functions like get.adjacency(), neighbors(). igraph具有多种功能,例如get.adjacency(),neighbors()。

Details. 细节。 This is the adjacency matrix with vertex names instead of 0,1 flags and i'm trying to get the contagion chain out of it. 这是顶点名称而不是0,1标志的邻接矩阵,而我正试图从中获取传染链。 Like the flow/sequence of an epidemic through a graph if a certain vertex is infected. 如果某个顶点被感染,就像通过图表的流行流/顺序一样。 Let's not worry about infection probabilities here (assume all vertices hit are infected with probability 1). 在这里,我们不必担心感染的可能性(假设命中的所有顶点都感染了可能性1)。

So suppose I hit vertex 1 (which is row 1 here). 因此,假设我到达了顶点1(这里是第1行)。 We see that it has outgoing links to vertex 4,5,18,22,23,24,25. 我们看到它具有到顶点4,5,18,22,23,24,25的传出链接。 So then the next vertices will be those connected to 4,5,18...25 ie those values in row4, row5, row18,... row25. 因此,下一个顶点将是那些连接到4,5,18 ... 25的顶点,即row4,row5,row18,... row25中的那些值。 Then, according to the model, the disease will travel through these and so forth. 然后,根据模型,疾病将通过这些疾病传播,依此类推。

I understand that I can pass a string to order the matrix rows. 我知道我可以传递一个字符串来排序矩阵行。 My problem is, I cannot figure out how to generate that sequence. 我的问题是,我无法弄清楚如何生成该序列。

The matrix looks like this. 矩阵看起来像这样。

    > channel
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
 [1,]    4    5   18   22   23   24   25   NA
 [2,]    6   10   11   18   25   NA   NA   NA
 [3,]    7   11   18   20   NA   NA   NA   NA
 [4,]   24   NA   NA   NA   NA   NA   NA   NA
 [5,]    1    3    9   13   14   NA   NA   NA
 [6,]    3    8    9   14   19   23   NA   NA
 [7,]    3    4    8   15   20   22   NA   NA
 [8,]    2    3   25   NA   NA   NA   NA   NA
 [9,]    3    4   11   13   20   NA   NA   NA
[10,]    4    5    8   15   19   20   21   22
[11,]    3   13   15   18   19   23   NA   NA
[12,]   11   13   16   NA   NA   NA   NA   NA
[13,]    4    6   14   15   16   17   19   21
[14,]    2    6   13   NA   NA   NA   NA   NA
[15,]    3   17   20   NA   NA   NA   NA   NA
[16,]    6   15   18   23   NA   NA   NA   NA
[17,]    2   25   NA   NA   NA   NA   NA   NA
[18,]    2    5   NA   NA   NA   NA   NA   NA
[19,]    3   11   NA   NA   NA   NA   NA   NA
[20,]    1    4    7   10   12   21   22   25
[21,]    2    4    6   13   14   16   18   NA
[22,]    1    3    4   15   23   NA   NA   NA
[23,]    1   16   24   NA   NA   NA   NA   NA
[24,]    7    8   19   20   22   NA   NA   NA
[25,]    7   12   13   17   NA   NA   NA   NA

I want to reorder this matrix based on a selection criteria as follows: 我想根据选择标准对该矩阵进行重新排序,如下所示:

R would be most helpful (but i'm interested in the algo so any python,ruby,etc.will be great).The resulting vector will have length of 115 (8x25=200 - 85 NAs=115). R将是最有帮助的(但我对算法感兴趣,因此任何python,ruby等都将很棒)。所得向量的长度为115(8x25 = 200-85 NAs = 115)。 and would look like this. 看起来像这样 Which is basically how the disease would spread if vertex 1, becomes infected. 基本上,如果顶点1被感染,疾病将如何传播。

4,5,18,22,23,24,25,24,1,3,9,13,14,2,5,1,3,4,15,23,1,16,24,7,8,19,20,22,7,12,13,17,7,8,19,20,22, 4,5,18,22,23,24,25,7,11,18,20...

What I know so far: 1. R has a package **igraph** which lets me calculate neighbors (graph, vertex, "out") 2. The same package can also generate get.adjlist(graph...), get.adjacency 到目前为止我所知道的:1. R有一个包**igraph** ,它可以让我计算邻居(graph, vertex, "out") 2.同一包也可以生成get.adjlist(graph...), get.adjacency

Finding a "contagion chain" like this is equivalent to a breadth-first search through the graph, eg: 像这样找到“传染链”,就相当于在图中进行了广度优先搜索,例如:

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. 函数t()转置矩阵。

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: NA值可以通过子设置删除:

> 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. 此外,Bioconductor的或CRAN的igraph程序包提供了图算法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM