%% Simplify Graph Using Specific Edge Variables
% Show how to simplify a multigraph using the 'PickVariable' and 'AggregationVariables' name-value pairs.
%
% Create a multigraph where the nodes represent locations and the edges represent modes of transport. The edges have properties that reflect the cost and time of each mode of transportation. Preview the edges table.

G = graph([1 1 1 1 1 1 2 2 2],[2 2 2 3 3 3 3 3 3],[],{'New York', 'Boston', 'Washington D.C.'});
G.Edges.Mode = categorical([1 2 3 1 2 3 1 2 3],[1 2 3],{'Air' 'Train' 'Bus'})';
G.Edges.Cost = [400 80 40 250 100 75 325 150 100]';
G.Edges.Time = [1 7 5 1.5 10 8 1.75 11 9]';
G.Edges

%
% Plot the graph for reference. Label the transportation mode on each edge, make the edge line widths proportional to the time, and the color of each edge proportional to the cost.

plot(G,'EdgeLabel',cellstr(G.Edges.Mode),'LineWidth',G.Edges.Time./min(G.Edges.Time),'EdgeCData',G.Edges.Cost)
colorbar

%
% Use the 'min' selection method and specify the value of 'PickVariable' as the 'Time' variable to find the quickest mode of transport between each set of nodes.

t = simplify(G,'min','PickVariable','Time');
plot(t,'EdgeLabel',cellstr(t.Edges.Mode))

%
% Use the 'sum' aggregation method and specify the value of 'AggregationVariables' as 'Cost' to compute how much money is made on each connection.

c = simplify(G,'sum','AggregationVariables','Cost');
plot(c,'EdgeLabel',c.Edges.Cost)

% Copyright 2012 The MathWorks, Inc.