Lecture notes · Algorithms · Lecture 13

Dijkstra

MIT 6.006 Introduction to Algorithms, lecture 13, 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 explains why shortest paths can be found faster when no edge has a negative weight, by growing outward from the start vertex one distance step at a time.

Key ideas

Words to know

invariant
A rule that stays true at every step of a process. Here, the distances we have already found stay correct as the algorithm keeps going.
relaxing
Checking an edge and updating a stored distance when you find a shorter route through it.
monotonically
Always moving in one direction. Here, distances going up along a path, or staying the same, but never going down.
dense
A graph with a lot of edges, close to every possible pair of vertices being connected.

Example

Think of driving from home to a friend's house and passing a gas station on the way. The total trip can never be shorter than the drive to the gas station, because the rest of the trip only adds distance. That is why, with no negative weights, the distance to a vertex is never less than the distance to any vertex before it on the path.

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

Part 2 · 10:00–20:00

This part explains why Dijkstra's method needs edge weights that are never negative, and how knowing the order of vertices by distance can make shortest-path search fast.

Key ideas

Words to know

Negative weight
A cost on an edge that is below zero, so going along it makes the total path shorter instead of longer.
DAG
Directed acyclic graph: a set of points joined by one-way arrows that never loop back around to where they started.
Zero-weight edge
A connection between two points that adds no cost to the path.
Linear time
A way of measuring work where doubling the size of the problem roughly doubles the time it takes.

Example

Imagine hiking away from a campsite where every step on the trail takes you farther from camp, so your distance only grows. Now imagine a trail with a shortcut that suddenly brings you back close to camp. The first trail is easy to track outward, but the shortcut makes that tracking go wrong, which is the problem negative weights cause.

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

Part 3 · 20:00–30:00

This part introduces Dijkstra's algorithm, which finds shortest paths in a weighted graph with non-negative weights by always handling the closest unfinished vertex next, using a special priority queue.

Key ideas

Words to know

priority queue
A list where you can always take out the item with the smallest value (its key) first.
changeable priority queue
A priority queue that also lets you find an item by its ID and make its key smaller.
relax
To check whether a path through an edge gives a shorter distance, and if so, update the distance estimate.
direct access array
A simple list where you find an item right away by its number, like going straight to shelf number 7.

Example

Think of a to-do list sorted by due date, where the item due soonest is always at the top. If you learn that one task is now more urgent, you move that task up by its name or number, and the list reorders itself. The changeable priority queue works the same way with distance estimates.

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

Part 4 · 30:00–40:00

This part explains how Dijkstra's algorithm keeps a list of distance guesses, always picks the closest unfinished vertex, and updates its neighbors, then runs it on a small example.

Key ideas

Words to know

relax
To check whether a path through a vertex gives a shorter distance to a neighbor, and if it does, update the neighbor's distance guess to that shorter value.
priority queue
A line of items where the item with the smallest key always comes out first. Here, the key is the distance guess.
triangle inequality
The rule that a direct path between two points is never longer than a path that goes through a third point. A relaxation fixes a spot where this rule is broken.
edge weight
The cost or length of traveling along one edge (one connection) between two vertices (points) in a graph.

Example

Think of planning a road trip. You always go to the nearest town you have not yet visited, then check whether the road through that town gets you to other towns faster than you knew before. Dijkstra works the same way: it always picks the closest unfinished vertex and checks whether it can shorten the route to its neighbors.

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

Part 5 · 40:00–50:00

This part explains why Dijkstra's method, once it finishes, has found the true shortest distance to every vertex, using a proof by induction.

Key ideas

Words to know

relaxation
Checking whether a path through one vertex gives a shorter route to the next vertex, and if so, updating the distance estimate to the shorter value.
induction
A proof method: show the claim is true for the first step, then show that if it is true up to some step, it must also be true for the next step.
priority queue
A list that always gives back the item with the smallest value first, here the vertex with the smallest distance estimate.
subpath
A piece of a longer path, from its start to some point along the way. A piece of a shortest path is also a shortest path to that point.

Example

Imagine you are planning trips from home to several towns, and you keep a note of the shortest known trip to each one. You always go to the closest town you have not yet visited, and any shorter route you find updates your note. Once you visit a town, its note is final, because no shorter route can turn up later.

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

Part 6 · 50:00–57:16

This part explains how fast Dijkstra's algorithm runs, and how the choice of priority queue changes that speed.

Key ideas

Words to know

priority queue
A list that always lets you quickly grab the item with the smallest value, and lets you change values as you go.
decrease key
Lowering the value stored for an item already in the queue, such as lowering a distance when a shorter path is found.
sparse graph
A graph with relatively few edges. The lecture describes it as one where the number of edges is at most the number of vertices.
binary heap
A tree-shaped way of storing items so the smallest is always easy to find. Adding or changing an item takes only about log-many steps.

Example

Think of a to-do list where you always need the most urgent task. If you keep the list unsorted, changing a task is quick, but finding the most urgent one means reading the whole list. A sorted or tree-shaped list makes finding the most urgent task quick, at the cost of a bit more work each time you make a change.

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

← Lecture 11: Weighted Shortest Paths · Lecture 15: Dynamic Programming, Part 1: SRTBOT, Fib, DAGs, Bowling →

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