Skip to content

Depth-First Search (DFS)

Understanding recursive graph traversal and maze/path exploration (used in the a-maze-ing project).


Table of Contents

  1. What is DFS?
  2. Graph Traversal
  3. DFS Mentality
  4. Stack Behavior
  5. Recursive Exploration
  6. Visual Example
  7. DFS Logic
  8. Visited Nodes
  9. Recursive Backtracking
  10. Iterative DFS
  11. Complexity
  12. DFS vs BFS
  13. DFS in Maze Generation
  14. Fly-in Connection
  15. Mental Model

1๏ธโƒฃ What is DFS?

DFS means:

Depth-First Search

It is an algorithm used to:

  • traverse graphs
  • explore paths deeply
  • generate mazes
  • search recursive structures

DFS explores:

go as deep as possible first

ONLY after:

backtrack

2๏ธโƒฃ Graph Traversal

Imagine this graph:

        start
       /  |  \
      A   B   C
     /         \
    D           goal

DFS might explore:

start
โ†“
A
โ†“
D
โ†“
backtrack
โ†“
B
โ†“
C
โ†“
goal

3๏ธโƒฃ DFS Mentality

DFS constantly thinks:

"Keep going deeper."

It prioritizes:

current branch exploration

before checking siblings.


4๏ธโƒฃ Stack Behavior

DFS behaves like:

Stack

A stack works like:

Last In
First Out

This is called:

LIFO


Example

ADD A
ADD B
ADD C

Stack:

[A, B, C]

Remove:

C

because:

C was added last

5๏ธโƒฃ Recursive Exploration

DFS is commonly implemented using:

recursion

Example

visit(node)
    โ†“
visit(neighbor)
    โ†“
visit(neighbor)

Recursion Flow

start
 โ†“
A
 โ†“
D
 โ†“
end branch
 โ†“
backtrack

6๏ธโƒฃ Visual Example

Graph:

        start
       /     \
      A       B
     / \
    C   D

DFS exploration:

start
โ†“
A
โ†“
C
โ†“
backtrack
โ†“
D
โ†“
backtrack
โ†“
B

7๏ธโƒฃ DFS Logic

Core Idea

Explore deeply before exploring widely.

Recursive Pseudo Code

FUNCTION dfs(node):

    mark node as visited

    FOR each neighbor:

        IF neighbor not visited:

            dfs(neighbor)

8๏ธโƒฃ Visited Nodes

DFS MUST track visited nodes.

Otherwise:

infinite recursion

may happen.


Example Loop

A -> B
โ†‘    โ†“
D <- C

Without visited tracking:

A -> B -> C -> D -> A -> ...

forever ๐Ÿ˜„


Visited Example

visited = {
    "start",
    "A",
    "B"
}

9๏ธโƒฃ Recursive Backtracking

When DFS reaches:

end of branch

it:

returns backwards

This is called:

backtracking


Visual Example

start
 โ†“
A
 โ†“
C
 โ†“
no neighbors
 โ†“
backtrack to A
 โ†“
explore D

๐Ÿ”Ÿ Iterative DFS

DFS can also be implemented without recursion.

Using:

stack

Iterative Pseudo Code

CREATE stack
CREATE visited set

PUSH start node

WHILE stack not empty:

    current = stack.pop()

    IF not visited:

        mark visited

        PUSH neighbors

1๏ธโƒฃ1๏ธโƒฃ Complexity

DFS complexity is usually:

O(V + E)

Where:

V = vertices
E = edges

1๏ธโƒฃ2๏ธโƒฃ DFS vs BFS

DFS

Explores:

deep first

BFS

Explores:

layer by layer

DFS Mentality

"Keep going deeper"

BFS Mentality

"Explore closest nodes first"

1๏ธโƒฃ3๏ธโƒฃ DFS in Maze Generation

DFS is EXTREMELY popular for:

maze generation

Because:

DFS naturally creates long corridors

Example

Your A-Maze-ing project uses:

Randomized DFS

to:

  • carve paths
  • explore cells
  • backtrack
  • generate perfect mazes

DFS Maze Flow

current cell
    โ†“
random neighbor
    โ†“
carve wall
    โ†“
continue deeper
    โ†“
no neighbors?
    โ†“
backtrack

1๏ธโƒฃ4๏ธโƒฃ Fly-in Connection

DFS is useful in Fly-in for:

  • graph traversal learning
  • recursive exploration
  • understanding pathfinding
  • understanding backtracking

BUT:

DFS does NOT guarantee cheapest path

or:

shortest weighted route

That is why:

Dijkstra fits Fly-in better

1๏ธโƒฃ5๏ธโƒฃ Mental Model

Think of DFS like:

exploring a cave

You:

  • keep moving deeper
  • follow one tunnel
  • only return when stuck

Final Mental Image

DFS always asks:

"How far can I go from here?"

NOT:

"What is the closest node?"

That is the BIG difference between:

DFS
vs
BFS