CS 225
Data Structures
April 23 – Dijkstra’s Algorithm
Wad ade Fag agen-Ulm lmschneid ider
CS 225 Data Structures April 23 Dijkstras Algorithm Wad ade Fag - - PowerPoint PPT Presentation
CS 225 Data Structures April 23 Dijkstras Algorithm Wad ade Fag agen-Ulm lmschneid ider Partition Property Consider an arbitrary partition of the vertices on G into two subsets U and V . V U A 4 Let e be an edge of 8 2 B D
Data Structures
April 23 – Dijkstra’s Algorithm
Wad ade Fag agen-Ulm lmschneid ider
Consider an arbitrary partition of the vertices on G into two subsets U and V.
A C D E B F 8 4 2 7 1 2 3 9 5
U V
Let e be an edge of minimum weight across the partition. Then e is part of some minimum spanning tree.
e
The partition property suggests an algorithm:
A C D E B F G H 16 5 5 2 15 16 10 11 8 9 12 4 17 13 9
A C D E B F 16 5 2 15 11 8 12 17 13 9
PrimMST(G, s): Input: G, Graph; s, vertex in G, starting vertex Output: T, a minimum spanning tree (MST) of G foreach (Vertex v : G): d[v] = +inf p[v] = NULL d[s] = 0 PriorityQueue Q // min distance, defined by d[v] Q.buildHeap(G.vertices()) Graph T // "labeled set" repeat n times: Vertex m = Q.removeMin() T.add(m) foreach (Vertex v : neighbors of m not in T): if cost(v, m) < d[v]: d[v] = cost(v, m) p[v] = m return T 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
PrimMST(G, s): foreach (Vertex v : G): d[v] = +inf p[v] = NULL d[s] = 0 PriorityQueue Q // min distance, defined by d[v] Q.buildHeap(G.vertices()) Graph T // "labeled set" repeat n times: Vertex m = Q.removeMin() T.add(m) foreach (Vertex v : neighbors of m not in T): if cost(v, m) < d[v]: d[v] = cost(v, m) p[v] = m 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
Heap Unsorted Array
repeat n times: Vertex m = Q.removeMin() T.add(m) foreach (Vertex v : neighbors of m not in T): if cost(v, m) < d[v]: d[v] = cost(v, m) p[v] = m 16 17 18 19 20 21 22 repeat n times: Vertex m = Q.removeMin() T.add(m) foreach (Vertex v : neighbors of m not in T): if cost(v, m) < d[v]: d[v] = cost(v, m) p[v] = m 16 17 18 19 20 21 22
PrimMST(G, s): foreach (Vertex v : G): d[v] = +inf p[v] = NULL d[s] = 0 PriorityQueue Q // min distance, defined by d[v] Q.buildHeap(G.vertices()) Graph T // "labeled set" repeat n times: Vertex m = Q.removeMin() T.add(m) foreach (Vertex v : neighbors of m not in T): if cost(v, m) < d[v]: d[v] = cost(v, m) p[v] = m 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
Heap
O(n2 + m lg(n)) O(n lg(n) + m lg(n))
Unsorted Array
O(n2) O(n2)
Sparse Graph: Dense Graph:
O(n + m lg(n))
when running an MST algorithm?
O(n lg(n) + m lg(n))
O(n + m lg(n))
O(n lg(n) + m lg(n))
O(m lg(n))
PrimMST(G, s): foreach (Vertex v : G): d[v] = +inf p[v] = NULL d[s] = 0 PriorityQueue Q // min distance, defined by d[v] Q.buildHeap(G.vertices()) Graph T // "labeled set" repeat n times: Vertex m = Q.removeMin() T.add(m) foreach (Vertex v : neighbors of m not in T): if cost(v, m) < d[v]: d[v] = cost(v, m) p[v] = m 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Binary Heap Fibonacci Heap Remove Min O( lg(n) ) O( lg(n) ) Decrease Key O( lg(n) ) O(1)*
What’s the updated running time?
Lab: Your final CS 225 lab is this week.
Final Exam: Final exams start on Reading Day (May 3)
Grades: There will be a “Pre-Final” grade update posted next week with all grades except your final.
A C D E B F G H 7 5 4 10 7 5 3 6 2 5 4 3
DijkstraSSSP(G, s): foreach (Vertex v : G): d[v] = +inf p[v] = NULL d[s] = 0 PriorityQueue Q // min distance, defined by d[v] Q.buildHeap(G.vertices()) Graph T // "labeled set" repeat n times: Vertex u = Q.removeMin() T.add(u) foreach (Vertex v : neighbors of u not in T): if _______________ < d[v]: d[v] = __________________ p[v] = m 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
A C D E B F G H 7 5 4 10 7
3
2 5 4 3
What about negative weight cycles?
A C D E B F G H 7 5 4 10 7 5 3 6
2 3 3
What about negative weight edges, without negative weight cycles?
DijkstraSSSP(G, s): foreach (Vertex v : G): d[v] = +inf p[v] = NULL d[s] = 0 PriorityQueue Q // min distance, defined by d[v] Q.buildHeap(G.vertices()) Graph T // "labeled set" repeat n times: Vertex u = Q.removeMin() T.add(u) foreach (Vertex v : neighbors of u not in T): if _______________ < d[v]: d[v] = __________________ p[v] = m 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
What is the running time?