Drawing a directed graph

1 view (last 30 days)
Deepa Maheshvare
Deepa Maheshvare on 12 Dec 2018
Answered: Steven Lord on 12 Dec 2018
I have the following graph,
function Digraph()
Edges = [1 2;2 3;3 4; 3 5;4 5;4 6;5 6;6 7]
t= Edges(:,1)
h = Edges(:,2)
G = graph(t,h)
NNodes = height(G.Nodes);
NNode = string(1:NNodes)';
G.Nodes.Name = cellstr(NNode);
values = 100*rand(7,1);
G.Nodes.Values = values;
tValues = values(t);
hValues = values(h);
Diff =(tValues-hValues);
G.Edges.Weight = Diff;
plot(G,'EdgeLabel',G.Edges.Weight)
end
Values are assigned to the nodes.The edge weights are assigned based on the difference in values between two nodes.I wish to draw the direction of flow from node 1 to node 7.
For instance, edge weight of the edge connecting node 1 to node 2 is positive so I wish to draw the arrowhead pointing towards 2.Edge weight of the edge connecting
node 3 to 5 is negative,so I want the arrowhead to point towards 3.
I'd like to ask for suggestions on how this can be done.

Answers (1)

Steven Lord
Steven Lord on 12 Dec 2018
I would recommend calling your function something other than Digraph. MATLAB is case sensitive, so that does not conflict with the digraph function included in MATLAB, but it's close enough that making a typo could lead to calling a different function than you expected.
Now on to your main question. Since your edges have directions the graph function is not the right tool for this job. Use digraph (lower-case d) instead. Once you've performed your computations on the digraph to compute the weights, you can reverse the polarity of the edges with negative weights by call flipedge to reverse the direction of those edges. Once you've reversed those edges, I recommend replacing the Weights of the flipped edges with their absolute values.

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!