6

So I recently implemented a non-recursive version of DFS. Turns out that I can mark the nodes "visited" as soon as they are pushed on the stack or when they are popped out. The problem which I was working on specifically stated to mark it "visited" when pushed on stack. Are both versions some kind of DFS. Or is it like one is DFS and the other is not. Any suggestions are welcomed.

What I think is that if I do the second way, it will mimic the recursive dfs. But why does the other one work?

A recursive dfs (please ignore this)

dfsRec(node)
{
    visitedArray[node]=1;

    for all neighbours of node
        if they aren't visited
            dfsRec(neighbour);
}

dfs(startNode)
{
    visitedArray;
    dfsRec(startNode);  
}
ishan3243
  • 1,870
  • 4
  • 30
  • 49

1 Answers1

3

The problem with the second way (i.e. marking the node visited when they are popped out) is that your code will loop forever whenever your graph has a cycle. Once DFS reaches that cycle, it would continue going in circles, because the nodes would not be marked visited until they are popped of the stack, so any node reachable through a cycle would be pushed again and again, until you run out of memory.

Note that the issue is not too different from the recursive implementation of DFS: recursive implementation will cause stack overflow instead of running out of memory, but the reason for it would be the same.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    I don't think that recursive dfs will ever lead to a stack-overflow due to a cycle. Thats what the "visited array" is for. So that duplicates aren't pushed on the stack. – ishan3243 Nov 03 '13 at 20:33
  • @ishan3243 If you set `visited[x]` when you leave vertex `x`, not before entering it, you wouldn't know it's visited when you come to the same spot through the cycle. – Sergey Kalinichenko Nov 03 '13 at 20:35
  • i have written a recursive dfs implementation above.....idk in what category it falls but it won't lead to any stack-overflows. – ishan3243 Nov 03 '13 at 20:46
  • 1
    @ishan3243 This is because you put `visitedArray[node]=1` at the beginning of your `dfsRec`, which corresponds to marking the node visited when it is pushed on the stack. Making the code equivalent to your second approach (i.e. when the node is marked visited when it's popped from the stack) you would need to move the `visitedArray[node]=1` assignment to the end of the function, right after the loop. – Sergey Kalinichenko Nov 03 '13 at 20:53
  • oh thanx.....so for bfs also, I have to mark it visited as soon as I put it in the queue right? – ishan3243 Nov 03 '13 at 21:00
  • 1
    @ishan3243 Correct, you want to mark the vertex visited when you put it in the queue. – Sergey Kalinichenko Nov 03 '13 at 21:02
  • Almost 8 years late but can I get an example on the issue mentioned when marking it as visited after we visit/pop it? This is also assuming we checked for visited/duplicates when adding it to the stack to visit. – CyberMew Jul 21 '21 at 21:26
  • Ignore the above, I found a good example here https://stackoverflow.com/questions/45623722/marking-node-as-visited-on-bfs-when-dequeuing – CyberMew Aug 27 '21 at 23:08