In a network of passes among basketball players I want to:
- Detect open triangles in the network
- Count the number of unique players in brokering position (A passes to B & C; B & C don't pass to each other; A is brokering)
- Count the number of times these player broker an open triangle
Following this question Extracting Open Triangles in R Igraph (Network Analysis) we can do the following:
library(igraph)
set.seed(1234)
G <- sample_gnm(10, 15)
G
IGRAPH 72f8e6a U--- 10 15 -- Erdos renyi (gnm) graph
+ attr: name (g/c), type (g/c), loops (g/l), m (g/n)
+ edges from 72f8e6a:
[1] 1-- 3 1-- 4 3-- 4 1-- 5 3-- 5 6-- 7 3-- 8 4-- 8 6-- 8 7-- 8 2-- 9 6-- 9 7-- 9 4--10 9--10
plot(G)
Find the open triangles:
openTriList <- unique(do.call(c, lapply(as_ids(V(G)), function(v) {
do.call(c, lapply(as_ids(neighbors(G, v)), function(v1) {
v2 <- as_ids(neighbors(G, v1))
v2 <- v2[shortest.paths(G, v, v2) == 2]
if(length(v2) != 0) {
lapply(v2, function(vv2) { c(v, v1, vv2)[order(c(v, v1, vv2))] })
} else { list() }
}))
})))
The results are correct:
do.call(rbind, openTriList)
[,1] [,2] [,3]
[1,] 1 3 8
[2,] 1 4 8
[3,] 1 4 10
[4,] 2 6 9
[5,] 2 7 9
[6,] 2 9 10
[7,] 3 4 10
[8,] 3 6 8
[9,] 3 7 8
[10,] 1 4 5
[11,] 3 4 5
[12,] 4 6 8
[13,] 4 7 8
[14,] 4 9 10
[15,] 3 5 8
[16,] 6 9 10
[17,] 7 9 10
[18,] 4 8 10
[19,] 6 8 9
[20,] 7 8 9
How do we find the players that are brokers?
- Player 2 is in this list because it is part of an open triangle, but is not a broker. We ignore this player.
And how can we efficiently count the number of times these player broker an open triangle?
- Player 9 is brokering 5 open triangles.
[The real data holds millions of passes and several thousands of players. So performance is an important aspect. Using combn results in extremely long computational times. Are there faster ways of doing this? Perhaps getting the adjacency graph to build a sparse matrix and converting it into a data.table object for joining by neighbours? See this link. ]
