▲ | Iterative DFS with stack-based graph traversal (2024)(dwf.dev) | |||||||||||||
39 points by cpp_frog 6 days ago | 9 comments | ||||||||||||||
▲ | ad-astra 3 days ago | parent | next [-] | |||||||||||||
Thanks for sharing! I had come across similar kinds of issues on my annual LeetCode prep and this very clear articulation is very helpful. Props to the author for making this so easy to visualize. | ||||||||||||||
▲ | dinobones 3 days ago | parent | prev | next [-] | |||||||||||||
I’m surprised this isn’t a more common and well known issue. 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. | ||||||||||||||
| ||||||||||||||
▲ | DannyBee 3 days ago | parent | prev | next [-] | |||||||||||||
One thing to keep in mind (which the original cited article in this article gets right, but this article gets wrong) - there is no guarnateed unique ordering of children visitation for nodes with >1 child. The parts they copy from the original article talk about this correctly, the parts they didn't, don't :) 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 ... | ||||||||||||||
▲ | almostgotcaught 3 days ago | parent | prev | next [-] | |||||||||||||
This is already the standard stack based DFS?
So I don't know what all the confusion is about... | ||||||||||||||
▲ | nicoty 3 days ago | parent | prev | next [-] | |||||||||||||
I implemented an iterative, stack-based DFS iterator in JS last year for a project that I didn't end up using it on. Maybe someone else can find some use of it: https://gist.github.com/nothingnesses/5f974a43a2da5d1d8a6b9c... | ||||||||||||||
▲ | quibono 3 days ago | parent | prev [-] | |||||||||||||
So... am I misunderstanding or is it enough to swap the iteration over the neighbours of a node and the visited check?
into
| ||||||||||||||
|