CS 225 Data Structures Dec Decem ember er 2 2 – Di Dijkstra’s Al Algorihtm G G Carl Evans
Gr Grap aphs To study all of these structures: 1. A common vocabulary 2. Graph implementations 3. Graph traversals 4. Graph algorithms
MS MST Algori orithm m Ru Runtime me: We know that MSTs are always run on a minimally connected graph: n-1 ≤ m ≤ n(n-1) / 2 O(n) ≤ O(m) ≤ O(n 2 )
MS MST Algori orithm m Ru Runtime me: • Prim’s Algorithm: • Kruskal’s Algorithm: O(n lg(n) + m lg(n)) O(n + m lg(n)) Sparse Graph: Sparse Graph: Dense Graph: Dense Graph:
Su Suppos ose I I h have a a n new h heap: Binary Heap Fibonacci Heap O( lg(n) ) O( lg(n) ) Remove Min O( lg(n) ) O(1)* Decrease Key What’s the updated running time? PrimMST(G, s): 6 foreach (Vertex v : G): 7 d[v] = +inf 8 p[v] = NULL 9 d[s] = 0 10 11 PriorityQueue Q // min distance, defined by d[v] 12 Q.buildHeap(G.vertices()) 13 Graph T // "labeled set" 14 15 repeat n times: 16 Vertex m = Q.removeMin() 17 T.add(m) 18 foreach (Vertex v : neighbors of m not in T): 19 if cost(v, m) < d[v]: 20 d[v] = cost(v, m) 21 p[v] = m
MS MST Al Algorithm Ru Runtime mes: • Prim’s Algorithm: • Kruskal’s Algorithm: O(n lg(n) + m lg(n)) O(m lg(n))
Final Final Big ig-O O MST Al Algorithm Ru Runtime mes: • Prim’s Algorithm: • Kruskal’s Algorithm: O(n lg(n) + m) O(m lg(n))
Post Break Grade CDF 100.00% 90.00% 80.00% 70.00% Students 60.00% 50.00% 40.00% 30.00% 20.00% 10.00% 0.00% 0.00% 10.00% 20.00% 30.00% 40.00% 50.00% 60.00% 70.00% 80.00% 90.00% 100.00% Course Grade (of 735) w/o EC
MP MP7 Extra Credit: Due Tomorrow! (+7 points!)
Sh Short ortest P Path
Dijkstra’ Di a’s A Algorithm ( (SSSP) P) DijkstraSSSP(G, s): 6 foreach (Vertex v : G): 7 d[v] = +inf 8 p[v] = NULL 9 d[s] = 0 10 11 PriorityQueue Q // min distance, defined by d[v] 12 Q.buildHeap(G.vertices()) 13 Graph T // "labeled set" 14 7 B 15 repeat n times: 10 C 16 Vertex u = Q.removeMin() A 5 17 T.add(u) 18 foreach (Vertex v : neighbors of u not in T): 6 4 3 D 19 if _______________ < d[v]: 3 20 d[v] = __________________ E 7 H 21 p[v] = m 5 2 5 4 F G
Di Dijkstra’ a’s A Algorithm ( (SSSP) P) What about negative weight cycles? 7 B 10 C A 5 -6 4 3 D 3 E 7 H 5 2 -5 4 F G
Di Dijkstra’ a’s A Algorithm ( (SSSP) P) What about negative weight edges, without negative weight cycles? 7 B 10 C A 5 6 4 3 D 3 E 7 H 2 -2 5 3 F G
Di Dijkstra’ a’s A Algorithm ( (SSSP) P) What is the running time? DijkstraSSSP(G, s): 6 foreach (Vertex v : G): 7 d[v] = +inf 8 p[v] = NULL 9 d[s] = 0 10 11 PriorityQueue Q // min distance, defined by d[v] 12 Q.buildHeap(G.vertices()) 13 Graph T // "labeled set" 14 15 repeat n times: 16 Vertex u = Q.removeMin() 17 T.add(u) 18 foreach (Vertex v : neighbors of u not in T): 19 if _______________ < d[v]: 20 d[v] = __________________ 21 p[v] = m
Recommend
More recommend