Depth-First Search (DFS) is a graph traversal algorithm that explores as far as possible along each branch before backtracking. It can be implemented using recursion or an explicit stack.
Let's consider a simple graph:
      A
    /     \
  B         C 
/   \           \
D   E          F
const graph = {
  A: ["B", "C"],
  B: ["A", "D", "E"],
  C: ["A", "F"],
  D: ["B"],
  E: ["B"],
  F: ["C"]
};
Output:
A B D E C F
O(V + E) where V is the number of vertices and E is the number of edges.
O(V) for storing visited nodes in recursion.
https://drawtocode.vercel.app/problems/depth-first-search
Loading component...
Loading component...