Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2nd Degree Connections in igraph

I think have this working correctly, but I am looking to mimic something similar to Facebook's Friend suggestion. Simply, I am looking to find 2nd degree connections (friends of your friends that you do not have a connection with). I do want to keep this as a directed graph and identify the 2nd degree outward connections (the people your friends connect to).

I believe my dummy code is achieving this, but since the reference is on indices and not vertex labels, I was hoping you could help me modify the code to return useable 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 will be great. Obviously I am looking to stay within R.

like image 360
Btibert3 Avatar asked Nov 04 '11 15:11

Btibert3


2 Answers

You can index back into the graph vertices like:

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

Also check out ?V for ways to get at this type of info through direct indexing.

like image 100
John Colby Avatar answered Sep 26 '22 23:09

John Colby


You can use the adjacency matrix $G$ of the graph $g$ (no latex here?). One of the properties of the adjacency matrix is that its nth power gives you the number of $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)

An edge in graph g2 represents a "friend-of-a-friend" bond.

like image 38
Ryogi Avatar answered Sep 22 '22 23:09

Ryogi