Adding edges to a TreeForm of an expressionTruncate TreeForm to show only the topTransform an expression into a graph that can be plotted with TreeGraph (not TreeForm)Processing and storing a graph with a large number of vertices s.t. k-hop neighborhoods are efficiently retrievableEdgeRenderingFunction questionUsing different colors in TreeForm?VertexLabels, EdgeLabels and the direction of the arrows on a directed graphFast way to get edge-list of graph in terms of vertex indices (not vertex names)How to work with phylogenetic trees?Problem with TreeFormDemonstrate that graphs with different colors of vertices/edges are not isomorphic

Why does the Mishnah use the terms poor person and homeowner when discussing carrying on Shabbat?

How to decline a wedding invitation from a friend I haven't seen in years?

Extreme flexible working hours: how to get to know people and activities?

sed + add word before string only if not exists

How do you say "homebrewer" in Spanish?

Why 1,2 printed by a command in $() is not interpolated?

How can I search for all contacts without email?

Fermat's statement about the ancients: How serious was he?

Is an entry level DSLR going to shoot nice portrait pictures?

US doctor working in Tripoli wants me to open online account

Non-aqueous eyes?

Who enforces MPAA rating adherence?

Are there any important biographies of nobodies?

How to hide an urban landmark?

Heap allocation on microcontroller

Why can I traceroute to this IP address, but not ping?

Who won a Game of Bar Dice?

New pedal fell off maybe 50 miles after installation. Should I replace the entire crank, just the arm, or repair the thread?

Active low-pass filters --- good to what frequencies?

Electricity free spaceship

How come the nude protesters were not arrested?

How did old MS-DOS games utilize various graphic cards?

Should I ask for an extra raise?

I have a problematic assistant manager, but I can't fire him



Adding edges to a TreeForm of an expression


Truncate TreeForm to show only the topTransform an expression into a graph that can be plotted with TreeGraph (not TreeForm)Processing and storing a graph with a large number of vertices s.t. k-hop neighborhoods are efficiently retrievableEdgeRenderingFunction questionUsing different colors in TreeForm?VertexLabels, EdgeLabels and the direction of the arrows on a directed graphFast way to get edge-list of graph in terms of vertex indices (not vertex names)How to work with phylogenetic trees?Problem with TreeFormDemonstrate that graphs with different colors of vertices/edges are not isomorphic













4












$begingroup$


Say I have an expression 1,2,3,4. It's TreeForm looks as follows:



1,2,3,4 //TreeForm



enter image description here



Now, on each level of that tree I would like to draw an edge where there isn't one, i.e. between 1 and List (first level), between List and 4 (second level) and between 2 and 3 (third level). Is it possible to do it in an efficient way? Can I somehow control the direction of all of these edges (including already existing ones)? I tried to use IGExpressionTree from the IGraph package together with EdgeAdd but it didn't work. Any suggestions?










share|improve this question











