[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [igraph] Generating a clustered graph
From: |
Víctor Pascual Cid |
Subject: |
Re: [igraph] Generating a clustered graph |
Date: |
Wed, 10 Aug 2011 16:03:18 +0200 |
Thank you very much for your help!
Your solution didn't work for me, spcecially the line:
> endpoints <- matrix(membership[edgelist], ncol=2, byrow=T)
Nevertheless, your email gave me some hints on how to develop a very first
approach (presumably very inefficient...) that seems to work (with a very first
bunch of tests):
num.clusters <- max(membership)
for (i in 1:nrow(edgelist)) {
newedgelist[i,1] <- membership[as.integer(edgelist[i,1]) +1]
newedgelist[i,2] <- membership[as.integer(edgelist[i,2]) +1]
}
adj <- mat.or.vec(num.clusters, num.clusters)
for (i in 1:nrow(newedgelist)) {
if (newedgelist[i,1] != newedgelist[i,2]) {
adj[as.integer(newedgelist[i,1]),
as.integer(newedgelist[i,2])] <- adj[as.integer(newedgelist[i,1]),
as.integer(newedgelist[i,2])] + 1
}
}
cluster.graph <- graph.adjacency(adj, mode="undirected", diag=FALSE)
The algorithm assumes that edgelist contains a pairs table with string values,
which refer to the id of the nodes in the graph.
Hope that can be useful for the community.
Any comments on how to improve it will be more than welcome!
Víctor
On Aug 10, 2011, at 13:10 , Tamas Nepusz wrote:
>> 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:
>
> adj <- mat.or.vec(num.clusters, num.clusters)
> adj[endpoints] <- 1
>
> Finally we create the meta-graph (clearing the diagonal of adj so we don't
> have loop edges for each cluster):
>
> cluster.graph <- graph.adjacency(cluster.graph, mode="undirected", diag=F)
>
> 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:
>
> adj <- mat.or.vec(num.clusters, num.clusters)
> for (i in 1:ecount(graph)) {
> adj[endpoints[i,1], endpoints[i,2]] <- adj[endpoints[i,1],
> 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
>
> _______________________________________________
> igraph-help mailing list
> address@hidden
> https://lists.nongnu.org/mailman/listinfo/igraph-help