2nd degree connections in igraph

I think this works correctly, but I am looking to mimic something similar to a Facebook Friend suggestion. Simply, I’m looking to find connections of the 2nd degree (friends of your friends with whom you have no connection). I want to save this as a directed graph and identify external connections of the second degree (to which your friends connect).

I believe that my dummy code achieves this, but since the link is to indexes, not vertex labels, I was hoping you could help me change the code to return useful names.

### create some fake data library(igraph) from <- sample(LETTERS, 50, replace=T) to <- sample(LETTERS, 50, replace=T) rel <- data.frame(from, to) head(rel) ### lets plot the data g <- graph.data.frame(rel) summary(g) plot(g, vertex.label=LETTERS, edge.arrow.size=.1) ## find the 2nd degree connections d1 <- unlist(neighborhood(g, 1, nodes="F", mode="out")) d2 <- unlist(neighborhood(g, 2, nodes="F", mode="out")) d1;d2; setdiff(d2,d1) 

Returns

 > setdiff(d2,d1) [1] 13 

Any help you can provide would be great. Obviously, I want to stay in R.

+4
source share
2 answers

You can index back to the top of the graph, for example:

 > V(g)[setdiff(d2,d1)] Vertex sequence: [1] "B" "W" "G" 

Also check the ?V ways to get this type of information by direct indexing.

+4
source

You can use the $ G $ adjacency matrix of the $ g $ graph (is there no latex here?). One of the properties of the adjacency matrix is ​​that its nth power gives you the number $ n $ -walks (paths of length n).

 G <- get.adjacency(g) G2 <- G %*% G # G2 contains 2-walks diag(G2) <- 0 # take out loops G2[G2!=0] <- 1 # normalize G2, not interested in multiplicity of walks g2 <- graph.adjacency(G2) 

The edge in the graph g2 is a "each other" relationship.

+2
source

Source: https://habr.com/ru/post/1379664/


All Articles