3.5.5 · D1Graphs

Foundations — DFS — algorithm, stack - recursion, O(V+E), visited array

1,747 words8 min readBack to topic

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.


1. What is a graph? (vertices and edges)

Start with a picture, not a definition.

Figure — DFS — algorithm, stack - recursion, O(V+E), visited array

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.


2. The symbols and (and why they are numbers and sets)

The letter does double duty and this trips people up. Nail it now.

In the figure above there are circles and lines.


3. Directed vs undirected edges — the arrow matters

An edge can be a two-way tunnel or a one-way street. This choice changes the complexity formula, so it must be crisp.

Figure — DFS — algorithm, stack - recursion, O(V+E), visited array

4. Neighbor, degree, and the adjacency list

Now we need the vocabulary DFS actually loops over.

Figure — DFS — algorithm, stack - recursion, O(V+E), visited array

In the figure, vertex has neighbors and , so . We store this as an adjacency list: a table where row lists 's neighbors.


5. Cycles and paths — why looping is even possible

DFS's whole visited machinery exists to survive one thing: cycles. Define it.

Figure — DFS — algorithm, stack - recursion, O(V+E), visited array

6. Big-O and the Handshake Lemma symbols

The parent writes , , and . Decode each.


7. Stack, LIFO, and recursion — the engine

DFS backtracks "for free." The reason is one data structure.


Prerequisite map

Vertices and edges

Adjacency list

Directed vs undirected

Paths and cycles

Degree and Handshake Lemma

Visited array

Stack LIFO

Backtracking

Recursion call stack

DFS complexity O of V plus E

DFS traversal

DFS parent topic

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.


Equipment checklist

Cover the right side and answer each before you open the parent note.

A graph is made of what two things?
Vertices (dots/circles) and edges (lines joining them).
What does mean inside the formula ?
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 ?
The number of edges touching — i.e. how many neighbors has.
What does graph[u] (an adjacency list row) store?
The list of 's direct neighbors.
What is a cycle, and why does DFS fear it?
A path returning to its start (); 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 .
What does equal and count?
— the number of unordered pairs from 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.

Connections

  • Parent: back to the DFS topic.
  • Stack data structure — the LIFO engine.
  • Recursion and the Call Stack — free backtracking.
  • Handshake Lemma — why .
  • 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.