Recursive version of DFS vs Dter Iterative

I am trying to understand the difference between DFS replication and DFS iteration. Does the one with the stack use an iterative or recursive approach?

For example, what will be the result of using recursive traversal of the DFS graph and iterative traversal of the DFS graph? The neighbors are repeated in alphabetical order.

Here is the schedule:

enter image description here

For a DFS bypass (one that has a stack is not sure if it is recursive or iterative), this is what I got: A, C, D, E, F. Can someone confirm what type of DFS bypass this is, and how will the other work? Thanks!

+5
source share
2 answers

As far as I understand, the recursive and iterative version differ only in the use of the stack. The recursive version uses the call stack, while the iterative version performs exactly the same steps, but uses the user-defined stack instead of the call stack. There is no difference in the sequence of steps itself (if appropriate tie-break rules are used for child nodes to ensure a uniform sequence of passage), so it is impossible to check the result to decide whether an iterative or recursive implementation has been used.

+3
source

As indicated in another answer, moving your graph using DFS will visit the vertices in the same way regardless of the actual DFS implementation using iteration or recursion. See Pseudocode in the Wikipedia article .

You have an additional requirement, which is to visit neighboring peaks in alphabetical order. This means that the stack needs to be sorted when things are clicked on it (in the iterative version), or it means that you have to overwrite adjacent vertices in sorted order (in the recursive version). Both implementations will behave the same.

Given the limitation in alphabetical order, the result A, C, D, E, F is the only possible DFS bypass of your graph.

0
source

Source: https://habr.com/ru/post/1207277/


All Articles