- Related Questions & Answers
- Depth First Search
- Breadth-first search traversal in Javascript
- Python Program to Implement Depth First Search Traversal using Post Order
- Depth First Search (DFS) for a Graph
- Depth First Search or DFS for a Graph
- Depth-First Search on a Digraph in Data Structure
- Web crawling using Breadth First Search at a specified depth
- Python Program for Depth First Binary Tree Search using Recursion
- Best First Search (Informed Search)
- Breadth First Search
- Construct Binary Search Tree from Preorder Traversal in Python
- How to get the pixel depth and color depth of a screen in JavaScript?
- In-order traversal in Javascript Tree
- Breadth First Search on Matrix in C++
- Breadth First Search on Matrix in Python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

DFS visits the child vertices before visiting the sibling vertices; that is, it traverses the depth of any particular path before exploring its breadth. A stack (often the program's call stack via recursion) is generally used when implementing the algorithm.

Following is how a DFS works −

- Visit the adjacent unvisited vertex. Mark it as visited. Display it. Push it in a stack.
- If no adjacent vertex is found, pop up a vertex from the stack. (It will pop up all the vertices from the stack, which do not have adjacent vertices.)
- Repeat Rule 1 and Rule 2 until the stack is empty.

Let us look at an illustration of how DFS traversal works.

Step | Traversal | Description |
---|---|---|

1 | Initialize the stack. | |

2 | Mark S as visited and put it onto the stack. Explore any unvisited adjacent node from S. We have three nodes and we can pick any of them. For this example, we shall take the node in an alphabetical order. | |

3 | Mark A as visited and put it onto the stack. Explore any unvisited adjacent node from A. Both S and D are adjacent to A but we are concerned for unvisited nodes only. | |

4 | Visit D and mark it as visited and put onto the stack. Here, we have B and Cnodes, which are adjacent to Dand both are unvisited. However, we shall again choose in an alphabetical order. | |

5 | We choose B, mark it as visited and put onto the stack. Here B does not have any unvisited adjacent node. So, we pop B from the stack. | |

6 | We check the stack top for a return to the previous node and check if it has any unvisited nodes. Here, we find D to be on the top of the stack. | |

7 | Only unvisited adjacent node is from D is Cnow. So we visit C, mark it as visited and put it onto the stack. |

As **C** does not have any unvisited adjacent node so we keep popping the stack until we find a node that has an unvisited adjacent node. In this case, there's none and we keep popping until the stack is empty. Let us look at how we can implement this in JavaScript.

DFS(node) { // Create a Stack and add our initial node in it let s = new Stack(this.nodes.length); let explored = new Set(); s.push(node); // Mark the first node as explored explored.add(node); // We'll continue till our Stack gets empty while (!s.isEmpty()) { let t = s.pop(); // Log every element that comes out of the Stack console.log(t); // 1. In the edges object, we search for nodes this node is directly connected to. // 2. We filter out the nodes that have already been explored. // 3. Then we mark each unexplored node as explored and push it to the Stack. this.edges[t] .filter(n => !explored.has(n)) .forEach(n => { explored.add(n); s.push(n); }); } }

Well, that was easy. We really just swapped the queue out for the stack and voila, we have DFS. That is really the only difference between the 2. DFS can also be implemented using recursion. But that is best avoided in this case as a bigger graph means we need extra memory just to keep track of the call stack.

You can test this using −

let g = new Graph(); g.addNode("A"); g.addNode("B"); g.addNode("C"); g.addNode("D"); g.addNode("E"); g.addNode("F"); g.addNode("G"); g.addEdge("A", "C"); g.addEdge("A", "B"); g.addEdge("A", "D"); g.addEdge("D", "E"); g.addEdge("E", "F"); g.addEdge("B", "G"); g.DFS("A");

This will give the output.

A D E F B G C

Advertisements