在R

时间:2015-05-26 20:18:19

标签: r tree binary-search-tree

我有一个问题,如果weigths有时是负数,如何在R中的节点之间找到路径?我首先尝试找到最短路径(在此处找到帖子Find distance of route from get.shortest.paths()),但出现错误:

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

下面我把我使用的代码和数据集的开头。同样,如果某人能给我一个如何在树上找到最长路线的小费,我将不胜感激。

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

0 个答案:

没有答案