dijkstra’s shortest paths cs 312 lecture 4. announcements project 1 comes out friday –min...
Post on 21-Dec-2015
213 views
TRANSCRIPT
Announcements
• 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
Objectives
• Follow up – big-O, big-Theta and big-Omega– Prim’s algorithm: outside of class.
• Understand the shortest paths problem.
• Understand Dijkstra’s algorithm
• Analyze Dijkstra’s algorithm
Big-Letters
bounds-exact describes -big
)}()()(..,|:{))((
or )),(())(())((
bounds-lower describes -big
)}()(..|:{))((
bounds-upper describes O-big
)}()(..|:{))((
0
0
0
ncfntndfndctnf
nfnfOnf
ndfntndtnf
ncfntnctnfO
NRRN
NRRN
NRRN
THE PROBLEMGiven:
•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 Dijkstra’s Algorithm.
1
2
34
5
10
10
100
30
20
50
5
50
Dijkstra’s Algorithm: Overview•Maintain
•a set S of vertices whose minimum distance from the source is known, •a set C of candidate vertices (SC=N, SC=).
•Initially, S={s}. (the source)•Keep adding vertices to S, choosing from C the node whose distance from the source is least.•Eventually, S=N.
Special path: a path from ‘s’ with all intermediate nodes in S.
Maintain an array D with:•If w S, then D[w] is the length of the shortest path to w.•If w S, then D[w] is the length of the shortest special path from s to w.
Dijkstra’s algorithmL[i,j]: the weight along path from i to j. D[k]: 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 (L[1…n, 1…n]) : array [2…n] array D [2…n]C {2,3…n} for i 2 to n do D[i] L[1,i] repeat n - 2 times v some element of C minimizing D[v] C C \ {v} for each w in C do D[w] min (D[w] , D[v] + L[v,w])return D
Inner loop
1
2
34
5
10
10
100
30
20
50
5
50 StepInit.
v-
C{2,3,4,5}
D[50, 30, 100, 10]
Choose node from C minimizing D (node 5)
1
2
34
5
10
10
100
30
20
50
5
50 StepInit.
1
v-5
C{2,3,4,5}{2,3,4}
D[50, 30, 100, 10][50, 30, 20, 10]
Compute D for another node in C
1
2
34
5
10
10
100
30
20
50
5
50 StepInit.
1
v-5
C{2,3,4,5}{2,3,4}
D[50, 30, 100, 10][50, 30, 20, 10]
Compute D for another node in C
1
2
34
5
10
10
100
30
20
50
5
50 StepInit.
1
v-5
C{2,3,4,5}{2,3,4}
D[50, 30, 100, 10][50, 30, 20, 10]
Compute D for another node in C
1
2
34
5
10
10
100
30
20
50
5
50 StepInit.
1
v-5
C{2,3,4,5}{2,3,4}
D[50, 30, 100, 10][50, 30, 20, 10]
Choose node from C minimizing D (node 4)
1
2
34
5
10
10
100
30
20
50
5
50 StepInit.
12
v-54
C{2,3,4,5}{2,3,4}{2,3}
D[50, 30, 100, 10][50, 30, 20, 10][50, 30, 20, 10]
Compute D for another node in C
1
2
34
5
10
10
100
30
20
50
5
50 StepInit.
12
v-54
C{2,3,4,5}{2,3,4}{2,3}
D[50, 30, 100, 10][50, 30, 20, 10][40, 30, 20, 10]
Compute D for another node in C
1
2
34
5
10
10
100
30
20
50
5
50 StepInit.
12
v-54
C{2,3,4,5}{2,3,4}{2,3}
D[50, 30, 100, 10][50, 30, 20, 10][40, 30, 20, 10]
Choose node from C minimizing D (node 3)
1
2
34
5
10
10
100
30
20
50
5
50 StepInit.
123
v-543
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]
Compute D for another node in C (last one)
To find not only the length, but where they pass:•add an array P[2..n], where P[v] contains v’s predecessor•follow the pointers P backwards from destination to source.
Modify the code:
•initialize P[i] to 1, for I=2,3,…,n
•replace the contents of the inner for loop by
if D[w]>D[v] + L[v,w] thenD[w]D[v]+L[v,w]
P[w]v
Special 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
2
34
5
10
10
100
30
20
50
5
50 S = {4,5}then path 1,5,4 is specialand path 1,4,3 is special but path 1,3,2 is not.
We prove by mathematical induction that•(a) If w1 and w S, then D[w] is the length of the shortest path to w from the source.•(b) If w S, then D[w] 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 D[v] is the length of the shortest path from s to v.By the induction hypothesis, D[v] is the shortest special path, so we must verify that the shortest path from s to v does not go outside of S.
s
v
x
The shortest path.
The shortest special path.
Suppose the contrary; the shortest path from s is not special, that is it goes outside S, and the first node encountered is x.
s
v
x
The shortest path.
The shortest special path.
Since the path from 1…x is special, D[x] = length of shortest special pathfrom 1 … x by (b).
D[x], by induction hypothesis (b)
s
v
x
So total distance from 1…x…v > 1…x (since … what?) and D[x] > D[v] since D[v] was chosen first. Therefore, D[x]+length(x…v) > D[v].
D[x], by induction hypothesis on (b)
Dijkstra (L[1…n, 1…n]) : array [2…n] array D [2…n]C {2,3…n} for i 2 to n do D[i] L[1,i] repeat n - 2 times v some element of C minimizing D[v] C C \ {v} for each w in C do D[w] min (D[w] , D[v] + L[v,w])return D
Analysis
)(nO