[R] Weighted Graph Link strength ( I am making mistake please help)

Gábor Csárdi csardi at rmki.kfki.hu
Wed Mar 25 20:20:35 CET 2009


Hello Nathan,

a couple of points.

1) You did not write it, but it seems that you use the igraph package.
It might be worth pointing this out, and also, to post to the
igraph-help mailing list if you don't get any answer here.

2) igraph can easily create graphs from edge lists, there is no need
to construct an adjacency matrix by hand.

3) Only the (not yet released) 0.6 version of igraph supports weighted
betweenness calculation, version 0.5.1 simply ignores the weights.
(And then all betweenness values are zero for your full graph.)

4) You have to call the edge attribute containing the weights 'weight'
(lowercase) or explicitly supply them in the betweenness() call,
otherwise they are ignored.

5. So here is how to do what you want, with igraph 0.6. Suppose you
have the following in the /tmp/test.csv file:
----------------------------------------
Node1,Node2,weight
1,2,5
2,3,30
2,4,30
1,4,5
3,4,30
1,3,2
----------------------------------------
Then the code is:

library(igraph)
mat <- read.csv("/tmp/text.csv")
g <- graph.data.frame(mat, dir=FALSE)
betweenness(g)

and the result is
3 0 0 0

Best,
Gabor

On Wed, Mar 25, 2009 at 7:56 PM, Sur Nathan
<surendar.swaminathan at gmail.com> wrote:
>
> Hello R experts,
>
>   I went through R mailing,Nabble R.I could not find solution.Can someone
> help me.
>
> I have undirected Graph.
>
> Here  is an example of spreadsheet I have( Unique 3559 Nodes)
>
> snippet of 4 rows.
>
> Node1   Node2   Weights
> 1       2       5
> 2       3       30
> 2       4       30
> 1       4       5
> 3       4       30
> 1       3       2
>
>
> I created a program reading the csv and created a Adjacency matrix.From the
> adjacency matrix I created graph.I am not sure the mistake I am making.
>
> Program to read and create Matrix
>
> matrixmy<-scan("test.csv",sep=",",skip=1)
>
> arr=array(0,dim=c(4,4))
> a<-1
>
> while(a<=length(matrixmy))
>  {
>  i=matrixmy[a]
>  a<-a+1
>  j=matrixmy[a]
>  a<-a+1
>
>  k=matrixmy[a]
>
>
> arr[i,j]<-k
> arr[j,i]<-k
>
>  a<-a+1
>  }
>
> Created matrix
>
> mat1<-matrix(data=arr,nrow=4,ncol=4)
>
> g<-graph.adjacency(adjmatrix=mat1,mode=c("undirected"))
>
> betweenness(g)
>
> The answer I get is
>
> 0 0 0 0
>
> I was expecting Node 1 will have high value.
>
> Can someone tell me why it is happening like this.
>
> Thanks in advance.
>
> Nathna
>
>
> --
> View this message in context: http://www.nabble.com/Weighted-Graph-Link-strength-%28-I-am-making-mistake-please-help%29-tp22707957p22707957.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