Aller au contenu principal
Interactive Algorithm Education

Visualize & Master Algorithms & Data Structures

Explore classic & modern sorting algorithms, efficient searching techniques, and interactive data structure visualizations — all with real-time step-by-step animation, comparisons, swaps, and Big-O metrics.

Graph Algorithms Visualizer

DFS — Depth-First Search

Time O(V + E) · Space O(V)
Étape 0 / 0
Speed 100ms
Step Progress 0 / 0
Visited 0
Frontier 0
Status Ready
Unvisited
Start
Active
Visited
MST / SPT
Rejected
SCC component

Queue

Step Explanation

Select an algorithm and press Play to begin.

Pseudocode
 

Depth-First Search (DFS)

Elementary (2/5) ~45 minutes Depth-first traversal LIFO stack / recursion Backtracking Preorder vs postorder visits Prereqs: Graph representation, Recursion or stacks
Quick Reference

Depth-First Search

DFS is a graph traversal algorithm that explores as far as possible along each branch before backtracking, using a LIFO stack.

Difficulty: Elementary (2/5) graph

Complexity

Best Time
O(V + E)
Average Time
O(V + E)
Worst Time
O(V + E)
Space
O(V)

When to Use

Use DFS for topological sorting, cycle detection, connected components, solving puzzles with single-path solutions, and tree traversals.

Pros

  • Low memory footprint compared to BFS on deep graphs
  • Naturally recursive formulation
  • Useful for topological ordering and cycle detection

Cons

  • Does not find shortest paths
  • Can get stuck in infinite loops on infinite graphs without visited tracking
  • Recursive implementation may overflow the stack on deep graphs

History

DFS has been studied since the 19th century as a maze-solving strategy. Its computer science formalization dates to the 1960s with depth-first search of trees by John Hopcroft and Robert Tarjan.

DFS is the explorer who walks a corridor to its dead-end, backs up to the last fork, and tries the next corridor — never leaving a path half-explored.

Instead of a queue, it uses a stack (or recursion), diving as deep as possible along one branch before backtracking. The trade-off vs BFS: DFS needs far less memory on deep graphs, but it does not find shortest paths.

How It Works

  1. Initialize: push the starting node onto the stack.
  2. Process: pop the top node; if unvisited, mark it visited.
  3. Explore: push all unvisited neighbors onto the stack.
  4. Repeat: continue until the stack is empty. The pop order defines the DFS traversal.

The recursive form is even simpler: visit(u) { mark u; for each neighbor v: if !visited[v]: visit(v) }.

Key Insight

The stack’s LIFO discipline means the most recently discovered node is always explored first — that produces “depth” instead of “breadth.”

The real power is in when a node is marked:

  • Preorder (when first encountered) gives traversal order.
  • Postorder (after all descendants finish) makes DFS the right tool for topological sorting and strongly connected components, because it records “a node before everything reachable from it.”

Worked Example

Run DFS on the visualizer’s default undirected graph (start A), visiting neighbors in order:

  • Visit A → push B, C → stack [B, C] → pop C.
  • Visit C → push E, F → stack [B, E, F] → pop F.
  • Visit F → neighbors C, E already visited → nothing → pop E.
  • Visit E → push D → stack [B, D] → pop D.
  • Visit D → neighbors B, E visited → pop B → visit B → done.

DFS order: A, C, F, E, D, B. Contrast this with BFS’s A, B, C, D, E, F — DFS chases one branch deep before ever touching the other side of the graph.

Edge Cases & Pitfalls

  • Disconnected graphs — DFS from one start only covers its component: restart from unvisited nodes to cover everything.
  • Cycles — the visited set is essential; without it, DFS loops forever.
  • Recursive stack overflow — on very deep graphs (e.g., a 10⁶-node chain), the call stack overflows. Use an explicit stack or increase the limit.
  • Memory on wide graphs — DFS stores only the current path: much leaner than BFS on wide graphs.

Comparison: DFS vs BFS

AspectDFSBFS
Data structureLIFO stack / recursionFIFO queue
Shortest path (unweighted)NoYes
Memory on wide graphsLow (path only)High (queue)
ApplicationsTopo sort, cycles, SCCsShortest paths, levels

Applications

  • Topological sorting — postorder DFS on a DAG
  • Cycle detection — a back edge to an in-progress ancestor reveals a cycle
  • Strongly connected components — Kosaraju/Tarjan both start with DFS
  • Maze / puzzle solving — finds some path (not necessarily shortest)
  • Connected components — flood-fill style labeling

Practice Trajectory

  1. Hand-trace DFS on the visualizer graph starting at D, writing the stack after each pop.
  2. Distinguish preorder from postorder and state which one topological sort uses.
  3. Explain how a “back edge” (edge to a currently in-progress node) detects a cycle.
  4. Contrast the memory profiles of BFS and DFS on a wide graph.
  5. Implement DFS iteratively with an explicit stack, then recursively, and compare the visit orders.