MATLAB Answers

How to plot a weighted graph?

212 views (last 30 days)
Jack Ie
Jack Ie on 5 Apr 2016
Commented: Steven Lord on 22 Aug 2019
for example: How to plot bellow graph?
G=[1 4 0.2
2 4 0.2
3 4 0.2
4 1 0.7
4 2 0.7
4 3 0.7
4 5 0.5
4 8 0.6
5 4 0.4
5 6 0.8
5 7 0.8
6 5 0.3
7 5 0.3
8 4 0.5
8 9 0.9
8 10 0.9
8 11 0.9
9 8 0.4
10 8 0.4
11 8 0.5
11 12 0.6
12 11 0.2];
and How to plot an unweighted graph?
  3 Comments
Walter Roberson
Walter Roberson on 6 Apr 2016
"A weight is a numerical value, assigned as a label to a vertex or edge of a graph. A weighted graph is a graph whose vertices or edges have been assigned weights; more specifically, a vertex-weighted graph has weights on its vertices and an edge-weighted graph has weights on its edges."
For example, if you were creating a pipeline network, then the weight might correspond to the carrying capacity of the pipe. If you were creating a road map, then the weight might correspond to the speed limit on the segment.

Sign in to comment.

Accepted Answer

Mike Garrity
Mike Garrity on 5 Apr 2016
Something like this?
G=[1 4 0.2; ...
2 4 0.2; ...
3 4 0.2; ...
4 1 0.7; ...
4 2 0.7; ...
4 3 0.7; ...
4 5 0.5; ...
4 8 0.6; ...
5 4 0.4; ...
5 6 0.8; ...
5 7 0.8; ...
6 5 0.3; ...
7 5 0.3; ...
8 4 0.5; ...
8 9 0.9; ...
8 10 0.9; ...
8 11 0.9; ...
9 8 0.4; ...
10 8 0.4; ...
11 8 0.5; ...
11 12 0.6; ...
12 11 0.2];
g = digraph(G(:,1),G(:,2),G(:,3));
plot(g,'EdgeLabel',g.Edges.Weight)
Requires R2015b. Do 'help digraph' to get more examples. You can do things like color the edges by the weights.
  7 Comments
Steven Lord
Steven Lord on 22 Aug 2019
Change the MarkerSize property of the object returned by plot, either in the call to plot itself (like LineWidth in the first line of code in my message from April 6th 2016) or afterwards (for this you can use highlight like I did in the last line of code in my message, but specifying MarkerSize instead of LineWidth.)

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 6 Apr 2016
  3 Comments
Joel Sande
Joel Sande on 11 Apr 2016
I downloaded the folder digraph. I don t know how to use it in my code. I have the 2014a version.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!