Why we need two "unvisited" words in DFS unified version.
When we pop node from S, then we visit it, then we add all
adjacent nodes to S, In this way, When we pop 1 from S, we add
2, 3 to S. at this time, 3 is unvisited, then when we pop 2 from S,
we will add 4, 3 to S. so there are two 3 in the S.
Based on, we need to first unvisit to avoid visit 3 twice
When we reach 3; 1,2,4 have been visited, so when we want to add
all 3 adjacent, we need second unvisited to avoid 1,2,4 to S.
1
/ \
2----3
\ /
4
DFS is an algorithm to search a hierarchical structure. But, pre-order
traversal seems to be something similar also. So, what is the difference
between the two?? DFS says:
If element found at root, return success.
If root has no descendants, return failure
Recursive DFS on left subtree: success if element found
If not, Recursive DFS on right subtree: success if element found