% demo example for the use of the Graph interface for graphViz4Matlab

% Generate the incidence matrix for a transportation
% network with m = 9 nodes and n = 11 edges
A = ...
[ -1 -1  0  0  0  0  0  0  0  0  0; ...
   0  0 -1 -1  0  0  0  0  0  0  0; ...
   0  0  0  0 -1 -1  0  0  0  0  0; ...
   1  0  1  0  1  0 -1 -1 -1  0  0; ...
   0  1  0  1  0  1  1  0  0 -1 -1; ...
   0  0  0  0  0  0  0  1  0  0  0; ...
   0  0  0  0  0  0  0  0  1  0  0; ...
   0  0  0  0  0  0  0  0  0  1  0; ...
   0  0  0  0  0  0  0  0  0  0  1];

% Draw the graph
h = Graph(A);

% Set the node labels for the production sites,
% storage sites and points of sale
nodelabels = {'P1','P2','P3','Z1','Z2','V1','V2','V3','V4'};

% Redraw the graph
h = Graph(A,h,nodelabels);

% Set additional edge labels
edgelabels = [0.8; 2.0; 2.5; 1.0; 1.2; 2.0; 1.0; 1.0; 1.0; 1.0; 1.0];
h = Graph(A,h,nodelabels,edgelabels);

% Set node positions XY manually
% Here: rotate graph about 135 degree
alpha = 135 * pi / 180;
R = [cos(alpha), -sin(alpha); sin(alpha), cos(alpha)];
XY = h.getNodePositions();
XY = XY - 0.5;            % shift to origin
XY = (R * XY')';          % rotate
XY = XY * 0.8;            % scale on 80%
XY(:,1) = XY(:,1) + 0.5;  % shift back
XY(:,2) = XY(:,2) + 0.4;  % shift back
h.setNodePositions(XY);

% Redraw the graph with tree layout
h = Graph(A,h,nodelabels,[],'-layout',Treelayout);

% Redraw the graph with circle layout
h = Graph(A,h,nodelabels,[],'-layout',Circlelayout);

