www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/ShortestPathInWeightedGraphExample.m
%% Shortest Path in Weighted Graph % Create and plot a graph with weighted edges. % Copyright 2015 The MathWorks, Inc. s = [1 1 1 2 2 6 6 7 7 3 3 9 9 4 4 11 11 8]; t = [2 3 4 5 6 7 8 5 8 9 10 5 10 11 12 10 12 12]; weights = [10 10 10 10 10 1 1 1 1 1 1 1 1 1 1 1 1 1]; G = graph(s,t,weights); plot(G,'EdgeLabel',G.Edges.Weight) %% % Find the shortest path between nodes 3 and 8, and specify two outputs to % also return the length of the path. [path,d] = shortestpath(G,3,8) %% % Since the edges in the center of the graph have large weights, the % shortest path between nodes 3 and 8 goes around the boundary of the graph % where the edge weights are smallest. This path has a total length of 4.