3
$\begingroup$

Say, I have an expression tree:

g = IGExpressionTree[ {{{1,2},{{3,4},{5,6}}},7}, VertexLabels -> "Name"]

From that tree I want to delete all the vertices that aren't the parents of any other vertices,i.e. vertices which are not source of any arrows in such tree, so I do:

VertexDelete[g,Complement[VertexList[g],First/@EdgeList[g]]]

But then instead of getting a modified tree as an output I get:

enter image description here

Can someone help me understand why that is and how to fix it?

$\endgroup$
  • $\begingroup$ Which Mathematica version? $\endgroup$ – Szabolcs May 27 at 12:57
  • $\begingroup$ @Szabolcs I'm using 11.3 $\endgroup$ – amator2357 May 27 at 12:58
  • 2
    $\begingroup$ does vc=AssociationThread[VertexList[g], GraphEmbedding[g]]; SetProperty[VertexDelete[g, _?(VertexOutDegree[g,#]==0&)], VertexCoordinates->{v_:>vc[v]}] work? $\endgroup$ – kglr May 27 at 14:05
  • $\begingroup$ @kglr It does! Thank you! $\endgroup$ – amator2357 May 27 at 14:19
3
$\begingroup$

I think this might be a bug in Mathematica's VertexDelete, or maybe the "LayeredEmbedding" GraphLayout.

To work around it, use Graph[VertexDelete[...], GraphLayout -> "SpringElectricalEmbedding"].

Alternatively, use IGExpressionTree[..., GraphRoot -> Automatic]. The problem has to do with the GraphRoot option, which here is meant to ensure that the tree is drawn with the correct root. You can ensure the correct root in another way:

g = IGExpressionTree[{{{1, 2}, {{3, 4}, {5, 6}}}, 7}, 
  VertexLabels -> "Name", GraphRoot -> Automatic, 
  GraphLayout -> {"LayeredEmbedding", "RootVertex" -> {}}]

This has a problem too, unfortunately: applying IndexGraph to the result won't render in Mathematica 11.3 and earlier as the vertex name for the root vertex was hard coded. In v12.0, this is no longer a problem, so perhaps I should change IGraph/M to use this method in M12.0+.

$\endgroup$
  • $\begingroup$ I thought it might have been a bug. Thank you for your help! $\endgroup$ – amator2357 May 27 at 13:27
  • 1
    $\begingroup$ @amator2357 I would consider this a bug, but I must note that the GraphRoot option I am using is not documented. I expect this to be pointed out when I report this to Wolfram ... Unfortunately, I was not able to find a more robust solution for setting the root than using GraphRoot for M11.3 and earlier. Now that 12.0 fixes many graph-related problems, I will change IGExpressionTree to use a different (and documented) method for that version. For M11.3, keep using the workaround I mentioned. I doubt that there will be a better way. $\endgroup$ – Szabolcs May 27 at 13:30
2
$\begingroup$
vc = AssociationThread[VertexList @ g, GraphEmbedding @ g]; 
SetProperty[VertexDelete[g, _?(VertexOutDegree[g, #] == 0 &)], 
  VertexCoordinates -> {v_ :> vc[v]}]

also works (per amator2357's confirmation in the comments).

$\endgroup$

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.