www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/ShortestPathIgnoringEdgeWeightsExample.m
%% Shortest Path Ignoring Edge Weights % Create and plot a graph with weighted edges, using custom node % coordinates. % Copyright 2015 The MathWorks, Inc. s = [1 1 1 1 1 2 2 7 7 9 3 3 1 4 10 8 4 5 6 8]; t = [2 3 4 5 7 6 7 5 9 6 6 10 10 10 11 11 8 8 11 9]; weights = [1 1 1 1 3 3 2 4 1 6 2 8 8 9 3 2 10 12 15 16]; G = graph(s,t,weights); x = [0 0.5 -0.5 -0.5 0.5 0 1.5 0 2 -1.5 -2]; y = [0 0.5 0.5 -0.5 -0.5 2 0 -2 0 0 0]; p = plot(G,'XData',x,'YData',y,'EdgeLabel',G.Edges.Weight); %% % Find the shortest path between nodes 6 and 8 based on the graph edge % weights. Highlight this path in green. [path1,d] = shortestpath(G,6,8) %% % highlight(p,path1,'EdgeColor','g') %% % Specify |Method| as |unweighted| to ignore the edge weights, instead % treating all edges as if they had a weight of 1. This method produces a % different path between the nodes, one that previously had too large of a % path length to be the shortest path. Highlight this path in red. [path2,d] = shortestpath(G,6,8,'Method','unweighted') %% % highlight(p,path2,'EdgeColor','r')