igraph-help
[Top][All Lists]

## Re: [igraph] Calculating average shortest path

 From: Gábor Csárdi Subject: Re: [igraph] Calculating average shortest path Date: Sat, 5 Mar 2011 19:27:31 -0500

```Hi Claudia,

when you use shortest.path(), you also count the paths from each
vertex to itself, this is why the result is different. These self
paths (always of length zero) are not counted in
average.shortest.paths().

undiag <- function(x) { diag(x) <- NA; x[is.infinite(x)] <- NA ;
na.omit(as.vector(x)) }
mean(undiag(shortest.paths(g1)))
mean(undiag(shortest.paths(g2, mode="out")))

Best,
Gabor

On Sat, Mar 5, 2011 at 5:53 PM, Claudia Muller-Birn <address@hidden> wrote:
> Dear all,
>
> I have a rather simple question regarding calculating the average shortest
> path length. There are actually to ways calculating it and I am wondering why
> I get two different results. Here is my toy example:
>
>>  g1 <- graph( c( 0,1, 1,2, 2,1, 2,3 ), directed=FALSE )
>>  g2 <- graph( c( 0,1, 1,2, 2,1, 2,3 ), directed=TRUE )
>
> First possibility to calculate the average shortest path (or is it just one
> path???)
>> average.path.length(g1, directed=FALSE)
> [1] 1.666667
>> average.path.length(g2, directed=TRUE)
> [1] 1.571429
>
> Second possibility would be by just calling the mean
>> sp1 <- shortest.paths(g1)
>> mean(sp1,na.rm=TRUE)
> [1] 1.25
>> sp2 <- shortest.paths(g2, v=V(g2), mode='out')
>> sp2[is.infinite(sp2)]<-NA
>> mean(sp2,na.rm=TRUE)
> [1] 1
>
> In my opinion, the second way to calculate the average shortest paths makes
> much more sense but then I am wondering about the meaning and calculation of
> the first possibility based on average.path.length. How is the latter
> function implemented or do I miss some settings?
>
> Thank you very much for any help or idea.
>
> Best,
> Claudia
>
>
> _______________________________________________
> igraph-help mailing list
> http://lists.nongnu.org/mailman/listinfo/igraph-help
>

--