Group Abstract Group Abstract

Message Boards Message Boards

0
|
4.2K Views
|
3 Replies
|
2 Total Likes
View groups...
Share
Share this post:

How to best graph a tree structure

Posted 10 years ago
POSTED BY: David Gathercole
3 Replies
Posted 10 years ago

You should check that output very carefully to make certain it is doing what you want. I'm not convinced I really understand exactly what the function should do with all possible variations of your input.

What the function was trying to do was massage the input into a consistent form and then use Outer to generate all the edges from source to destination verticies.

POSTED BY: Bill Simpson

That's really great Bill! Thank you so much.

A few of these functions are a tad unfamiliar to me, so this will hopefully fuel me to get through these sorts of problems alone in future.

POSTED BY: David Gathercole
Posted 10 years ago

Is this what you are looking for?

In[1]:= tree = {1, {2}, {{3, 4}}, {{{5, 6}, {7}}}};
flat[v_] := If[ListQ[v], Flatten[v], {v}];
verts = Map[{flat[First[#]], flat[Last[#]]} &, Partition[tree, 2, 1]];
edgelist = Flatten[Map[Outer[DirectedEdge, Sequence @@ #] &, verts]]

Out[3]= {1 \[DirectedEdge] 2, 2 \[DirectedEdge] 3, 2 \[DirectedEdge] 4, 3 \[DirectedEdge] 5,
 3 \[DirectedEdge] 6, 3 \[DirectedEdge] 7, 4 \[DirectedEdge] 5, 4 \[DirectedEdge] 6, 4 \[DirectedEdge] 7}

With a little bit of concentration and time it should be possible to simplify that even more.

POSTED BY: Bill Simpson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard