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

    %% Shortest Path Distance for All Node Pairs
% Create and plot a graph.

% Copyright 2015 The MathWorks, Inc.

s = [1 1 1 2 5 5 5 8 9 9];
t = [2 3 4 5 6 7 8 9 10 11];
G = graph(s,t);
plot(G)

%%
% Calculate the shortest path distance between all node pairs in the graph.
% Since the graph edges do not have weights, all edge distances are taken
% to be 1.
d = distances(G)

%%
% |d| is symmetric because |G| is an undirected graph. In general |d(i,j)|
% is the length of the shortest path between node |i| and node |j|, and for
% undirected graphs this is equivalent to |d(j,i)|. 
%
% For example, find the length of the shortest path between node 1 and node
% 10.
d(1,10)