I want to obtain a list of mutually connected nodes between every pair of nodes in my graph:
library(igraph)
G <- graph(c(1,2,1,3,1,4,2,4, 2,3,2,5,3,5,4,5,5,6,5,7,7,8,7,9), directed=F)
plot(G)

- the edge is undirected.
In this graph, for instance, node 1 and 2 share common nodes 3 and 4. And node 1 and 3 share common node 2. I would like to get a list of this or as a format of a data frame..
Is there a command for getting something like either one of these:
(1)
node1 node2 mutual
1 2 3, 4
1 3 2
1 4 2
2 3 1, 5
or (2)
node1 node2 mutual
1 2 3
1 2 4
1 3 2
1 4 2
2 3 1
2 3 5
I was able to get the number of mutual nodes between two nodes using this code:
# function to count the number of mutual friends between every pair of nodes
mutual_friends <- function(G) {
# initialize an emptry matrix to store number of mutual friends between pairs of nodes
num_nodes <- vcount(G)
mutual_friends <- matrix(0, nrow=num_nodes, ncol=num_nodes)
# loop over each node
for (node in 1:num_nodes) {
# get this node's list of friends
friends <- neighbors(G, node)
# add a count of 1 between all pairs of the node's friends
for (i in friends)
for (j in friends)
mutual_friends[i, j] = mutual_friends[i, j] + 1
}
# make the output readable with column names
dimnames(mutual_friends) <- list(row=V(G)$name, col=V(G)$name)
diag(mutual_friends) <- NA
mutual_friends
}
(coding credit to: https://rstudio-pubs-static.s3.amazonaws.com/72599_65ecae185590432cb2373df4825d2ef9.html#connected-components
But I'm struggling with getting a list of the mutual nodes between every pair of nodes. I appreciate any kind of advice and help. Thanks!