11
$\begingroup$

This question is similar to this: Nested list to graph. How to "flatten" an arbitrary expression, eg

expr = a[b[c, d[e][f], g], h]

to a list of key-value pairs representing Graph edges of the expression tree:

enter image description here

These can be extracted by applying WReach's exception-based method --> Can TreeForm be displayed “sideways”?:

Block[{TreePlot},
  t_TreePlot := Throw@Hold@t;
  Catch@MakeBoxes@TreeForm[expr]
  ][[1, 1]]

Giving:

{{"a", "0", "a[b[c, d[e][f], g], h]"} -> {"b", "1", 
   "b[c, d[e][f], g]"}, {"b", "1", "b[c, d[e][f], g]"} -> {"c", "2", 
   "c"}, {"b", "1", "b[c, d[e][f], g]"} -> {"d[e]", "3", 
   "d[e][f]"}, {"d[e]", "3", "d[e][f]"} -> {"f", "4", "f"}, {"b", "1",
    "b[c, d[e][f], g]"} -> {"g", "5", "g"}, {"a", "0", 
   "a[b[c, d[e][f], g], h]"} -> {"h", "6", "h"}}

(Why are they cast to String?) Note non-atomic sub-expressions replaced with their Head. Based on the above, and the rule: ({h_, _, _} -> {a_, _, _}) :> ToExpression@h -> ToExpression@ a gives:

{a -> b, b -> c, b -> d[e], d[e] -> f, b -> g, a -> h}

TreeForm is a wrapper around TreePlot. TreePlot[%, VertexLabeling -> True] gives:

enter image description here

Since the layout is different, TreePlot must be making use of the other components of the output of the Block above.

EDIT:

How do TreeForm and SparseAray`ExpressionToTree (see below) extract these pairs of vertices? "Proof of work" is to extract the position (in the expression) of each vertex along with the edges.

Previously, I asked how to extract these "edges" based on a more restricted example Alternatives ordering affects pattern matching in Cases?. Also tried ReplaceList but don't know how to map it consistently across all levels.

$\endgroup$
10
$\begingroup$

Although kguler posted an answer using a nice internal function that does this (almost) directly I find this kind of expression manipulation interesting in itself so I wanted to see what could be done without it.

expr = a[b[c, d[e][f], g], h];

edges =
  Reap[
    Cases[expr, h_[___, c_[___] | c_?AtomQ, ___] /; Sow[h -> c], {0, -1}] 
  ][[2, 1]];

TreePlot[edges, VertexLabeling -> True]

enter image description here

Or for the different layout:

TreePlot[edges, Automatic, Head @ expr, VertexLabeling -> True]

enter image description here


A user asked about non-unique node names. Here is a first attempt at addressing that case.

tree[expr_] :=
  Module[{e2, edges, head},
    e2 = MapIndexed[head, expr, {0, -1}, Heads -> True];
    edges = Reap[
       Cases[e2, (head[h_, x_])[___, 
          head[head[c_, {z__}][___] | c_?AtomQ, {a__}], ___] /; 
         Sow[Annotation[h, x] -> 
           Annotation[c, If[{z} === {}, {a}, {z}]]], {0, -1}]][[2, 1]];
    edges = edges /. head -> Annotation;
    TreePlot[edges, Automatic, edges[[-1, 1]], VertexLabeling -> True]
  ]

a[b[c, d[e][f], g, b, d[e][b]], h] // tree

enter image description here

This very likely has bugs that will need to be addressed as I did it in a hurry and tired, but I think it at least gives us a place to start.

$\endgroup$
  • $\begingroup$ Thanks, nice use of Sow/Reap, and inner Alternatives. One issue, how to specify Head[expr] as RootVertex? Maybe using a function of Position[expr, h] (in Sow)? $\endgroup$ – alancalvitti Jun 27 '14 at 21:09
  • $\begingroup$ @alancalvitti I'm glad this helps. Regarding "RootVertex" does this do what you want? TreePlot[%, Automatic, Head@expr, VertexLabeling -> True] $\endgroup$ – Mr.Wizard Jun 28 '14 at 1:11
  • $\begingroup$ this answer, as the others here, relies on the nodes being all different. it's not easy to see how to extend it to the case where e.g. b appears twice at different places. one would probably need some temporary node numbering? $\endgroup$ – user3240588 Sep 12 '17 at 18:14
  • $\begingroup$ @user3240588 Good question! I'll have to think about that. $\endgroup$ – Mr.Wizard Sep 12 '17 at 20:47
  • $\begingroup$ @user3240588 Please see my update, and look for cases where it fails so I can try to fix those. $\endgroup$ – Mr.Wizard Sep 12 '17 at 21:25
8
$\begingroup$

An alternative method to WReach's method is to use SparseArray`ExpressionToTree which produces the same output without string wrappers:

