Lecture notes · Algorithms · Lecture 10

Depth-First Search

MIT 6.006 Introduction to Algorithms, lecture 10, explained in plain English

Want this lecture to stick? In LessonBound you get checked practice questions for every part, can ask about anything you don't get, and can listen with your screen off. Free, no ads.

Learn this lecture free

Watch or read the original lecture: MIT OpenCourseWare.

Part 1 · 0:00–10:00

This part reviews what a graph is, explains what linear time means, and introduces depth-first search and the reachability problem.

Key ideas

Words to know

vertex
A point in a graph. It is also called a node. Think of it as a dot.
edge
A connection between two vertices. In a directed graph it is an arrow with a direction, so you can only travel one way along it.
linear time
A way of describing speed: the work grows at the same rate as the size of the input. Double the input, and the work roughly doubles.
reachability
Whether you can get from a starting point to another point by following the allowed connections, such as edges in a graph.

Example

Imagine a town where intersections are vertices and one-way streets are edges. Reachability asks which intersections you can drive to from your house without breaking any traffic rules. Breadth-first search would check every place one turn away, then two turns away. Depth-first search would keep driving down one street as far as it goes, then turn back at a dead end.

3 checked practice questions for this part are in the app.

Part 2 · 10:00–20:00

This part explains depth-first search, a method that starts at a source node and follows paths as far as it can to find every node that can be reached from that source, and then proves the method works.

Key ideas

Words to know

reachable
A node is reachable from a source if there is some route of edges that gets you from the source to that node.
parent tree
A record that stores, for each node, which node you came from to reach it. Following the links backward traces a route to the source.
recursion
When a function calls itself to handle a smaller piece of the same job, like a set of nested boxes opened one inside another.
induction
A proof method. You show the claim is true for the starting case, then show that if it is true for one step, it is true for the next step.

Example

Think of exploring a maze with a ball of string. You walk down one corridor as far as it goes, and when you hit a dead end you walk back to the last junction and try a different corridor. Depth-first search works the same way, always going deep before it comes back to try other options.

3 checked practice questions for this part are in the app.

Part 3 · 20:00–30:00

Depth-first search explores a graph by going as deep as it can along one path, and this lesson shows how it can be used to find the separate clumps of connected vertices in a graph.

Key ideas

Words to know

reachable
You can get to it by following edges from where you start.
recursive
A function that calls itself to do part of the work, like a task that hands smaller versions of itself to helpers.
undirected graph
A graph where edges go both ways, so you can travel either direction along an edge.
connected component
A group of vertices where you can get from any one of them to any other, and which has no edges leading out to the rest of the graph.

Example

Imagine exploring a city by always taking the first street you see and walking as far as you can. When you hit a dead end, you turn back to the last fork and try another street. You will see every place you can reach, but the route you walked may be much longer than the shortest one.

3 checked practice questions for this part are in the app.

Part 4 · 30:00–40:00

This part explains how depth-first search runs in linear time, how it finds separate groups in a graph, and how it can put the nodes of a special kind of graph (a DAG) into a valid order.

Key ideas

Words to know

DAG
Directed Acyclic Graph: a set of dots joined by one-way arrows, with no loops that let you walk in a circle back to a starting dot.
topological order
A list of all the dots in a directed graph where every arrow points from a dot earlier in the list to a dot later in the list.
finishing order
The order in which DFS finishes visiting each dot, which is the moment all of that dot's neighbors have been checked.
connected component
A group of dots that can all reach each other through edges, and that has no edges linking it to the rest of the graph.

Example

Imagine a class where two students are friends only with each other. If you start a search at one of them and follow friendships, you will find just those two, and you know they form their own group. Doing this from every student finds every separate group in the class.

3 checked practice questions for this part are in the app.

Part 5 · 40:00–52:18

This part shows how a depth-first search can put the nodes of a graph in a valid order and how it can detect and find a cycle.

Key ideas

Words to know

DAG
Directed acyclic graph: a graph whose arrows point one way and never loop back to where they started.
topological order
A list of all the nodes where every arrow goes from an earlier node in the list to a later one.
cycle
A path of arrows that starts at one node and comes back to that same node.
ancestor
A node that was visited earlier in the search's chain of calls, before the node you are looking at.

Example

Think of getting ready for work: you must put on socks before shoes, and a shirt before a jacket. A valid order lists each task after the ones it depends on. If two tasks each had to come before the other, no valid order would exist, just like a cycle in a graph.

3 checked practice questions for this part are in the app.

← Lecture 9: Breadth-First Search · Lecture 11: Weighted Shortest Paths →

Notes written by AI (Claude) from the lecture's official transcript, so check anything important against the lecture. MIT OpenCourseWare · CC BY-NC-SA 4.0 · not affiliated with or endorsed by MIT. These notes are shared under the same license.

LessonBound · Free lecture notes · Privacy · Terms · New York