作业帮 > 综合 > 作业

matlab 中有这样一个函数:[dist,path,pred] = graphshortestpath(G,S,T),

来源:学生作业帮 编辑:大师作文网作业帮 分类:综合作业 时间:2024/11/11 12:49:57
matlab 中有这样一个函数:[dist,path,pred] = graphshortestpath(G,S,T),
matlab 中有这样一个函数:[dist,path,pred] = graphshortestpath(G,S,T),
它表示从S到每个节点的最短路径中,目标节点的先驱,即目标节点的前面一个节点.
例如下面的代码.你看每一组返回值中,path向量的倒数第二个数,跟pred中相应节点的数字是不是相同.即依次抽取path的倒数第二个值,组成了pred数组.
>> W = [.41 .99 .51 .32 .15 .45 .38 .32 .36 .29 .21];
>> DG = sparse([6 1 2 2 3 4 4 5 5 6 1],[2 6 3 5 4 1 6 3 4 3 5],W);
>> [dist,path,pred] = graphshortestpath(DG,1,1)
dist =
0
path =
1
pred =
0 6 5 5 1 4
>> [dist,path,pred] = graphshortestpath(DG,1,2)
dist =
1.3600
path =
1 5 4 6 2
pred =
0 6 5 5 1 4
>> [dist,path,pred] = graphshortestpath(DG,1,3)
dist =
0.5300
path =
1 5 3
pred =
0 6 5 5 1 4
>> [dist,path,pred] = graphshortestpath(DG,1,4)
dist =
0.5700
path =
1 5 4
pred =
0 6 5 5 1 4
>> [dist,path,pred] = graphshortestpath(DG,1,5)
dist =
0.2100
path =
1 5
pred =
0 6 5 5 1 4
>> [dist,path,pred] = graphshortestpath(DG,1,6)
dist =
0.9500
path =
1 5 4 6
pred =
0 6 5 5 1 4