expr = a[b[c, d[e][f], g], h];
ett = SparseArray`ExpressionToTree[expr]
(* {{a,0,a[b[c,d[e][f],g],h]}->{b,1,b[c,d[e][f],g]},
    {b,1,b[c,d[e][f],g]}->{c,2,c},
    {b,1,b[c,d[e][f],g]}->{d[e],3,d[e][f]},
    {d[e],3,d[e][f]}->{f,4,f},
    {b,1,b[c,d[e][f],g]}->{g,5,g},
    {a,0,a[b[c,d[e][f],g],h]}->{h,6,h}} *)

edges = ett[[All,All,1]] (* thanks: @Mr.Wizard *)
(* or edges = ett /. Rule[a_, b_] :> Rule[First[a], First[b]];*)
(* {a->b,b->c,b->d[e],d[e]->f,b->g,a->h} *)

Graph[edges, VertexLabels -> Placed["Name", {Center, Center}],
             VertexSize -> .3, VertexLabelStyle -> Directive[Red, Italic, 20],
             ImagePadding -> 20, ImageSize -> 400, 
             GraphLayout -> {"LayeredEmbedding", "RootVertex" -> edges[[1,1]]}]

enter image description here

Update: You can also use GraphComputation`ExpressionGraph:

eg = GraphComputation`ExpressionGraph[expr, VertexSize -> Large, 
   VertexLabelStyle -> Directive[Red, Italic, 20]];
SetProperty[eg,  VertexLabels -> {v_ :> 
    Placed[PropertyValue[{eg, v}, VertexLabels], Center]}]

enter image description here

$\endgroup$
  • 3
    $\begingroup$ More directly: SparseArray`ExpressionToTree[expr][[All, All, 1]]. Also, thanks for showing me ExpressionToTree! (Again?) $\endgroup$ – Mr.Wizard Jun 27 '14 at 1:24
  • $\begingroup$ @kguler, +1 - btw ETT --> ReleaseHold. But I'm asking how TreeForm (and ETT) convert the expr. I edited my Q to reflect this. $\endgroup$ – alancalvitti Jun 27 '14 at 18:40
  • $\begingroup$ @alancalvitti Is my answer of no interest to you? Are you only interested in the details of the internal implementation and not an equivalent method in top-level code? $\endgroup$ – Mr.Wizard Jun 27 '14 at 19:37
  • $\begingroup$ Yes that works, thank you, I had to test on some expressions of interest. $\endgroup$ – alancalvitti Jun 27 '14 at 21:06
3
$\begingroup$

IGraph/M now includes IGExpressionTree:

<<IGraphM`
IGExpressionTree[expr]

enter image description here

GraphQ[%]
(* True *)
$\endgroup$
0
$\begingroup$

Is this what you are seeking?

expr = a[b[c, d[e][f], g], h]
boxes = ToBoxes@TreeForm[expr]
positions = Cases[boxes, LineBox[{x__}] -> x, Infinity]
nodes =
  Cases[
    boxes,
    StyleBox[x_, __] :> ToExpression@x, Infinity] /. 
      {t_Times :> First@t, Verbatim[HoldForm][x_] -> x}
Rule @@@ Extract[nodes, List /@ positions]

{a -> b, a -> h, b -> c, b -> d, b -> g, d -> f}

$\endgroup$
  • $\begingroup$ No, for example that doesn't match d[e]. But also, How to extract it directly without using TreeForm? $\endgroup$ – alancalvitti Jun 26 '14 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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