I have a question, how to find path between nodes in R if weigths are sometimes negative? I tried first with finding the shortest path (found a post here Find distance of route from get.shortest.paths()), but an error appears:
Error in .Call("R_igraph_get_shortest_paths", graph, as.igraph.vs(graph, :
At structural_properties.c:4406 : Weight vector must be non-negative, Invalid value
Below i put the code which I used and the beginning of the dataset. Equally I would appreciate if sb could give me a tip how to find a longest route in a tree.
install.packages("igraph")
library(igraph)
start_id <- base.set$id.parent
end_id <- base.set$id
value <- base.set$value
tree <- as.data.frame(cbind(start_id, end_id, value))
gtree <- graph.data.frame(tree[-1,],directed=F)
class(tree)
tkplot(gtree)
E(gtree)
print(gtree, e=TRUE, v=TRUE)
## calculate shortest path between vertex 234 and 245
(tmp2 = get.shortest.paths(gtree, from='234', to='245',weights=E(gtree)$value))
# compute the min distances from '234' to all other vertices
tmp3 <- shortest.paths(g2,v='234',weights=E(g2)$value)
# print min distance from '234' to '245'
tmp3[1, which(V(g2)$name == '245')]
###data set
start_id end_id value
1 NA 0 0.000000000
2 0 1 -0.372677962
3 0 2 0.250537488
4 0 3 0.580284747
5 0 4 -0.674850541
6 0 5 1.958636421
7 0 6 -2.651893647
8 0 7 0.072457405
9 1 8 -0.357686434
10 1 9 -0.934105631
11 1 10 -0.752714220
12 1 11 -1.068627403
13 2 12 -0.281384892
14 2 13 -1.264358146
15 2 14 -1.043058993
16 2 15 -0.409834522
17 3 16 -0.302880563
18 3 17 -0.423105784
19 3 18 1.104676924
20 3 19 -0.497643735
21 3 20 -2.280475263
22 3 21 1.316331335
23 3 22 -1.143292548