# Graph Traverse DFS Recursive vs Iterative different sequence in the results

**URL:** <https://forum.codewithmosh.com/t/graph-traverse-dfs-recursive-vs-iterative-different-sequence-in-the-results/1356>\
**Category:** Data Structures\
**Created:** [December 20, 2020, 9:28pm UTC](https://forum.codewithmosh.com/t/graph-traverse-dfs-recursive-vs-iterative-different-sequence-in-the-results/1356 "2020-12-20T21:28:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![adbarani](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@adbarani](https://forum.codewithmosh.com/u/adbarani)\
**Post date:** [December 20, 2020, 9:28pm UTC](https://forum.codewithmosh.com/t/graph-traverse-dfs-recursive-vs-iterative-different-sequence-in-the-results/1356/1 "2020-12-20T21:28:02Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/1X/c576defb48701b11a11b4f852329689040791691.png)

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

Please advise why it’s show different sequence

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [August 3, 2022, 2:23am UTC](https://forum.codewithmosh.com/t/graph-traverse-dfs-recursive-vs-iterative-different-sequence-in-the-results/1356/2 "2022-08-03T02:23:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![zalogarciam](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/zalogarciam/32/6551_2.png) [@zalogarciam](https://forum.codewithmosh.com/u/zalogarciam)\
**Post date:** [August 9, 2023, 1:47am UTC](https://forum.codewithmosh.com/t/graph-traverse-dfs-recursive-vs-iterative-different-sequence-in-the-results/1356/3 "2023-08-09T01:47:13Z")

</div>

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…
