Depth First Search or DFS for a Graph
Depth First Search or DFS for a Graph
1. Visited
2. Not Visited
2. Take the top item of the stack and add it to the visited list.
3. Create a list of that vertex's adjacent nodes. Add the ones which
aren't in the visited list to the top of the stack.
Visit
the element and put it in the visited list
Next, we visit the element at the top of stack i.e. 1 and go to its
adjacent nodes. Since 0 has already been visited, we visit 2 instead.
Difference Between BFS and DFS:
Parameters BFS DFS
BFS(Breadth First
Search) uses Queue data DFS(Depth First Search) uses
Data structure for finding the Stack data structure.
Structure shortest path.
Conceptual BFS builds the tree level DFS builds the tree sub-tree by
Difference by level. sub-tree.