3
votes

Q.I have a erdos.reyni graph. I infect a vertex and want to see what sequence of vertices the disease would follow? igraph has helful functions like 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. 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).

So suppose I hit vertex 1 (which is row 1 here). We see that it has outgoing links to vertex 4,5,18,22,23,24,25. So then the next vertices will be those connected to 4,5,18...25 i.e. those values in 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). and would look like this. Which is basically how the disease would spread if vertex 1, becomes infected.

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

2
interesting comments. Now that you guys insist. This matrix is the adjacency matrix with vector names instead of the binary flags. The output would be the epidemic flow model.. except that it is not modeling a disease but rather a bank default!! :) As for the hint... well from the last 2 weeks of coding i gathered that with high level languages like R/java, it was much better manipulating vectors than the old school char types in C++.user2217564
Wait, isn't this basically a breadth-first search through the graph?Marius
Have a look at graph.bfs(), specifically the order argument.Marius
@Marius yes.. that's exactly what i'm trying to achieve! Except since this is not a erdos.reyni directoinal graph the breadth-first will lead to an infinite sequence.. i just want to know how to get the fist few. will look at graph.bfs()user2217564
Breadth-first search works on undirected graphs because you mantain a list of the nodes you've visited, it doesn't end up infinite, it just gets to all reachable nodes.Marius

2 Answers

5
votes

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

enter image description here

1
votes

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.