Intuition The ONE core idea
DFS is just this: walk into a graph as deep as one path goes, drop a marker in every room you enter, and step back to the last fork when you dead-end. Everything else on the parent page — the visited array, the stack, the O ( V + E ) count — is bookkeeping that makes this one walk fast and loop-free.
Before you can read the parent DFS note, you must own every symbol it uses without apology. This page builds them from nothing , in an order where each idea rests only on the ones above it. Nothing here assumes you have seen a graph before line one.
Start with a picture, not a definition.
Look at the figure. The circles are rooms — in graph language a vertex (plural: vertices). The lines connecting them are tunnels — an edge . That is the whole object.
A graph is a set of vertices (dots, drawn as circles) joined by edges (lines). Formally we write G = ( V , E ) where V is the collection of vertices and E is the collection of edges.
Intuition Why DFS needs this
DFS is a way to walk a graph . If you don't have the picture of dots-and-lines fixed in your head, "traversal", "neighbor", and "adjacency list" are meaningless. Every later symbol is built on this drawing.
The letter V does double duty and this trips people up. Nail it now.
V and E
V = the set of vertices and , when written in a formula like O ( V + E ) , the number of vertices (how many circles).
E = the set of edges and the number of edges (how many lines).
So "v ∈ V " means "v is one of the vertices", and "V = 4 " means "there are 4 vertices". Context tells you which meaning is live.
In the figure above there are V = 4 circles and E = 4 lines.
V is not "a vertex"
V (capital) is the whole collection or the count . A single vertex gets a lowercase letter like u , v , or a number like 0 , 1 , 2 . When the parent writes ∑ v ∈ V it means "add something up, once for each vertex v in the set V ."
An edge can be a two-way tunnel or a one-way street. This choice changes the complexity formula, so it must be crisp.
Definition Undirected vs directed
Undirected edge (left, plain line): you can walk A → B and B → A . One line = travel both ways.
Directed edge (right, arrow): A → B only. To also go back you'd need a second arrow B → A .
Intuition Why the topic cares
The parent's Handshake Lemma line — ∑ deg ( v ) = 2 E for undirected but ∑ outdeg ( v ) = E for directed — is entirely about this distinction. An undirected line is "seen" from both its endpoints (counted twice); a directed arrow leaves from exactly one. Hold that thought for §6.
Now we need the vocabulary DFS actually loops over.
Definition Neighbor and degree
A neighbor of vertex u is any vertex directly connected to u by one edge.
The degree of u , written deg ( u ) , is how many neighbors u has — i.e. how many edges touch it.
For directed graphs, outdeg ( u ) = arrows leaving u .
In the figure, vertex 0 has neighbors 1 and 2 , so deg ( 0 ) = 2 . We store this as an adjacency list : a table where row u lists u 's neighbors.
Definition Adjacency list
An adjacency list is the data layout graph[u] = [list of u's neighbors]. The parent's 0:[1,2] 1:[3] 2:[] 3:[] is an adjacency list: vertex 0 points to 1 and 2, vertex 2 points to nobody.
adjacency list and not a grid?
DFS's inner line for v in graph[u] walks exactly this list. The reason the whole algorithm is O ( V + E ) and not O ( V 2 ) is that scanning graph[u] costs only deg ( u ) — you touch real edges, not empty cells of a V × V table. The data structure choice is the complexity.
DFS's whole visited machinery exists to survive one thing: cycles. Define it.
Definition Path and cycle
A path is a sequence of vertices where each consecutive pair is joined by an edge: 0 → 1 → 3 .
A cycle is a path that returns to where it started: 0 → 1 → 2 → 0 . A tree is a graph with no cycles.
Intuition Why cycles force a visited array
Follow the triangle in the figure with your finger: 0 → 1 → 2 → 0 → 1 → … — you never stop. That is why the parent must mark rooms. In a tree (no cycle) there's only one way to reach each node, so a plain traversal never revisits and needs no marker. Cycle = the reason visited exists.
The parent writes O ( V + E ) , ∑ v deg ( v ) = 2 E , and ( 2 n ) . Decode each.
O ( ⋅ )
O ( f ) means "the work grows no faster than f , ignoring constant multipliers and small terms." O ( V + E ) = "work is proportional to (number of vertices plus number of edges)." We add because DFS does one chunk of work per vertex and a separate chunk per edge.
Definition The summation symbol
∑
∑ v ∈ V deg ( v ) reads: "for every vertex v in V , take deg ( v ) , and add them all together." It is just a compact for-loop that adds .
∑ v deg ( v ) = 2 E (the Handshake Lemma)
Every undirected edge has two endpoints. When you total up all the degrees, each edge gets counted once from each end — so the grand total is exactly 2 E . Picture shaking hands: every handshake involves two hands, so total hands-in-handshakes = 2 × (number of handshakes). See Handshake Lemma .
Definition The choose symbol
( 2 n )
( 2 n ) = 2 n ( n − 1 ) counts how many unordered pairs you can pick from n things. In a complete graph every pair is joined, so E = ( 2 n ) — that's where the parent's O ( n 2 ) forecast comes from.
DFS backtracks "for free." The reason is one data structure.
A stack is a pile where the last thing you put on is the first you take off — L ast I n, F irst O ut. push adds to the top, pop removes from the top. See Stack data structure .
Intuition Why a stack = backtracking
When you dive 0 → 1 → 3 and dead-end at 3, the most recent fork you must return to is 1, then 0 — exactly reverse order of how you entered. That is LIFO. The call stack of recursion is a stack you never had to build; that's why recursive DFS is so short. See Recursion and the Call Stack .
Degree and Handshake Lemma
DFS complexity O of V plus E
Read it top-down: the raw picture (vertices/edges) feeds every branch; cycles justify the visited array; the stack + recursion give backtracking; degree + Handshake give the cost — and all of it converges on DFS.
Cover the right side and answer each before you open the parent note.
A graph G = ( V , E ) is made of what two things? Vertices (dots/circles) and edges (lines joining them).
What does V mean inside the formula O ( V + E ) ? The number of vertices (its other meaning is the whole set of vertices).
Difference between an undirected and a directed edge? Undirected = travel both ways on one line; directed = one-way, shown by an arrow.
What is deg ( u ) ? The number of edges touching u — i.e. how many neighbors u has.
What does graph[u] (an adjacency list row) store? The list of u 's direct neighbors.
What is a cycle, and why does DFS fear it? A path returning to its start (0 → 1 → 2 → 0 ); without a visited marker DFS would loop around it forever.
State the Handshake Lemma in one line. Summing all vertex degrees counts every undirected edge twice, so ∑ v deg ( v ) = 2 E .
What does ( 2 n ) equal and count? 2 n ( n − 1 ) — the number of unordered pairs from n items (edges of a complete graph).
What does LIFO mean and why does DFS use it? Last In First Out; it makes you return to the most-recent fork first, which is backtracking.
Why is recursive DFS "free" backtracking? The program's call stack is already a LIFO stack, so returns happen automatically.
Parent: back to the DFS topic .
Stack data structure — the LIFO engine.
Recursion and the Call Stack — free backtracking.
Handshake Lemma — why ∑ deg = 2 E .
Cycle Detection — cycles are why visited exists.
BFS — Breadth-First Search — same foundations, a queue instead of a stack.
Connected Components — repeated DFS launches over the same graph.
Topological Sort — needs directed edges from §3.