Depth-First Search


Depth-first search (DFS) is a graph traversal algorithm that explores as far as possible along each branch before backtracking. Imagine traversing a maze – you’d follow one path until you hit a dead end, then retrace your steps to the last intersection and try a different path. DFS works similarly.

Here’s a breakdown of the algorithm and its implementation in JavaScript:

Algorithm:

  1. Start at a designated root node.
  2. Mark the current node as visited.
  3. Iterate over the unvisited neighbors of the current node.
  4. For each unvisited neighbor, recursively call the DFS function.
  5. If all neighbors of the current node have been visited, backtrack.

JavaScript Implementation:

function dfs(graph, startNode, visited = new Set()) {
  console.log(`Visiting node: ${startNode}`);
  visited.add(startNode);

  for (const neighbor of graph[startNode]) {
    if (!visited.has(neighbor)) {
      dfs(graph, neighbor, visited);
    }
  }
}


// Example graph represented as an adjacency list
const graph = {
  'A': ['B', 'C'],
  'B': ['D', 'E'],
  'C': ['F'],
  'D': [],
  'E': ['F'],
  'F': []
};

dfs(graph, 'A');

Explanation:

Output of the example:

Visiting node: A
Visiting node: B
Visiting node: D
Visiting node: E
Visiting node: F
Visiting node: C
Visiting node: F

Use Cases:

DFS has various applications, including:

Time and Space Complexity:

This explanation and code example provides a foundation for understanding and implementing depth-first search in your projects. Remember to adapt the implementation based on your specific graph representation and requirements.