igraph-help
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [igraph] A question about shortest.paths {igraph}


From: Tamás Nepusz
Subject: Re: [igraph] A question about shortest.paths {igraph}
Date: Mon, 6 Aug 2012 10:51:23 +0200

Hi Moses, 

> 1. Is there a way to get all geodesics together at once, instead of
> running get.shortest.paths(g, i, 1:n) separately n times?

You can use a for loop, which is what get.shortest.paths would do internally 
anyway:

result <- list()
for (i in 1:n) { result <- c(result, get.shortest.paths(g, i, 1:n)) }

It could be the case that there is a more efficient solution but I'm not an 
expert in R. Also, note that you might be interested in get.all.shortest.paths 
instead of get.shortest.paths -- the former returns only a single path for any 
vertex pair v1-v2 even if there are multiple geodesics between v1 and v2 with 
the same length. If you use get.all.shortest.paths, you will need only the $res 
component of the result:

for (i in 1:n) { result <- c(result, get.all.shortest.paths(g, i, 1:n)$res) }

> 2. Since, in my data, vertices have an attribute (say, taking values
> 1, 2, 3, ...), is it possible to filter out from the outcome of the
> previous computations only those geodesics which contain at least one
> intermediary vertex taking a particular value of the attribute? And
> how can I do this?

# Construct a list containing the acceptable intermediate vertices
acceptable.vertices <- which(V(g)$attribute == X)
# Get the shortest paths from a particular vertex
paths <- get.shortest.paths(g, v, 1:n)
# Filter on those that contain at least one acceptable vertex in the path
paths <- paths[acceptable.vertices %in% paths]

Best,
T.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]