Filter löschen
Filter löschen

Use the treeplot function

3 Ansichten (letzte 30 Tage)
Stefan Zeiter
Stefan Zeiter am 3 Mai 2018
Kommentiert: Steven Lord am 3 Mai 2018
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

Antworten (1)

Steven Lord
Steven Lord am 3 Mai 2018
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 Kommentare
Stefan Zeiter
Stefan Zeiter am 3 Mai 2018
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 am 3 Mai 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Graph and Network Algorithms finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by