# Issue with displaying a tree graph after deleting a vertex

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:

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

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

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+.

• I thought it might have been a bug. Thank you for your help! – amator2357 May 27 at 13:27
• @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. – Szabolcs May 27 at 13:30
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).