Why aren't weight values incorporated into graph and digraph?

4 ビュー (過去 30 日間)
Nathan Garcia
Nathan Garcia 2022 年 2 月 23 日
コメント済み: Nathan Garcia 2022 年 2 月 23 日
I would like to create a network graph using the graph function or the digraph function. I would like edges lengths in the network to reflect values that I have in a vector. I tried the examples provided in the video at the following page.
It describes how to add weight data to the graph and digraph functons. The video gives the impression that the distance of the edges can be changed by changing the "weight" value. However, the distances do not reflect the weight values in the video example. Also when I change the values dramatically there is no change in the distance of edges. Is there a fix to this or another graphing function that I can use?


Steven Lord
Steven Lord 2022 年 2 月 23 日
You can have the edge weights influence the layout of the graph or digraph but it is possible to specify edge weights such that drawing the network with edges of exactly those lengths is impossible. One such example that violates the triangle inequality is:
EndNodes = [1 2; 1 3; 2 3];
Weights = [5; 5; 20]; %
E = table(EndNodes, Weights);
g = graph(E);
See the example "Graph Layout Based on Edge Weights" on the documentation page for the layout function for an example. In the case of this simple a graph the layout using 'force' with or without a WeightEffect looks the same, but for a more complicated graph (like a randomly weighted buckyball) it may not.
B = graph(bucky);
B.Edges.Weight = randi(20, numedges(B), 1);
plot(B); % No weight effect
h = plot(B);
layout(h, 'force', 'WeightEffect', 'direct') % Higher weighted edges tend to be longer
plot(B, 'layout', 'force', 'WeightEffect', 'inverse') % higher weight, shorter edge
  1 件のコメント
Nathan Garcia
Nathan Garcia 2022 年 2 月 23 日
Thank you! that was helpful!


その他の回答 (0 件)


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!

Translated by