5
$\begingroup$

I would like to recreate the following picture in Mathematica. I know how to draw a tree with GraphLayout. But I don't know how to create the shape of nodes as below. A bit hints about where to start will be appreciated!

enter image description here

$\endgroup$
1
  • 2
    $\begingroup$ I know how to draw a tree with GraphLayout -- include that code as a starting point, and the data you want to represent in numerical form. help us help you! $\endgroup$
    – MarcoB
    Jun 5, 2020 at 15:19

2 Answers 2

6
$\begingroup$
vertexShape[n_] := Graphics[{
   EdgeForm[Black],
   FaceForm[Lighter@Gray],
   Disk[{0, 0}],
   White,
   Disk[0.5 #, 0.2] & /@ CirclePoints[n]
   }]

shapes = Thread[Range[0, 11] -> Table[
     vertexShape@RandomInteger[4],
     12]];

SeedRandom[110]
g = TreeGraph[
  RandomInteger[#] \[UndirectedEdge] # + 1 & /@ Range[0, 10],
  EdgeStyle -> Black,
  VertexSize -> 0.5,
  VertexShape -> shapes
  ]

Example

Here's an example of how you can set internal nodes to blank and set the other ones according to some other rules:

shapes = Cases[
   Thread[Range[0, Length@VertexList[g] - 1] -> VertexOutDegree[g]],
   (x_ -> 1) :> (x -> vertexShape@RandomInteger[{1, 4}])
   ];
shapes = Prepend[shapes, _ -> vertexShape[0]];
$\endgroup$
5
  • $\begingroup$ Beautiful, and lightning fast! :-) $\endgroup$
    – MarcoB
    Jun 5, 2020 at 15:21
  • $\begingroup$ @MarcoB thanks :D $\endgroup$
    – C. E.
    Jun 5, 2020 at 15:22
  • 1
    $\begingroup$ Your internal nodes with degree > 1 have inner circles. Shouldn't these be empty? $\endgroup$
    – flinty
    Jun 5, 2020 at 15:58
  • $\begingroup$ @flinty OP says he knows how to create the graph, he's just asking about how to create the vertex shapes, as far as I understand. $\endgroup$
    – C. E.
    Jun 5, 2020 at 16:01
  • $\begingroup$ @flinty added an example of how to create the shape list too now though. $\endgroup$
    – C. E.
    Jun 5, 2020 at 16:35
2
$\begingroup$
(* generate a random tree *)
edges = Table[i <-> RandomInteger[{0, i - 1}], {i, 1, 20}]; 
(* random circles appear on edges with degree 1 only *)
circles = If[# == 1, RandomInteger[{1, 4}], 0] & /@ VertexDegree[Graph[edges]];
gencircles[{x_, y_}, name_] := 
 If[circles[[name + 1]] > 0, 
    Disk[{x, y} + 0.1*#, .05] & /@ CirclePoints[circles[[name + 1]]]
   , Nothing]
vsf[{x_, y_}, name_, {w_, h_}] := {Gray, Disk[{x, y}, .2], White, gencircles[{x, y}, name]}
Graph[edges,  VertexShapeFunction -> vsf]

graph with dots

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.