igraph-help
[Top][All Lists]

## Re: [igraph] Generating a clustered graph

 From: Tamas Nepusz Subject: Re: [igraph] Generating a clustered graph Date: Wed, 10 Aug 2011 13:10:50 +0200 User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18) Gecko/20110617 Lightning/1.0b2 Thunderbird/3.1.11

```> I'm a newbie with R. Can you give a hint on how can I do it?
I'm a newbie as well because I usually work with the Python interface, but
here's a very crude solution that nevertheless works unless you have
thousands of clusters.

First, let's store the number of clusters:

num.clusters <- max(membership)

We then iterate over the edge list of the graph and check which clusters the
endpoints belong to:

edgelist <- get.edgelist(graph)
endpoints <- matrix(membership[edgelist], ncol=2, byrow=T)
edgelist <- get.edgelist(graph)

Then let's create a square matrix which will store the adjacency matrix of
our meta-graph:

Finally we create the meta-graph (clearing the diagonal of adj so we don't
have loop edges for each cluster):

Note that the above code is for igraph 0.6, which uses 1-based indexing
everywhere. If you use igraph 0.5.4 (which is very likely), you may have to
add 1 to some of the indices to make it work. Most likely you'll need this:

membership <- membership + 1
edgelist <- get.edgelist(graph) + 1

Also, the above code does not give weights for the edges in the meta-graph.
If you want to assign weights to the edges that specify how many edges go
between clusters in the original graph, you can do it as follows:

for (i in 1:ecount(graph)) {
endpoints[i,2]] + 1
}

I'm sure there's an easier and more efficient way of doing the above
calculation in R but I'm not that familiar with R and this is the best I
managed to get.

Cheers,
Tamas

```