hey
I want to apply the treeplot function. My goal is to generate a tree like I illustrated in attached picture.
I don't know how to use the correct syntax.
I hope someone can help me.
Best

답변 (1개)

Steven Lord
Steven Lord 2018년 5월 3일

0 개 추천

Number your nodes 1 through 11. Create a vector with 11 elements. Each element in the vector should contain the number of the node that is the parent of the node with that element number (or 0 if that node has no parent.) For example if node 2 was the parent of node 6, element 6 of the vector would be 2.
If you want the nodes to be laid out more closely to how they appear in that picture, with all children of the red 1 being level with the red 2, etc. consider building a digraph and calling plot on the digraph. Specifying the 'layered' layout looks pretty close to the picture you posted, but if you want to get even closer you can explicitly specify the X and Y coordinates for your nodes.

댓글 수: 7

Stefan Zeiter
Stefan Zeiter 2018년 5월 3일
편집: Stefan Zeiter 2018년 5월 3일
I attempted following input.
treeplot([0 1 1 1 1 2 2 2 3 3 4])
But it doesn't work. The tree looks different. So I still don't understand the principle.
until second level the tree looks like I expect.
treeplot([0 1 1 1 1 2 2 2])
I solved the problem.
treeplot([0 1 1 1 1 2 2 2 6 6 10])
Like this way it works.
So you numbered the node with the red 1 as the first element of your vector.
The nodes on the next level down are the second through fifth element, and they all have parent 1. The red 2 is the second element.
The nodes on the third level down are the sixth through eighth element, and they all have parent 2. The red 3 is the sixth element.
The nodes on the fourth level down are the ninth and tenth element. The red 4 is the ninth element. The parent of those nodes is not the third element of the vector as you've stated (that would be the 3 on the second level) but the sixth element (the red 3.) So the ninth and tenth elements of your vector should be 6 not 3.
Finally, the red 5 is the eleventh element of the vector, and its parent is the ninth element (the red 4.) The 4 should be a 9.
This line of code generates a plot that is isomorphic to your figure, although it puts all the leaves on the bottom level.
treeplot([0 1 1 1 1 2 2 2 6 6 9])
As I said, if you want more control over the way the plot is laid out build an adjacency matrix and use that to create a digraph that you can plot. This will get you closer.
>> v = [0 1 1 1 1 2 2 2 6 6 9];
>> adjacency = sparse(v(2:11), 2:11, 1, 11, 11);
>> d = digraph(adjacency);
>> plot(d, 'layout', 'layered')
If you're not sure why I built the adjacency matrix the way I did, display it and look at each row. Which rows contain non-zero elements and to which nodes do those rows correspond?
full(adjacency)
Thanks for your help.
Now I would like to apply the function in an adaptive way.
That means I will solve the TSP and apply the treeplot function at the end. The number of cities is variable.
Therefore I guess first is it necessary to calculate the number of nodes from the tree. To generate the number of nodes I wrought these lines.
n = 5 %number of cities
number_of_nodes = 0;
number_of_nodes = 1:n - 1;
number_of_nodes = sum(number_of_nodes) + 1
For 5 cities there are 11 nodes necessary. 6 cities need 16 nodes. How can I now use the treeplot function in an adaptive way?
For 5 cities (11 nodes) the function would be:
treeplot([0 1 1 1 1 2 2 2 6 6 9])
For 6 cities (16 nodes) the function is:
treeplot([0 1 1 1 1 1 2 2 2 2 7 7 7 11 11 14])
Steven Lord
Steven Lord 2018년 5월 3일
If you're doing more heavy-duty graph manipulation work, I strongly recommend you switch from treeplot to working with graph or digraph objects if you are using a release that has them available. Then you don't need to worry about how to create the vector of parents, you can just call plot.
Thanks for your hint
I'm not a confident Matlab user. As a output I receive from my TSP script an array where the optimal way is stored.
For example:
wayarray = [1; 4; 2; 3; 6; 5]
I know how I can illustrate a tree when the number of cities is known. But I don't know how I can make my treeplot function or as you recommanded the graph function dependent on the number of cities.
Steven Lord
Steven Lord 2018년 5월 3일
If you open the documentation in the installation you're using and go to the Mathematics section of the MATLAB documentation, one of the subsections in that section should be Graph and Network Algorithms. That subsection page includes a number of Topics that describe how to create, manipulate, query, and visualize graph and digraph objects.
You can use the techniques from those Topics pages and the examples on the graph and digraph pages to convert your raw "city X is connected to city Y" data into one of these objects and then use the functions that allow you to ask questions (like "Is there an edge from A to B?", findedge, or "What's the shortest path from C to D?", shortestpath) on the graph or digraph object and/or the functions that allow you to manipulate (adding a new city with addnode or removing an edge between two cities with rmedge) that object.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

질문:

2018년 5월 3일

댓글:

2018년 5월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by