[R] Minimum Spanning Tree

Gábor Csárdi csardi at rmki.kfki.hu
Wed Apr 8 21:49:45 CEST 2009


Actually,

library(igraph)

tab <- read.csv("http://www.nabble.com/file/p22957493/sp_matrix.csv")
tab <- tab[,-1]

g <- graph.adjacency(as.matrix(tab), weighted=TRUE)
V(g)$label <- V(g)$name

mst <- as.undirected(minimum.spanning.tree(g))

lay <- layout.reingold.tilford(mst, root=which.max(degree(mst))-1)
lay <- cbind(lay[,2], lay[,1])    # rotate
x11(width=15, height=8)
plot(mst, layout=lay, vertex.size=25, vertex.size2=10,
     vertex.shape="rectangle", asp=FALSE,
     vertex.label.cex=0.7, vertex.color="white")

works relatively well for me on your graph. It doesn't for you?

Best,
Gabor

On Wed, Apr 8, 2009 at 9:14 PM, jpearl01 <joshearl1 at hotmail.com> wrote:
>
>
> Make the graph undirected first and then choose the right plotting
> parameters. E.g. the following works fine for me:
>
> set.seed(2)
> g <- erdos.renyi.game(100, 300, type="gnm", directed=TRUE)
> E(g)$weight <- runif(ecount(g))
> mst <- minimum.spanning.tree(g)
>
> mst <- simplify(as.undirected(mst))
> lay <- layout.reingold.tilford(mst, root=which.max(degree(mst))-1)
> plot(mst, layout=lay, vertex.size=5, asp=FALSE, vertex.color=NA,
>       vertex.frame.color=NA)
>
> G.
>
>
> Thanks for all your help Gabor,  However, I'm unable to get a display in R
> which is at all readable.  I'm not sure why that is (if the data is
> generated randomly, like in your example the tree builds/reads fine...
> however on my dataset whichever node is picked as the root just completely
> over powers the rest of the nodes which all clump together in a manner that
> is unreadable).  My dataset is:
> http://www.nabble.com/file/p22957493/sp_matrix.csv sp_matrix.csv
> If you would like to take a look.  However, as it stands I think I'll have
> to find another option.  Thanks again for all your efforts.
>
> ~josh
> --
> View this message in context: http://www.nabble.com/Minimum-Spanning-Tree-tp22934813p22957493.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Gabor Csardi <Gabor.Csardi at unil.ch>     UNIL DGM




More information about the R-help mailing list