Consider a fixed vertex i.
In a DFS preorder, the ancestors of i must appear before i. Besides them, the only other vertices that may appear before i are complete subtrees branching out from the path from the root to i.
More precisely, let the path from the root to i be
1=p0,p1,…,pd=i.
For every vertex pr on this path, except i itself, we may choose some children of pr that are not on the path to i. If such a child is chosen, its whole subtree appears before i.
Therefore, before reaching i, we see:
- all d ancestors of i,
- plus some complete side subtrees.
So if vertex i is the i-th vertex in the DFS order, the total number of vertices taken from side subtrees must be
ki=i−1−depthi.
If ki<0, vertex i can never be placed in position i.