例如下面的代碼。妳看每壹組返回值中,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