%% Shortest Path in Multigraph
% Plot the shortest path between two nodes in a multigraph and highlight the specific edges that are traversed.
%
% Create a weighted multigraph with five nodes. Several pairs of nodes have more than one edge between them. Plot the graph for reference.

G = graph([1 1 1 1 1 2 2 3 3 3 4 4],[2 2 2 2 2 3 4 4 5 5 5 2],[2 4 6 8 10 5 3 1 5 6 8 9]);
p = plot(G,'EdgeLabel',G.Edges.Weight);

%
% Find the shortest path between node 1 and node 5. Since several of the node pairs have more than one edge between them, specify three outputs to shortestpath to return the specific edges that the shortest path traverses.

[P,d,edgepath] = shortestpath(G,1,5)

%
% The results indicate that the shortest path has a total length of 11 and follows the edges given by G.Edges(edgepath,:).

G.Edges(edgepath,:)

%
% Highlight this edge path by using the highlight function with the 'Edges' name-value pair to specify the indices of the edges traversed.

highlight(p,'Edges',edgepath)

% Copyright 2012 The MathWorks, Inc.