TransWikia.com

How do I reference a string as the root node of a graph?

Mathematica Asked on March 15, 2021

I’ve produced the following tree plot:

enter image description here

But I want the root to be $a$, which is a string. I looked at the documentation, it says:

enter image description here

So it seems it’s very easy, we just write something like:

TreePlot[{1 -> 4, 1 -> 6, 1 -> 8, 2 -> 6, 3 -> 8, 4 -> 5, 7 -> 8}, Automatic, 1, VertexLabels -> Automatic]

Where we switch 1 by "a" but this is not working. So how do I reference a string as the root node of a graph?

One Answer

edgelist = {"a" -> "d", "a" -> "f", "a" -> "h", "b" -> "f", "c" -> "h", 
   "d" -> "e", "g" -> "h"};

TreePlot[edgelist, Automatic, "a",    VertexLabels -> Automatic] 

enter image description here

TreePlot[edgelist, Automatic, "e",    VertexLabels -> Automatic] 

enter image description here

If the vertex list of your tree is Range[8]:

edgelist2 = {1 -> 4, 1 -> 6, 1 -> 8, 2 -> 6, 3 -> 8, 4 -> 5, 7 -> 8};

TreePlot[edgelist2, Automatic, 1, VertexLabels -> Automatic]

enter image description here

you can label the vertices using vlabels:

vlabels = CharacterRange["a", "h"];

TreePlot[edgelist2, Automatic, 1, VertexLabels -> {i_ :> vlabels[[i]]}] 

enter image description here

Answered by kglr on March 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP