Retrieve all edges of a community in igraph - r

How can I get all edges of a community in igraph?
g <- graph.edgelist(edges, directed=FALSE)
c <- edge.betweenness.community(g)
listOfCommunities <- communities(c)
listOfCommunities stores lists of verteces for each community. I would like to get all the edges for each community.

Related

R network package - is there a faster way to add edges?

I have an adjacency matrix (m) of 7000 nodes. Creating a network from this using
n <- 7000
m <- matrix(rbinom(3*n, 1, 0.2), n,n)
diag(m) <- 0
g <- network(m, directed = F)
is very slow. Is there a more efficient way of creating large random networks in R? Alternative methods such as using iGraph would also be appreciated.
With igraph, you can do:
library('igraph')
g <- graph.adjacency(m, mode='undirected')
The edges can then be retrieved using E(g) and vertices using V(g).
Check out the igraph docs for more information.

R iGraph: degree in the case of bidirectional edges

I have noticed that the function degree in iGraph doesn't straighforwardly allow to calculate the degree of the undirected skeleton graph of a directed graph, whenever bidirectional edges are involved.
For example,
g <-graph_from_literal( a-+b,a++c,d-+a,a-+e,a-+f )
d1 <- degree(g,v='a',mode="all")
# 6
nn <- unique(neighbors(g,'a',mode='all'))
d2 <- length(nn)
# 5
As I wanted d2, instead of d1, I have used a different route based on finding the neighbors of the considered vertex.
My question is: is there a better/faster way to do this, maybe using some other iGraph function that I'm not aware of?
Create an undirected copy of the graph, collapse the multiple edges in the undirected graph into a single edge, and then calculate the degree on that:
> g2 <- as.undirected(g, mode="collapse")
> degree(g2)

Ordering cluster list by cluster size, R igraph

I have a network (g) with thousands of clusters, however I can't seem to figure out how to order them by size. It looks like the membership attribute sorts clusters somewhat arbitrarily. For example:
c <- clusters(g)
c$membership
gs <- induced.subgraph(g, c$membership==1)
This will indeed give me the largest cluster, but if I try
gs <- induced.subgraph(g, c$membership==2)
It doesn't give me the second largest cluster, but an arbitrary cluster that happens to be second in the list.
Is there a way to order c$membership according to cluster size, i.e., 1 – largest, 2 – second largest, etc.?
You could do it this way:
# largest subgraph
gs <- induced.subgraph(g, c$membership==order(-c$csize)[1])
# second largest subgraph
gs <- induced.subgraph(g, c$membership==order(-c$csize)[2])
# etc...
Here's a working example.
library(igraph)
g <- graph.full(5) %du% graph.full(4) %du% graph.full(3)
set.seed(1) # for reproducible plots
par(mar=c(0,0,0,0),mfrow=c(1,2))
plot(g)
c <- clusters(g)
gs <- induced.subgraph(g, c$membership==order(-c$csize)[1])
plot(gs)

extract a connected subgraph from a subset of vertices with igraph

I have a graph G(V,E) unweighted, undirected and connected graph with 12744 nodes and 166262 edges. I have a set of nodes (sub_set) that is a subset of V. I am interested in extracting the smallest connected subgraph where sub_set is a part of this new graph. I have managed to get a subgraph where my subset of nodes is included but I would like to know if there is a way to minimise the graph.
Here is my code (adapted from http://sidderb.wordpress.com/2013/07/16/irefr-ppi-data-access-from-r/)
library('igraph')
g <- erdos.renyi.game(10000, 0.003) #graph for illustrating my propose
sub_set <- sample(V(g), 80)
order <- 1
edges <- get.edges(g, 1:(ecount(g)))
neighbours.vid <- unique(unlist(neighborhood(g, order, which(V(g) %in% sub_set))))
rel.vid <- edges[intersect(which(edges[,1] %in% neighbours.vid), which(edges[,2] %in% neighbours.vid)),]
rel <- as.data.frame(cbind(V(g)[rel.vid[,1]], V(g)[rel.vid[,2]]), stringsAsFactors=FALSE)
names(rel) <- c("from", "to")
subgraph <- graph.data.frame(rel, directed=F)
subgraph <- simplify(subgraph)
I have read this post
minimum connected subgraph containing a given set of nodes, so I guess that my problem could be "The Steiner Tree problem", is there any way to try to find a suboptimal solution using igraph?
Not sure if that's what you meant but
subgraph<-minimum.spanning.tree(subgraph)
produces a graph with the minimum number of edges in which all nodes stay connected in one component.

generating a community graph in igraph

I have been searching for an answer to this question but could not find any mention, so I decided to post here. I am trying to see if igraph or any packages provide a simple way to create a "community graph" where each node represents a community in the network and the ties represent ties between the communities. I can get the community detection algorithm to work fine in igraph, but I could not find a way to collapse the results to just show connections between each community. Any assistance would be appreciated.
You can simply use the contract.vertices() function. This contracts groups of vertices into a single vertex, essentially the same way you want it. E.g.
library(igraph)
## create example graph
g1 <- graph.full(5)
V(g1)$name <- 1:5
g2 <- graph.full(5)
V(g2)$name <- 6:10
g3 <- graph.ring(5)
V(g3)$name <- 11:15
g <- g1 %du% g2 %du% g3 + edge('1', '6') + edge('1', '11')
## Community structure
fc <- fastgreedy.community(g)
## Create community graph, edge weights are the number of edges
cg <- contract.vertices(g, membership(fc))
E(cg)$weight <- 1
cg2 <- simplify(cg, remove.loops=FALSE)
## Plot the community graph
plot(cg2, edge.label=E(cg2)$weight, margin=.5, layout=layout.circle)

Resources