Reputation: 1160
Having a weighted directed network such as this:
library(igraph)
g <- erdos.renyi.game(25, 1/10, directed = TRUE)
E(g)$weight <- runif(length(E(g)), 1, 5)
When running the shortest path function sp <- shortest.paths(g)
, I obviously get a matrix with the sum of the weighed edges in between. However, I would like to count how many vertices are on that path.
The idea comes from searching for the shortest paths for a train network, with the edges as train stations, being that I would like to see the amount of transfers (in between vertices or nodes), for each shortest path.
EDIT: For example if a shortest path is A to C, I want to know how many vertices are in between. For example if the full path is A - E - B - C, then the E and B are the intermediate vertices so I would have a value of 2. The full vertices path is ok as well, so in this case it would give me 4 vertices for the full shortest path.
Upvotes: 1
Views: 477
Reputation: 1160
Found an alternate solution to the problem.
sp <- shortest_paths(g, V(g), V(g), mode = "all",
weights = E(g)$weight, output = "vpath")$vpath
lengths(sp)
This gives the exact number of vertices on that path. So for the intermediate stops it's just a matter of subtracting 2 to the value.
Upvotes: 1