$endgroup$







  • 1




    $begingroup$
    maybe EdgeAdd[GraphComputation`ExpressionGraph[1, 2, 3, 4 ], 2->3,4->7,5->6]?
    $endgroup$
    – kglr
    May 23 at 11:50











  • $begingroup$
    @kglr Thanks! Does that work, I've tried EdgeAdd[IGExpressionTree[1, 2, 3, 4, VertexLabels->"Subexpression"],4[UndirectedEdge]6], but that didn't work, I wonder why..
    $endgroup$
    – amator2357
    May 23 at 11:55






  • 1




    $begingroup$
    What if you have more than two nodes at the same level in the tree?
    $endgroup$
    – Szabolcs
    May 23 at 12:02






  • 1




    $begingroup$
    @amator2357 IGExpressionTree does not use the same vertex names as GraphComputation`ExpressionGraph
    $endgroup$
    – Szabolcs
    May 23 at 12:07











  • $begingroup$
    @Szabolcs it'll never happen for my case
    $endgroup$
    – amator2357
    May 23 at 12:07















4












$begingroup$


Say I have an expression 1,2,3,4. It's TreeForm looks as follows:



1,2,3,4 //TreeForm



enter image description here



Now, on each level of that tree I would like to draw an edge where there isn't one, i.e. between 1 and List (first level), between List and 4 (second level) and between 2 and 3 (third level). Is it possible to do it in an efficient way? Can I somehow control the direction of all of these edges (including already existing ones)? I tried to use IGExpressionTree from the IGraph package together with EdgeAdd but it didn't work. Any suggestions?










share|improve this question











$endgroup$







  • 1




    $begingroup$
    maybe EdgeAdd[GraphComputation`ExpressionGraph[1, 2, 3, 4 ], 2->3,4->7,5->6]?
    $endgroup$
    – kglr
    May 23 at 11:50











  • $begingroup$
    @kglr Thanks! Does that work, I've tried EdgeAdd[IGExpressionTree[1, 2, 3, 4, VertexLabels->"Subexpression"],4[UndirectedEdge]6], but that didn't work, I wonder why..
    $endgroup$
    – amator2357
    May 23 at 11:55






  • 1




    $begingroup$
    What if you have more than two nodes at the same level in the tree?
    $endgroup$
    – Szabolcs
    May 23 at 12:02






  • 1




    $begingroup$
    @amator2357 IGExpressionTree does not use the same vertex names as GraphComputation`ExpressionGraph
    $endgroup$
    – Szabolcs
    May 23 at 12:07











  • $begingroup$
    @Szabolcs it'll never happen for my case
    $endgroup$
    – amator2357
    May 23 at 12:07













4












4








4





$begingroup$


Say I have an expression 1,2,3,4. It's TreeForm looks as follows:



1,2,3,4 //TreeForm



enter image description here



Now, on each level of that tree I would like to draw an edge where there isn't one, i.e. between 1 and List (first level), between List and 4 (second level) and between 2 and 3 (third level). Is it possible to do it in an efficient way? Can I somehow control the direction of all of these edges (including already existing ones)? I tried to use IGExpressionTree from the IGraph package together with EdgeAdd but it didn't work. Any suggestions?










share|improve this question











$endgroup$




Say I have an expression 1,2,3,4. It's TreeForm looks as follows:



1,2,3,4 //TreeForm



enter image description here



Now, on each level of that tree I would like to draw an edge where there isn't one, i.e. between 1 and List (first level), between List and 4 (second level) and between 2 and 3 (third level). Is it possible to do it in an efficient way? Can I somehow control the direction of all of these edges (including already existing ones)? I tried to use IGExpressionTree from the IGraph package together with EdgeAdd but it didn't work. Any suggestions?







graphs-and-networks trees igraphm






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 23 at 13:30









m_goldberg

90.5k873203




90.5k873203










asked May 23 at 11:12









amator2357amator2357

74310




74310







  • 1




    $begingroup$
    maybe EdgeAdd[GraphComputation`ExpressionGraph[1, 2, 3, 4 ], 2->3,4->7,5->6]?
    $endgroup$
    – kglr
    May 23 at 11:50











  • $begingroup$
    @kglr Thanks! Does that work, I've tried EdgeAdd[IGExpressionTree[1, 2, 3, 4, VertexLabels->"Subexpression"],4[UndirectedEdge]6], but that didn't work, I wonder why..
    $endgroup$
    – amator2357
    May 23 at 11:55






  • 1




    $begingroup$
    What if you have more than two nodes at the same level in the tree?
    $endgroup$
    – Szabolcs
    May 23 at 12:02






  • 1




    $begingroup$
    @amator2357 IGExpressionTree does not use the same vertex names as GraphComputation`ExpressionGraph
    $endgroup$
    – Szabolcs
    May 23 at 12:07











  • $begingroup$
    @Szabolcs it'll never happen for my case
    $endgroup$
    – amator2357
    May 23 at 12:07












  • 1




    $begingroup$
    maybe EdgeAdd[GraphComputation`ExpressionGraph[1, 2, 3, 4 ], 2->3,4->7,5->6]?
    $endgroup$
    – kglr
    May 23 at 11:50











  • $begingroup$
    @kglr Thanks! Does that work, I've tried EdgeAdd[IGExpressionTree[1, 2, 3, 4, VertexLabels->"Subexpression"],4[UndirectedEdge]6], but that didn't work, I wonder why..
    $endgroup$
    – amator2357
    May 23 at 11:55






  • 1




    $begingroup$
    What if you have more than two nodes at the same level in the tree?
    $endgroup$
    – Szabolcs
    May 23 at 12:02






  • 1




    $begingroup$
    @amator2357 IGExpressionTree does not use the same vertex names as GraphComputation`ExpressionGraph
    $endgroup$
    – Szabolcs
    May 23 at 12:07











  • $begingroup$
    @Szabolcs it'll never happen for my case
    $endgroup$
    – amator2357
    May 23 at 12:07







1




1




$begingroup$
maybe EdgeAdd[GraphComputation`ExpressionGraph[1, 2, 3, 4 ], 2->3,4->7,5->6]?
$endgroup$
– kglr
May 23 at 11:50





$begingroup$
maybe EdgeAdd[GraphComputation`ExpressionGraph[1, 2, 3, 4 ], 2->3,4->7,5->6]?
$endgroup$
– kglr
May 23 at 11:50













$begingroup$
@kglr Thanks! Does that work, I've tried EdgeAdd[IGExpressionTree[1, 2, 3, 4, VertexLabels->"Subexpression"],4[UndirectedEdge]6], but that didn't work, I wonder why..
$endgroup$
– amator2357
May 23 at 11:55




$begingroup$
@kglr Thanks! Does that work, I've tried EdgeAdd[IGExpressionTree[1, 2, 3, 4, VertexLabels->"Subexpression"],4[UndirectedEdge]6], but that didn't work, I wonder why..
$endgroup$
– amator2357
May 23 at 11:55




1




1




$begingroup$
What if you have more than two nodes at the same level in the tree?
$endgroup$
– Szabolcs
May 23 at 12:02




$begingroup$
What if you have more than two nodes at the same level in the tree?
$endgroup$
– Szabolcs
May 23 at 12:02




1




1




$begingroup$
@amator2357 IGExpressionTree does not use the same vertex names as GraphComputation`ExpressionGraph
$endgroup$
– Szabolcs
May 23 at 12:07





$begingroup$
@amator2357 IGExpressionTree does not use the same vertex names as GraphComputation`ExpressionGraph
$endgroup$
– Szabolcs
May 23 at 12:07













$begingroup$
@Szabolcs it'll never happen for my case
$endgroup$
– amator2357
May 23 at 12:07




$begingroup$
@Szabolcs it'll never happen for my case
$endgroup$
– amator2357
May 23 at 12:07










2 Answers
2






active

oldest

votes


















4












$begingroup$

g0 = GraphComputation`ExpressionGraph[1, 2, 3, 4 , ImageSize -> 200];
newedges = UndirectedEdge @@@ GatherBy[Rest@VertexList@g0, GraphDistance[g0, 1, #] &]



2 [UndirectedEdge] 3, 4 [UndirectedEdge] 7, 5 [UndirectedEdge] 6




g1 = EdgeAdd[g0, newedges];
Row[g0, g1, Spacer[10]]


enter image description here



Alternatively, you can use newedges to post-process the TreeForm of the input expression to add the new lines:



tf1 = TreeForm[1, 2, 3, 4, DirectedEdges -> True, ImageSize -> Medium]; 
tf2 = RawBoxes[ToBoxes[tf1] /. l : (_ArrowBox | _LineBox) :>
l, Dashing @ Small, LineBox @ # ] &[List @@@ newedges];
Row @ tf1, tf2


enter image description here



If there are more than two nodes at the same level in the tree:



g0 = GraphComputation`ExpressionGraph[1, 2, 3, 4, 5 , ImageSize -> 200];
newedges = Join @@ Map[UndirectedEdge @@@ # &,
Subsets[#, 2] & /@ GatherBy[Rest@VertexList@g0, GraphDistance[g0, 1, #] &]]



2 [UndirectedEdge] 3, 4 [UndirectedEdge] 7,
4 [UndirectedEdge] 8, 7 [UndirectedEdge] 8, 5 [UndirectedEdge] 6




 g1 = SetProperty[EdgeAdd[g0, newedges],
EdgeShapeFunction -> Alternatives @@ newedges :> "CurvedArc"]
Row[g0, g1, Spacer[10]]


enter image description here






share|improve this answer











$endgroup$




















    5












    $begingroup$

    With IGraph/M,



    g = IGExpressionTree[1, 2, 3, 4]


    enter image description here



    Notice that the vertices created by this function are lists (encoding subexpression positions), and their length is the same if they are on the same tree level.



    IGExpressionTree[1, 2, 3, 4, VertexLabels -> "Name"]


    enter image description here



    Thus we can easily create the additional edges:



    pathEdges[list_] := DirectedEdge @@@ Partition[list, 2, 1]

    newEdges = Flatten[pathEdges /@ GatherBy[VertexList[g], Length]]
    (* 1 [DirectedEdge] 2, 2, 1, 1 [DirectedEdge] 2, 1, 2, 2, 1 [DirectedEdge] 2, 2 *)

    EdgeAdd[g, newEdges]


    enter image description here



    If you never have more than two nodes at a level, then simply use



    EdgeAdd[
    g,
    DirectedEdge @@@ GatherBy[Most@VertexList[g], Length]
    ]


    Both of these rely on the order in which vertices are returned by IGExpressionTree (which is sorted) as well as on GatherBy not changing this order.






    share|improve this answer











    $endgroup$













      Your Answer








      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "387"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f198930%2fadding-edges-to-a-treeform-of-an-expression%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      4












      $begingroup$

      g0 = GraphComputation`ExpressionGraph[1, 2, 3, 4 , ImageSize -> 200];
      newedges = UndirectedEdge @@@ GatherBy[Rest@VertexList@g0, GraphDistance[g0, 1, #] &]



      2 [UndirectedEdge] 3, 4 [UndirectedEdge] 7, 5 [UndirectedEdge] 6




      g1 = EdgeAdd[g0, newedges];
      Row[g0, g1, Spacer[10]]


      enter image description here



      Alternatively, you can use newedges to post-process the TreeForm of the input expression to add the new lines:



      tf1 = TreeForm[1, 2, 3, 4, DirectedEdges -> True, ImageSize -> Medium]; 
      tf2 = RawBoxes[ToBoxes[tf1] /. l : (_ArrowBox | _LineBox) :>
      l, Dashing @ Small, LineBox @ # ] &[List @@@ newedges];
      Row @ tf1, tf2


      enter image description here



      If there are more than two nodes at the same level in the tree:



      g0 = GraphComputation`ExpressionGraph[1, 2, 3, 4, 5 , ImageSize -> 200];
      newedges = Join @@ Map[UndirectedEdge @@@ # &,
      Subsets[#, 2] & /@ GatherBy[Rest@VertexList@g0, GraphDistance[g0, 1, #] &]]



      2 [UndirectedEdge] 3, 4 [UndirectedEdge] 7,
      4 [UndirectedEdge] 8, 7 [UndirectedEdge] 8, 5 [UndirectedEdge] 6




       g1 = SetProperty[EdgeAdd[g0, newedges],
      EdgeShapeFunction -> Alternatives @@ newedges :> "CurvedArc"]
      Row[g0, g1, Spacer[10]]


      enter image description here






      share|improve this answer











      $endgroup$

















        4












        $begingroup$

        g0 = GraphComputation`ExpressionGraph[1, 2, 3, 4 , ImageSize -> 200];
        newedges = UndirectedEdge @@@ GatherBy[Rest@VertexList@g0, GraphDistance[g0, 1, #] &]



        2 [UndirectedEdge] 3, 4 [UndirectedEdge] 7, 5 [UndirectedEdge] 6




        g1 = EdgeAdd[g0, newedges];
        Row[g0, g1, Spacer[10]]


        enter image description here



        Alternatively, you can use newedges to post-process the TreeForm of the input expression to add the new lines:



        tf1 = TreeForm[1, 2, 3, 4, DirectedEdges -> True, ImageSize -> Medium]; 
        tf2 = RawBoxes[ToBoxes[tf1] /. l : (_ArrowBox | _LineBox) :>
        l, Dashing @ Small, LineBox @ # ] &[List @@@ newedges];
        Row @ tf1, tf2


        enter image description here



        If there are more than two nodes at the same level in the tree:



        g0 = GraphComputation`ExpressionGraph[1, 2, 3, 4, 5 , ImageSize -> 200];
        newedges = Join @@ Map[UndirectedEdge @@@ # &,
        Subsets[#, 2] & /@ GatherBy[Rest@VertexList@g0, GraphDistance[g0, 1, #] &]]



        2 [UndirectedEdge] 3, 4 [UndirectedEdge] 7,
        4 [UndirectedEdge] 8, 7 [UndirectedEdge] 8, 5 [UndirectedEdge] 6




         g1 = SetProperty[EdgeAdd[g0, newedges],
        EdgeShapeFunction -> Alternatives @@ newedges :> "CurvedArc"]
        Row[g0, g1, Spacer[10]]


        enter image description here






        share|improve this answer











        $endgroup$















          4












          4








          4





          $begingroup$

          g0 = GraphComputation`ExpressionGraph[1, 2, 3, 4 , ImageSize -> 200];
          newedges = UndirectedEdge @@@ GatherBy[Rest@VertexList@g0, GraphDistance[g0, 1, #] &]



          2 [UndirectedEdge] 3, 4 [UndirectedEdge] 7, 5 [UndirectedEdge] 6




          g1 = EdgeAdd[g0, newedges];
          Row[g0, g1, Spacer[10]]


          enter image description here



          Alternatively, you can use newedges to post-process the TreeForm of the input expression to add the new lines:



          tf1 = TreeForm[1, 2, 3, 4, DirectedEdges -> True, ImageSize -> Medium]; 
          tf2 = RawBoxes[ToBoxes[tf1] /. l : (_ArrowBox | _LineBox) :>
          l, Dashing @ Small, LineBox @ # ] &[List @@@ newedges];
          Row @ tf1, tf2


          enter image description here



          If there are more than two nodes at the same level in the tree:



          g0 = GraphComputation`ExpressionGraph[1, 2, 3, 4, 5 , ImageSize -> 200];
          newedges = Join @@ Map[UndirectedEdge @@@ # &,
          Subsets[#, 2] & /@ GatherBy[Rest@VertexList@g0, GraphDistance[g0, 1, #] &]]



          2 [UndirectedEdge] 3, 4 [UndirectedEdge] 7,
          4 [UndirectedEdge] 8, 7 [UndirectedEdge] 8, 5 [UndirectedEdge] 6




           g1 = SetProperty[EdgeAdd[g0, newedges],
          EdgeShapeFunction -> Alternatives @@ newedges :> "CurvedArc"]
          Row[g0, g1, Spacer[10]]


          enter image description here






          share|improve this answer











          $endgroup$



          g0 = GraphComputation`ExpressionGraph[1, 2, 3, 4 , ImageSize -> 200];
          newedges = UndirectedEdge @@@ GatherBy[Rest@VertexList@g0, GraphDistance[g0, 1, #] &]



          2 [UndirectedEdge] 3, 4 [UndirectedEdge] 7, 5 [UndirectedEdge] 6




          g1 = EdgeAdd[g0, newedges];
          Row[g0, g1, Spacer[10]]


          enter image description here



          Alternatively, you can use newedges to post-process the TreeForm of the input expression to add the new lines:



          tf1 = TreeForm[1, 2, 3, 4, DirectedEdges -> True, ImageSize -> Medium]; 
          tf2 = RawBoxes[ToBoxes[tf1] /. l : (_ArrowBox | _LineBox) :>
          l, Dashing @ Small, LineBox @ # ] &[List @@@ newedges];
          Row @ tf1, tf2


          enter image description here



          If there are more than two nodes at the same level in the tree:



          g0 = GraphComputation`ExpressionGraph[1, 2, 3, 4, 5 , ImageSize -> 200];
          newedges = Join @@ Map[UndirectedEdge @@@ # &,
          Subsets[#, 2] & /@ GatherBy[Rest@VertexList@g0, GraphDistance[g0, 1, #] &]]



          2 [UndirectedEdge] 3, 4 [UndirectedEdge] 7,
          4 [UndirectedEdge] 8, 7 [UndirectedEdge] 8, 5 [UndirectedEdge] 6




           g1 = SetProperty[EdgeAdd[g0, newedges],
          EdgeShapeFunction -> Alternatives @@ newedges :> "CurvedArc"]
          Row[g0, g1, Spacer[10]]


          enter image description here







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 24 at 5:32

























          answered May 23 at 12:02









          kglrkglr

          196k10220442




          196k10220442





















              5












              $begingroup$

              With IGraph/M,



              g = IGExpressionTree[1, 2, 3, 4]


              enter image description here



              Notice that the vertices created by this function are lists (encoding subexpression positions), and their length is the same if they are on the same tree level.



              IGExpressionTree[1, 2, 3, 4, VertexLabels -> "Name"]


              enter image description here



              Thus we can easily create the additional edges:



              pathEdges[list_] := DirectedEdge @@@ Partition[list, 2, 1]

              newEdges = Flatten[pathEdges /@ GatherBy[VertexList[g], Length]]
              (* 1 [DirectedEdge] 2, 2, 1, 1 [DirectedEdge] 2, 1, 2, 2, 1 [DirectedEdge] 2, 2 *)

              EdgeAdd[g, newEdges]


              enter image description here



              If you never have more than two nodes at a level, then simply use



              EdgeAdd[
              g,
              DirectedEdge @@@ GatherBy[Most@VertexList[g], Length]
              ]


              Both of these rely on the order in which vertices are returned by IGExpressionTree (which is sorted) as well as on GatherBy not changing this order.






              share|improve this answer











              $endgroup$

















                5












                $begingroup$

                With IGraph/M,



                g = IGExpressionTree[1, 2, 3, 4]


                enter image description here



                Notice that the vertices created by this function are lists (encoding subexpression positions), and their length is the same if they are on the same tree level.



                IGExpressionTree[1, 2, 3, 4, VertexLabels -> "Name"]


                enter image description here



                Thus we can easily create the additional edges:



                pathEdges[list_] := DirectedEdge @@@ Partition[list, 2, 1]

                newEdges = Flatten[pathEdges /@ GatherBy[VertexList[g], Length]]
                (* 1 [DirectedEdge] 2, 2, 1, 1 [DirectedEdge] 2, 1, 2, 2, 1 [DirectedEdge] 2, 2 *)

                EdgeAdd[g, newEdges]


                enter image description here



                If you never have more than two nodes at a level, then simply use



                EdgeAdd[
                g,
                DirectedEdge @@@ GatherBy[Most@VertexList[g], Length]
                ]


                Both of these rely on the order in which vertices are returned by IGExpressionTree (which is sorted) as well as on GatherBy not changing this order.






                share|improve this answer











                $endgroup$















                  5












                  5








                  5





                  $begingroup$

                  With IGraph/M,



                  g = IGExpressionTree[1, 2, 3, 4]


                  enter image description here



                  Notice that the vertices created by this function are lists (encoding subexpression positions), and their length is the same if they are on the same tree level.



                  IGExpressionTree[1, 2, 3, 4, VertexLabels -> "Name"]


                  enter image description here



                  Thus we can easily create the additional edges:



                  pathEdges[list_] := DirectedEdge @@@ Partition[list, 2, 1]

                  newEdges = Flatten[pathEdges /@ GatherBy[VertexList[g], Length]]
                  (* 1 [DirectedEdge] 2, 2, 1, 1 [DirectedEdge] 2, 1, 2, 2, 1 [DirectedEdge] 2, 2 *)

                  EdgeAdd[g, newEdges]


                  enter image description here



                  If you never have more than two nodes at a level, then simply use



                  EdgeAdd[
                  g,
                  DirectedEdge @@@ GatherBy[Most@VertexList[g], Length]
                  ]


                  Both of these rely on the order in which vertices are returned by IGExpressionTree (which is sorted) as well as on GatherBy not changing this order.






                  share|improve this answer











                  $endgroup$



                  With IGraph/M,



                  g = IGExpressionTree[1, 2, 3, 4]


                  enter image description here



                  Notice that the vertices created by this function are lists (encoding subexpression positions), and their length is the same if they are on the same tree level.



                  IGExpressionTree[1, 2, 3, 4, VertexLabels -> "Name"]


                  enter image description here



                  Thus we can easily create the additional edges:



                  pathEdges[list_] := DirectedEdge @@@ Partition[list, 2, 1]

                  newEdges = Flatten[pathEdges /@ GatherBy[VertexList[g], Length]]
                  (* 1 [DirectedEdge] 2, 2, 1, 1 [DirectedEdge] 2, 1, 2, 2, 1 [DirectedEdge] 2, 2 *)

                  EdgeAdd[g, newEdges]


                  enter image description here



                  If you never have more than two nodes at a level, then simply use



                  EdgeAdd[
                  g,
                  DirectedEdge @@@ GatherBy[Most@VertexList[g], Length]
                  ]


                  Both of these rely on the order in which vertices are returned by IGExpressionTree (which is sorted) as well as on GatherBy not changing this order.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited May 23 at 13:16

























                  answered May 23 at 12:06









                  SzabolcsSzabolcs

                  168k17460974




                  168k17460974



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Mathematica Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      Use MathJax to format equations. MathJax reference.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f198930%2fadding-edges-to-a-treeform-of-an-expression%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Wikipedia:Vital articles Мазмуну Biography - Өмүр баян Philosophy and psychology - Философия жана психология Religion - Дин Social sciences - Коомдук илимдер Language and literature - Тил жана адабият Science - Илим Technology - Технология Arts and recreation - Искусство жана эс алуу History and geography - Тарых жана география Навигация менюсу

                      Bruxelas-Capital Índice Historia | Composición | Situación lingüística | Clima | Cidades irmandadas | Notas | Véxase tamén | Menú de navegacióneO uso das linguas en Bruxelas e a situación do neerlandés"Rexión de Bruxelas Capital"o orixinalSitio da rexiónPáxina de Bruselas no sitio da Oficina de Promoción Turística de Valonia e BruxelasMapa Interactivo da Rexión de Bruxelas-CapitaleeWorldCat332144929079854441105155190212ID28008674080552-90000 0001 0666 3698n94104302ID540940339365017018237

                      What should I write in an apology letter, since I have decided not to join a company after accepting an offer letterShould I keep looking after accepting a job offer?What should I do when I've been verbally told I would get an offer letter, but still haven't gotten one after 4 weeks?Do I accept an offer from a company that I am not likely to join?New job hasn't confirmed starting date and I want to give current employer as much notice as possibleHow should I address my manager in my resignation letter?HR delayed background verification, now jobless as resignedNo email communication after accepting a formal written offer. How should I phrase the call?What should I do if after receiving a verbal offer letter I am informed that my written job offer is put on hold due to some internal issues?Should I inform the current employer that I am about to resign within 1-2 weeks since I have signed the offer letter and waiting for visa?What company will do, if I send their offer letter to another company