Graph Traverse DFS Recursive vs Iterative different sequence in the results

Going through both videos of different approach to solve DFS I’ve notice the output sequence is different

They should be the same sequence since it’s just implementation details

Please advise why it’s show different sequence

1 Like

Graphs do not have a fixed order for traversals since there is no guarantees on ordering within the same level. The traversal only requires that everything at level n be traversed before everything at level n+1.

Therefore the reordering is acceptable. I believe Mosh mentioned it in passing in one of the videos.

Hello I noticed the same with the exercises in point 9. I had a different result.
I found out that is because the order is reading the adjacency list in the for statement. If you reverse that list before entering there… you should have it. Of course this is not recommended since you increase the complexity of the algorithm…