Editing a graph.

6 次查看(过去 30 天)
Lewis Waswa
Lewis Waswa 2023-7-7
编辑: Aniketh 2023-7-8
I have a graph with about 115 nodes, all differently interconnected.
I would like to do two things: Plot the directed graph illustrating the connection between the nodes. And to return the longest path on the graph.
B=A(1:115,:);
G = digraph(B(:,1),B(:,2));
%layout(G,"force")
plot(G, "NodeFontSize",8,"MarkerSize",6);
The code provides me with a directed graph but it is too sparse. How can I standardize the distance between nodes and make it more compact? and secondly, how can I calculate the longest path?
Kindly assist.

回答(1 个)

Aniketh
Aniketh 2023-7-8
编辑:Aniketh 2023-7-8
Hi Lewis, MATLAB provides options to change the strucure through the layout function. You can check out this page for more details. You could try out 'force' or 'subspace' arguments, see what works best for your case.
In case this is still too sparse for the graph you are implementing one method I would suggest would be to manually normalize the values in the graph while plotting and unnormalize right after, should be an easy enough workaround for the illustration part.
For the second part,assuming you want the longest distance 'on' the graph(longest edge) and not the distance between any two nodes, because that could become a NP hard problem,
Here is how you could do that:
distances = distances(G);
longest_path_length = max(distances(:));
longest_path = shortestpath(G, 1, numel(G.Nodes));

类别

Help CenterFile Exchange 中查找有关 Graph and Network Algorithms 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by