Dijkstra's Shortest Path Algorithm
Dijkstra's Shortest Path Algorithm
Greedy Algorithm
1
Optimization Problems
• Some problems can have many possible/ feasible solutions
with each solution having a specific cost. We wish to find the
best solution with the optimal cost.
• Maximization problem finds a solution with maximum cost
• Minimization problem finds a solution with minimum cost
2
Optimization Problems
• Two common techniques:
• Greedy Algorithms (local)
• Make the greedy choice and THEN
• Solve sub-problem arising after the choice is made
• The choice we make may depend on previous choices,
but not on solutions to sub-problems
• Top down solution, problems decrease in size
• Dynamic Programming (global)
• We make a choice at each step
• The choice depends on solutions to sub-problems
3
• Bottom up solution, smaller to larger sub-problems
Greedy Algorithm
• A greedy algorithm works in phases. At each phase:
• makes the best choice available right now, without regard for future
consequences
• hopes to end up at a global optimum by choosing a local optimum at
each step. For some problem, it works
5
Single-Source Shortest Path
Problem
• Problem: Given a weighted directed graph G, find the shortest paths
from a source vertex v to all other vertices in the graph.
• “Shortest-path” = minimum weight
• Weight of path is sum of edges
• E.g., a road map: what is the shortest path from Chapel Hill to
Charlottesville?
Dijkstra's algorithm
Dijkstra's algorithm - is a solution to the single-source shortest path
problem in graph theory.
Input: Weighted graph G={E,V} and source vertex v∈V, such that all
edge weights are nonnegative
24
2 3
9
s
18
14
2 6
6
30 4 19
11
15 5
5
6
20 16
t
7 44
Dijkstra's Shortest Path
Algorithm S={ }
PQ = { s, 2, 3, 4, 5, 6, 7, t }
∞
∞
24
2 3
0 9
s
18
14 ∞ 2 6
6 ∞
30 ∞ 4 19
11
15 5
5
6
20 16
t
7 44
distance label ∞ ∞
Dijkstra's Shortest Path
Algorithm
S={ }
PQ = { s, 2, 3, 4, 5, 6, 7, t }
delmin
∞
∞
24
2 3
0 9
s
18
14 ∞ 2 6
6 ∞
30 ∞ 4 19
11
15 5
5
6
20 16
t
7 44
distance label ∞ ∞
Dijkstra's Shortest Path
Algorithm S={s}
PQ = { 2, 3, 4, 5, 6, 7, t }
decrease key
∞
∞
X 9
24
2 3
0 9
s
18
14 X 14
∞ 6
2
6 ∞
30 ∞ 4 19
11
15 5
5
6
20 16
t
7 44
distance label ∞ 15
X ∞
Dijkstra's Shortest Path
Algorithm S={s}
PQ = { 2, 3, 4, 5, 6, 7, t }
delmin
∞
∞
X 9
24
2 3
0 9
s
18
14 X 14
∞ 6
2
6 ∞
30 ∞ 4 19
11
15 5
5
6
20 16
t
7 44
distance label ∞ 15
X ∞
Dijkstra's Shortest Path
Algorithm S = { s, 2 }
PQ = { 3, 4, 5, 6, 7, t }
∞
∞
X 9
24
2 3
0 9
s
18
14 X 14
∞ 6
2
6 ∞
30 ∞ 4 19
11
15 5
5
6
20 16
t
7 44
∞ 15
X ∞
Dijkstra's Shortest Path
Algorithm S = { s, 2 }
PQ = { 3, 4, 5, 6, 7, t }
decrease key
∞ 33
X
∞
X 9
24
2 3
0 9
s
18
14 X 14
∞ 6
2
6 ∞
30 ∞ 4 19
11
15 5
5
6
20 16
t
7 44
∞ 15
X ∞
Dijkstra's Shortest Path
Algorithm S = { s, 2 }
PQ = { 3, 4, 5, 6, 7, t }
∞ 33
X
∞
X 9
24
2 3
0 9
delmin
s
18
14 X 14
∞ 6
2
6 ∞
30 ∞ 4 19
11
15 5
5
6
20 16
t
7 44
∞ 15
X ∞
Dijkstra's Shortest Path
Algorithm S = { s, 2, 6 }
PQ = { 3, 4, 5, 7, t }
32
∞ 33
X X
∞
X 9
24
2 3
0 9
s
18
14 X 14
∞ 6
2
6 ∞
44
30 X
∞ 4 19
11
15 5
5
6
20 16
t
7 44
∞ 15
X ∞
Dijkstra's Shortest Path
Algorithm S = { s, 2, 6 }
PQ = { 3, 4, 5, 7, t }
32
∞ 33
X X
∞
X 9
24
2 3
0 9
s
18
14 X 14
∞ 6
2
6 ∞
44
30 X
∞ 4 19
11
15 5
5
6
20 16
t
7 44
∞ 15
X delmin ∞
Dijkstra's Shortest Path
Algorithm S = { s, 2, 6, 7 }
PQ = { 3, 4, 5, t }
32
∞ 33
X X
∞
X 9
24
2 3
0 9
s
18
14 X 14
∞ 6
2
6 ∞
X 35
44
30 X
∞ 4 19
11
15 5
5
6
20 16
t
7 44
X 15
∞ 59 ∞
X
Dijkstra's Shortest Path
Algorithm S = { s, 2, 6, 7 }
PQ = { 3, 4, 5, t } delmin
32
∞ 33
X X
∞
X 9
24
2 3
0 9
s
18
14 X 14
∞ 6
2
6 ∞
X 35
44
30 X
∞ 4 19
11
15 5
5
6
20 16
t
7 44
X 15
∞ 59 ∞
X
Dijkstra's Shortest Path
Algorithm S = { s, 2, 3, 6, 7 }
PQ = { 4, 5, t }
32
∞ 33
X X
∞
X 9
24
2 3
0 9
s
18
14 X 14
∞ 6
2
6 ∞
X 35
44 X 34
30 X
∞ 4 19
11
15 5
5
6
20 16
t
7 44
X 15
∞ X ∞
51 59 X
Dijkstra's Shortest Path
Algorithm S = { s, 2, 3, 6, 7 }
PQ = { 4, 5, t }
32
∞ 33
X X
∞
X 9
24
2 3
0 9
s
18
14 X 14
∞ 6
2
6 ∞
X 35
44 X 34
30 X
∞ 4 19
11
15 5
5
6
20 16
delmin
t
7 44
X 15
∞ X ∞
51 59 X
Dijkstra's Shortest Path
Algorithm S = { s, 2, 3, 5, 6, 7 }
PQ = { 4, t }
32
∞ 33
X X
∞
X 9
24
2 3
0 9
s
18
14 X 14
∞ 6
2
6 45 ∞
X
X 35
44 X 34
30 X
∞ 4 19
11
15 5
5
6
20 16
t
7 44
X 15
∞ 50 51 X ∞
X 59 X
Dijkstra's Shortest Path
Algorithm S = { s, 2, 3, 5, 6, 7 }
PQ = { 4, t }
32
∞ 33
X X
∞
X 9
24
2 3
0 9
s
18
14 X 14
∞ 6
2
6 45 ∞
X
X 35
44 X 34
30 X
∞ 4 19
11
15 5 delmin
5
6
20 16
t
7 44
X 15
∞ 50 51 X ∞
X 59 X
Dijkstra's Shortest Path
Algorithm S = { s, 2, 3, 4, 5, 6, 7 }
PQ = { t }
32
∞ 33
X X
∞
X 9
24
2 3
0 9
s
18
14 X 14
∞ 6
2
6 45 ∞
X
X 35
44 X 34
30 X
∞ 4 19
11
15 5
5
6
20 16
t
7 44
X 15
∞ 50 51 X ∞
X 59 X
Dijkstra's Shortest Path
Algorithm S = { s, 2, 3, 4, 5, 6, 7 }
PQ = { t }
32
∞ 33
X X
∞
X 9
24
2 3
0 9
s
18
14 X 14
∞ 6
2
6 45 ∞
X
X 35
44 X 34
30 X
∞ 4 19
11
15 5
5
6
20 16
t
7 44
delmin 50 51 X ∞
X 59 X
X 15
∞
Dijkstra's Shortest Path
Algorithm S = { s, 2, 3, 4, 5, 6, 7, t }
PQ = { }
32
∞ 33
X X
∞
X 9
24
2 3
0 9
s
18
14 X 14
∞ 6
2
6 45 ∞
X
X 35
44 X 34
30 X
∞ 4 19
11
15 5
5
6
20 16
t
7 44
X 15
∞ 50 51 X ∞
X 59 X
Dijkstra's Shortest Path
Algorithm S = { s, 2, 3, 4, 5, 6, 7, t }
PQ = { }
32
∞ 33
X X
∞
X 9
24
2 3
0 9
s
18
14 X 14
∞ 6
2
6 45 ∞
X
X 35
44 X 34
30 X
∞ 4 19
11
15 5
5
6
20 16
t
7 44
X 15
∞ 50 51 X ∞
X 59 X
Implementations and Running Times
• The simplest implementation is to store vertices in an array or
linked list. This will produce a running time of O(|V|^2 + |E|)
• For sparse graphs, or graphs with very few edges and many
nodes, it can be implemented more efficiently storing the graph
in an adjacency list using a binary heap or priority queue. This
will produce a running time of O((|E|+|V|) log |V|)