I would like to go one step further with this question (Find total of second variable related to the distance of route from get.shortest.paths()). How does one get the matrix of distances between nodes, when the 'shortest' path is found using the newcost variable?
(My experience with igraph is very limited).
df2 = rbind(c(234,235,21.6,75),
c(234,326,11.0,35),
c(235,241,14.5,78),
c(326,241,8.2,98),
c(241,245,15.3,75),
c(234,245,38.46,65))
df2 = as.data.frame(df2)
names(df2) = c("start_id","end_id","newcost","distance")
require(igraph)
g2 <- graph.data.frame(df2, directed=FALSE)
tmp2 = shortest.paths(g2,weights=E(g2)$newcost)
tmp2 #this gives the matrix of newcost-weighted shortest distances
Where I could use help is how to find all paths, say using optimal.path <- get.shortest.paths
, and using sum(E(g2, path = optimal.path)$distance)
to create the matrix of distances
what I would really like is an edgelist of distances for all node pairs, like:
startid endid shortestdist
234 235 75
234 245 208
What is tricky about this problem is that newcost is used to find the shortest paths, but what I want is the sum of another variable - the distance variable on each shortest path between node pairs.