
I am studying Depth First Search (DFS) on undirected graphs and I am confused about discovery order.
I have attached a figure of an undirected graph with DFS discovery (d) and finishing (f) times marked on each vertex.
In my traversal, vertex d directly discovers vertex e (there is an edge between them, and e is still unvisited when DFS is at d).
However, I have seen explanations suggesting that DFS must backtrack to an ancestor (like a) before discovering e, even though d is connected to e.
My questions are:
Is my DFS traversal and timing valid?
Can d directly discover e in DFS, or does this depend on adjacency list order?
Is there any DFS rule that forbids discovering e from d if an edge exists?
I want to be sure I am applying DFS rules correctly.
Your hand-drawn image is absolutely correct. In order to understand DFS, we need to understand that we always choose depth over breadth, so even though there is a vertex between a and e, the very fact that b was the "first" child to process, b's children are to be traversed before the other children of a. This is how c comes into the picture and then c's children are prioritized over the other children of ancestors, this is how we get to d and then finally d's children are prioritized over the siblings of ancestors.
This is an easy way to understand DFS:
Of course, since the children of a are b, c and e, from the image we don't see how they are to be queued up, so if e was the "first" queued child, then it would be the first to be processed after a.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With