308-203A
Introduction to Computing II
Lecture 16: Dijkstra’s
Algorithm
Fall Session 2000
Graphs with Weighted Edges
Graphs frequently come with extra “satellite data”
for the vertices or edges.
A common varient is assigning each edge some positive
weight.
Graph G = (V, E)
weight: E
(i.e. a mapping from
edges to real numbers)
Example
The paradigmatic example is a map of highways
labelled with distances:
Roberval
101
Val-d’Or
259
318
531
Trois-Rivières
130
211
206
Québec
142
158
Montréal
147
Chicoutimi
Sherbrooke
Rivière-du-Loup
Weight of a Path Problems
For a path, P:
P = ( v1, v2, … vi )
The weight of the path is:
WP = weight(vi, v(i+1))
Shortest-Path Problems
There is a general class of problems on such
graphs involving finding paths of minimal weight.
Some are easy to solve and some are hard….
A Hard Problem
The Travelling Salesman Problem: find a shortest
path through a connected graph which visits all of
the vertices exactly once.
This problem is “NP-Complete,” which means
there is no computationally tractable solution
(according to decades of empirical evidence,
NP-Complete problems require exponential time
to solve)
A Not-So-Hard Problem
The Single-Source Shortest Path Problem:
Given a vertex, v, find the shortest path from v to
every vertex reachable from v.
A nice solution is Dijkstra’s algorithm, which is
a kind of algorithm known as a “Greedy” algorithm.
Greedy algorithms
A “Greedy” algorithm is an algorithm based on the
principle of always taking the choice which looks
best in the short-run.
In most cases, this does not actually yield an optimal
long-term solution (or even a good one)
A Greedy Algorithm that Works
Say you’re on a long drive.
Clearly, the way to minimize the number of gas stops
is to stop at a gas station only when you know you’ll
run out of gas before you see another one.
This is “greedy” because given the choice to stop or
not to stop, you always choose the one which appears
to minimize the total number of stops.
A Flawed “Greedy Algorithm”
A greedy algorithm does not work for the Travelling
Salesman Problem:
Assume we start from “a”
a
10
b
3
c
1
5
8
d
e
14
A Flawed “Greedy Algorithm”
Best “greedy” choices gives the path:
P = (a, b, c, d, e)
a
10
b
3
Weight = 30
c
1
5
8
d
e
14
A Flawed “Greedy Algorithm”
But I can find a better path:
P = (a, b, e, c, d)
a
10
b
3
Weight = 26
c
1
5
8
d
e
14
Dijkstra’s Algorithm
For the Single-Source Shortest Path Problem, the
“Greedy” approach actually does work.
Idea:
1) Label all vertices with a best-known distance
so far
2) Initialize distance to 0 for the starting vertex
and for all others
Dijkstra’s Algorithm
Idea (continued):
3) Maintain a list, A, of “unexplored” vertices, which
is initialized to all vertices
4) Choose vertices from A to explore by a “greedy”
strategy: always take the one with minimum distance
5) When you explore a vertex, update the best known
distance to its neighbors and remove it from A.
Dijkstra (pseudocode)
for each v V, distance(v) = ;
distance( startVertex ) = 0;
A := V;
B := Ø ;
for j := 1 to ( |V| - 1 )
{
u := Extract-Min( A ) ;
B := B { u } ;
for each v Neighbors( u )
Relax(v, u)
}
// Initialize
// Grow set B
// “Greedy” choice
// Update distances
Helper Routine
Updates are performed by checking whether u
yields a better way to get to v
Relax(Vertex v, Vertex u)
{
newWeight = distance( u ) + weight( u, v ) ;
if (newWeight < distance( v ) )
distance( v ) = newWeight ;
}
Example
Initialize starting vertex to 0 and all others to
5
0
a
b
2
3
d
c
8
A = {a, b, c, d, e, f }
e
7
3
B=
1
f
Example
Remove minimum element from A and call Relax()
5
0
5
a
b
2
3
3 d
c
8
A = {b, c, d, e, f }
e
7
3
B={a}
1
f
Example
Remove minimum element from A and call Relax()
5
0
5
a
b
2
3
3 d
c
8
A = {b, c, d, e, f }
e
7
3
B={a}
1
f
Example
Now “d” is the minimum in A
5
0
a
b
c
2
3
3 d
5
8
e
11
7
3
8 + 3 =11
A = {b, c, e, f }
B = { a, d }
1
f
Example
Now “b” is the minimum in A:
We find we now have a better path to “e”
5
0
a
5
b
3
5+2 = 7
3 d
8
A = { c, e, f }
c
2
e
7
7
3
B = { a, b, d }
1
f
Example
Now “e” is the minimum in A:
5
0
a
5
b
2
3
3 d
A = { c, f }
c
8
e
7
7
3
7 + 7 = 14
1
f
7 + 3 = 10
B = { a, b, d, e }
Example
Now “f” is the minimum in A:
5
0
a
5
b
2
3
3 d
A={ f}
c
8
e
7
7
3
11
1
f
10
B = { a, b, c, d, e }
10 + 1
= 11
Example
Done!
5
0
a
5
b
2
3
3 d
A={ f}
c
8
e
7
7
3
11
1
f
10
B = { a, b, c, d, e }
Running time
Like for Depth and Breadth-First Searches,
we execute a body of code once per vertex,
and that code looks only at the neighbors of
the vertex it is run for:
O( |V| + |E| )
Proof of Correctness
Since “Greedy Algorithms” don’t always give optimal
solutions, we must furnish a proof that, in the particular
case of Dijkstra’s algorithm, one actually does
compute the shortest possible paths…
Any questions?
© Copyright 2026 Paperzz