简体   繁体   中英

How to find number of nodes connected to two nodes in a graph?

I have a direct graph with the following adjacency matrix

as.matrix(read.table(header=T, text=
" A B C D E F
A 0 1 0 0 0 1
B 0 0 0 0 0 1
C 0 1 0 0 1 0
D 0 1 0 0 1 1
E 0 0 0 0 0 1
F 0 0 0 0 0 0"))

I want to do some matrix manipulation that can give me the number of unique nodes that follow two nodes. For example in the previous matrix the result should be

  A B C D E F
A|0 3 0 0 2 4|
B|3 3 3 3 3 5|
C|0 3 0 0 2 4|
D|0 3 0 0 2 4|
E|2 3 2 2 2 5|
F|4 5 4 4 5 4|

I'm using R for my coding. That would be great if somebody can help on that

I found the answer, I share it as it might be useful:

D<-colSums(A)
B<-matrix(D,ncol=length(D),nrow=length(D))
Result<-B+t(B)-t(A)%*%A

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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