Iterative Dfs with Stack-Based Graph Traversal (2024)
Posted5 months agoActive5 months ago
dwf.devTechstory
calmpositive
Debate
0/100
AlgorithmGraph TraversalSoftware Development
Key topics
Algorithm
Graph Traversal
Software Development
Article discusses iterative DFS with stack-based graph traversal, providing a technical explanation and implementation details.
Snapshot generated from the HN discussion
Discussion Activity
Light discussionFirst comment
3d
Peak period
4
84-90h
Avg / period
3
Key moments
- 01Story posted
Aug 21, 2025 at 1:09 PM EDT
5 months ago
Step 01 - 02First comment
Aug 24, 2025 at 10:07 PM EDT
3d after posting
Step 02 - 03Peak activity
4 comments in 84-90h
Hottest window of the conversation
Step 03 - 04Latest activity
Aug 25, 2025 at 7:44 AM EDT
5 months ago
Step 04
Generating AI Summary...
Analyzing up to 500 comments to identify key contributors and discussion patterns
ID: 44975313Type: storyLast synced: 11/20/2025, 7:55:16 PM
Want the full context?
Jump to the original sources
Read the primary article or dive into the live Hacker News thread when you're ready.
I stumbled upon this issue when trying to convert a recursive DFS to iterative because my recursive DFS was running out of stack space.
The solution produced by this iterative version was wrong, completely different from the recursive implementation.
It’s fascinating how many primitive, basic algorithms are probably implemented incorrectly but work just well enough that no one ever cares or notices… reminds me of how so many text books have an incorrect or overflowing version of binary search.
People usually implement graph traversal first and only after that do they choose a FIFO queue for BFS or a stack for DFS as their data structure.
Extra, Extra - Read All About It: Nearly All Binary Searches and Mergesorts are Broken https://research.google/blog/extra-extra-read-all-about-it-n...
The DFS orderings where the children visitation is swapped, etc, are all still equally correct and valid. That is - a DFS algorithm that randomized the children order is still valid.
IE for example, if you change the "for nbr in graph[node]" line to "for nbr in reversed(sorted(graph[node]))", the resulting DFS ordering is still valid and correct.
If you want them in a specific ordering, you'd usually have to force them into it in the algorithm. It rarely makes sense to try to force the structure to be ordered (as they do here) for the algorithm.
This often hits people who use graphs with pointers, or multiple threads, or ...