cs 225
play

CS 225 Data Structures Dec Decem ember er 2 2 Di Dijkstras Al - PowerPoint PPT Presentation

CS 225 Data Structures Dec Decem ember er 2 2 Di Dijkstras 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


  1. CS 225 Data Structures Dec Decem ember er 2 2 – Di Dijkstra’s Al Algorihtm G G Carl Evans

  2. Gr Grap aphs To study all of these structures: 1. A common vocabulary 2. Graph implementations 3. Graph traversals 4. Graph algorithms

  3. 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 )

  4. 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:

  5. 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

  6. MS MST Al Algorithm Ru Runtime mes: • Prim’s Algorithm: • Kruskal’s Algorithm: O(n lg(n) + m lg(n)) O(m lg(n))

  7. 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))

  8. 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

  9. MP MP7 Extra Credit: Due Tomorrow! (+7 points!)

  10. Sh Short ortest P Path

  11. 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

  12. 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

  13. 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

  14. 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