Use the treeplot function
Show older comments
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
Answers (1)
Steven Lord
on 3 May 2018
0 votes
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 Comments
Stefan Zeiter
on 3 May 2018
Edited: Stefan Zeiter
on 3 May 2018
Stefan Zeiter
on 3 May 2018
Steven Lord
on 3 May 2018
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)
Stefan Zeiter
on 3 May 2018
Steven Lord
on 3 May 2018
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.
Stefan Zeiter
on 3 May 2018
Steven Lord
on 3 May 2018
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.
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!