Title: Dijkstra
1 Dijkstras Shortest Paths
CS 312 Lecture 4
2Announcements
- Project 1 comes out Friday
- Min spanning trees and scheduling
- due 2 weeks from Friday (1 week later than
current calendar) - Quiz 1 is ready and waiting
- Open book
- 30 min limit
- get a copy on line before you start
3Objectives
- Follow up
- big-O, big-Theta and big-Omega
- Prims algorithm outside of class.
- Understand the shortest paths problem.
- Understand Dijkstras algorithm
- Analyze Dijkstras algorithm
4Big-Letters
5THE PROBLEM
- Given
- a directed graph G?N,A?
- each edge has a nonnegative length
- one designated source node
- Determine the length of the shortest path from
the source to each of the other nodes of the
graph.
THE SOLUTION
A greedy algorithm called Dijkstras Algorithm.
6- Dijkstras Algorithm Overview
- Maintain
- a set S of vertices whose minimum distance from
the source is known, - a set C of candidate vertices (S?CN, S?C?).
- Initially, Ss. (the source)
- Keep adding vertices to S, choosing from C the
node whose distance from the source is least. - Eventually, SN.
- Special path a path from s with all
intermediate nodes in S. - Maintain an array D with
- If w ? S, then Dw is the length of the shortest
path to w. - If w ? S, then Dw is the length of the shortest
special path from s to w.
1
10
50
30
2
5
100
5
10
20
3
4
50
7Dijkstras algorithm
Li,j the weight along path from i to j.
Dk length of the shortest special path from
source to k. j in C shortest path from source
to j is not known. k in S shortest path from
source to k is known. Dijkstra (L1n, 1n)
array 2n array D 2n C ? 2,3n for i ? 2
to n do Di ? L1,i repeat n - 2 times v
? some element of C minimizing Dv C ? C \
v for each w in C do Dw ? min
(Dw , Dv Lv,w) return D
Inner loop
8Choose node from C minimizing D (node 5)
1
Step Init.
v -
C 2,3,4,5
D 50, 30, 100, 10
10
50
30
2
5
100
5
10
20
3
4
50
9Compute D for another node in C
1
Step Init. 1
v - 5
C 2,3,4,5 2,3,4
D 50, 30, 100, 10 50, 30, 20, 10
10
50
30
2
5
100
5
10
20
3
4
50
10Compute D for another node in C
1
Step Init. 1
v - 5
C 2,3,4,5 2,3,4
D 50, 30, 100, 10 50, 30, 20, 10
10
50
30
2
5
100
5
10
20
3
4
50
11Compute D for another node in C
1
Step Init. 1
v - 5
C 2,3,4,5 2,3,4
D 50, 30, 100, 10 50, 30, 20, 10
10
50
30
2
5
100
5
10
20
3
4
50
12Choose node from C minimizing D (node 4)
1
Step Init. 1
v - 5
C 2,3,4,5 2,3,4
D 50, 30, 100, 10 50, 30, 20, 10
10
50
30
2
5
100
5
10
20
3
4
50
13Compute D for another node in C
1
Step Init. 1 2
v - 5 4
C 2,3,4,5 2,3,4 2,3
D 50, 30, 100, 10 50, 30, 20, 10 50, 30, 20,
10
10
50
30
2
5
100
5
10
20
3
4
50
14Compute D for another node in C
1
Step Init. 1 2
v - 5 4
C 2,3,4,5 2,3,4 2,3
D 50, 30, 100, 10 50, 30, 20, 10 40, 30, 20,
10
10
50
30
2
5
100
5
10
20
3
4
50
15Choose node from C minimizing D (node 3)
1
Step Init. 1 2
v - 5 4
C 2,3,4,5 2,3,4 2,3
D 50, 30, 100, 10 50, 30, 20, 10 40, 30, 20,
10
10
50
30
2
5
100
5
10
20
3
4
50
16Compute D for another node in C (last one)
1
Step Init. 1 2 3
v - 5 4 3
C 2,3,4,5 2,3,4 2,3 2
D 50, 30, 100, 10 50, 30, 20, 10 40, 30, 20,
10 35, 30, 20, 10
10
50
30
2
5
100
5
10
20
3
4
50
17- To find not only the length, but where they pass
- add an array P2..n, where Pv contains vs
predecessor - follow the pointers P backwards from destination
to source.
- Modify the code
- initialize Pi to 1, for I2,3,,n
- replace the contents of the inner for loop by
- if DwgtDv Lv,w thenDw?DvLv,w
-
Pw?v
18Special Path
A path from the source to node x is special if
all nodes in the path from the source to x
belong to S.
1
10
50
S 4,5 then path 1,5,4 is special and path
1,4,3 is special but path 1,3,2 is not.
30
2
5
100
5
10
20
3
4
50
19- We prove by mathematical induction that
- (a) If w?1 and w ? S, then Dw is the length of
the shortest path to w from the source. - (b) If w ? S, then Dw is the length of the
shortest special path from s to w. - Basis Initially, only s ? S, so (a) holds
(vacuously). (b) is true because the only special
path from s is the direct path and D is
initialized accordingly. - Induction hypothesis Conditions (a) and (b) hold
before a node v is added to S. - Induction step for (a) For nodes already in S
nothing changes, so (a) is true for them. Before
adding v to S we must check that Dv is the
length of the shortest path from s to v. - By the induction hypothesis, Dv is the shortest
special path, so we must verify that the shortest
path from s to v does not go outside of S.
20Suppose the contrary the shortest path from s is
not special, that is it goes outside S, and the
first node encountered is x.
x
s
v
The shortest path.
The shortest special path.
21Since the path from 1x is special, Dx length
of shortest special path from 1 x by (b).
Dx, by induction hypothesis (b)
x
s
v
The shortest path.
The shortest special path.
22So total distance from 1xv gt 1x (since
what?) and Dx gt Dv since Dv was chosen
first. Therefore, Dxlength(xv) gt Dv.
Dx, by induction hypothesis on (b)
x
s
v
23Dx, by induction hypothesis on (b)
x
s
v
Total distance to v via x gt Dx, (lengths are
positive) Dx gt Dv, (the algorithm chose v
before x)
24Suppose the contrary the shortest path from s
goes outside S, and the first node encountered is
x.
Dx, by induction hypothesis on (b)
x
s
v
Total distance to v via x gt Dx, (lengths are
positive) Dx gt Dv, (the algorithm chose v
before x) ? Total distance to v via x gt Dv,
(transitivity) Contradiction! (The
shortest path is longer than the shortest special
path ???)
25Analysis
Dijkstra (L1n, 1n) array 2n array D
2n C ? 2,3n for i ? 2 to n do Di ?
L1,i repeat n - 2 times v ? some element
of C minimizing Dv C ? C \ v for
each w in C do Dw ? min (Dw , Dv
Lv,w) return D
26Analysis
Dijkstra (L1n, 1n) array 2n array D
2n C ? 2,3n for i ? 2 to n do Di ?
L1,i repeat n - 2 times v ? some element
of C minimizing Dv C ? C \ v for
each w in C do Dw ? min (Dw , Dv
Lv,w) return D
Therefore