A is an adjacency matrix stored as a matrix object:
#1 2 3 4 5 6 7 8 9
A <- matrix(data=c( 0,0,1,1,0,0,0,1,0, #1
0,0,0,0,1,0,0,0,0, #2
1,0,0,0,0,0,0,0,0, #3
1,0,0,0,0,0,0,0,1, #4
0,1,0,0,0,1,0,0,0, #5
0,0,0,0,1,0,0,0,0, #6
0,0,0,0,0,0,0,0,0, #7
1,0,0,0,0,0,0,0,0, #8
0,0,0,1,0,0,0,0,0 ),#9
nrow=9, ncol=9)
The graph of A looks like this:
I am trying to identify the neighbors of neighbors, and to create a new adjacency matrix N with values that capture the neighbors of some node j who are exactly one step away from a given node i.
In A for example, 3 is neighbors with 1, and 1 is neighbors with 4 and 8. But 3 is neighbors with neither 4 nor 8. In the desired adjacency matrix N, I want the row/column representing 3 to contain a value of 1 for columns that represent nodes 4 and 8, but not node 1. The solution matrix N should look like this:
#1 2 3 4 5 6 7 8 9
N <- matrix(data=c( 0,0,0,0,0,0,0,0,1, #1
0,0,0,0,0,1,0,0,0, #2
0,0,0,1,0,0,0,1,0, #3
0,0,1,0,0,0,0,1,0, #4
0,0,0,0,0,0,0,0,0, #5
0,1,0,0,0,0,0,0,0, #6
0,0,0,0,0,0,0,0,0, #7
0,0,1,1,0,0,0,0,0, #8
1,0,0,0,0,0,0,0,0 ),#9
nrow=9, ncol=9)
For clarity, here are the neighbors of neighbors in A that become the information stored in N.
#1: 9
#2: 6
#3: 4, 8
#4: 8, 3
#5: -
#6: 2
#7: -
#8: 3, 4
#9: 1
This post asks a similar question, but uses a different language and involves a slightly different solution.
Note: the ideal solution would scale to networks with 100 nodes and run tens of thousands of times, so I'm hoping for a solution that's efficient.
