Graph

Depth-First Search (DFS)

Explore a graph by going as deep as possible first, while recording parent relationships, pre/post numbering, and edge types.

DFS often feels simple at first, but it becomes harder when learners must track discovery order, backtracking, parent pointers, timestamps, and edge classification at the same time.

The key teaching flow is: start at a root → go as deep as possible → backtrack when stuck → assign pre/post numbers → classify non-tree edges using structure and time.

Category
Graph
Start Vertex
a
Core Process
Descend • Backtrack
Key Outputs
Tree • Pre/Post • Edge Types

Quick Facts

Main Idea

DFS keeps moving deeper into the graph before returning to explore alternate branches.

Discovery Time

A pre number is assigned when a vertex is first discovered.

Completion Time

A post number is assigned when DFS finishes exploring a vertex and backtracks.

Traversal Tree

The edges used to discover new vertices form the DFS tree.

Key Idea

DFS is not just about visiting vertices. It is also about recording when a vertex begins and when it finishes.

The most important mental model is: DFS has two phases for every vertex — discovery and completion. That is why both pre and post numbers matter.

Visualization

This visualization starts at a, keeps the adjacency list visible, records parent relationships as the DFS tree grows, then slows the backtracking phase so you can see exactly when post numbers are assigned. After traversal, the animation classifies non-tree edges as back, forward, or cross.

Watch actively: pause before each return step and predict which vertex gets the next post number.

How It Works

1
Start at the Root

Begin at a chosen start vertex, here a.

2
Go Deeper

Visit an unvisited neighbor, record its parent, and continue as deeply as possible.

3
Backtrack When Stuck

If a vertex has no unvisited neighbors, DFS returns to its parent.

4
Finish and Record

Assign post numbers during the return steps and complete the traversal tree.

Pre and Post Numbering

Pre Number

A pre number is assigned the moment DFS first arrives at a vertex. It marks discovery time.

Post Number

A post number is assigned only after DFS finishes exploring all descendants of that vertex. It marks completion time.

Why Post Numbers Feel Harder

Learners often follow discovery easily, but post numbers require understanding the backtracking process.

Why Both Matter

Together, pre and post numbers help explain edge classification and ancestor-descendant relationships.

In this lesson, the hardest moment is usually not going deeper — it is understanding why a vertex finishes when it does.

Tree Edges and Non-Tree Edges

Tree Edges

These are the edges used to discover new vertices during DFS. Together they form the DFS tree.

Non-Tree Edges

These connect already discovered vertices and are analyzed after the DFS structure is clear.

Why Separate Them

It is easier to understand edge classification after the traversal tree and timestamps are complete.

What the Tree Shows

The DFS tree shows how the graph was explored, not every edge that exists in the graph.

Back, Forward, and Cross Edges

Back Edge

Points from a descendant back to an ancestor in the DFS tree.

Forward Edge

Points from an ancestor to a descendant, but is not itself a tree edge.

Cross Edge

Connects vertices that are in different completed branches, where neither is an ancestor of the other.

Why Time Helps

Pre/post intervals make these edge types easier to justify instead of just memorizing names.

Why It Matters

Graph Traversal Foundation

DFS is one of the most important graph algorithms and supports many later topics.

Recursion Thinking

DFS helps learners understand how recursive exploration and return steps work together.

Structural Reasoning

It teaches that traversal creates structure, not just a visitation order.

Edge Interpretation

Pre/post numbering makes graph relationships more precise and more explainable.

Pseudocode

This pseudocode shows the core DFS process with pre and post numbering:

time = 1

DFS(G, start):
    for each vertex v in G:
        visited[v] = false
        parent[v] = null

    EXPLORE(start)

EXPLORE(v):
    visited[v] = true
    pre[v] = time
    time = time + 1

    for each neighbor u of v:
        if visited[u] == false:
            parent[u] = v
            EXPLORE(u)

    post[v] = time
    time = time + 1

The important detail is that pre is assigned on discovery, while post is assigned only after returning from deeper calls.

Common Confusions

DFS means “visit every neighbor immediately”

Not exactly. DFS follows one branch as deep as possible before returning to explore alternatives.

Post numbers are assigned when a vertex is seen

No. Post numbers are assigned only when DFS finishes that vertex and backtracks.

Every edge in the graph becomes a tree edge

No. Only discovery edges become tree edges. Other edges must be classified separately.

Forward and cross edges are the same

They are different. Forward edges go to descendants; cross edges connect separate completed branches.

Outcome

After this lesson, learners should be able to trace DFS traversal order, record parents, explain why pre and post numbers differ, and classify non-tree edges as back, forward, or cross using structure and timing.