www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/HighlightShortestPathExample.m

    %% Highlight Shortest Path
% Create and plot a graph. Return a handle to the |GraphPlot| object, |h|.

% Copyright 2015 The MathWorks, Inc.

n = 10;
A = delsq(numgrid('L',n+2));
G = graph(A,'OmitSelfLoops'); 
G.Edges.Weight = ones(numedges(G),1);
h = plot(G);

%%
% Highlight the shortest path between nodes 74 and 21 by changing the color
% of the nodes and edges along the path to green.
path = shortestpath(G,74,21);
highlight(h,path,'NodeColor','g','EdgeColor','g')