cs 401
play

CS 401 Dijkstras Algorithm / Minimum Spanning Tree Xiaorui Sun 1 - PowerPoint PPT Presentation

CS 401 Dijkstras Algorithm / Minimum Spanning Tree Xiaorui Sun 1 Single Source Shortest Path Given an (un)directed connected graph ! = ($, &) with non- negative edge weights ( ) 0 and a start vertex , . Find length of shortest paths


  1. CS 401 Dijkstra’s Algorithm / Minimum Spanning Tree Xiaorui Sun 1

  2. Single Source Shortest Path Given an (un)directed connected graph ! = ($, &) with non- negative edge weights ( ) ≥ 0 and a start vertex , . Find length of shortest paths from , to each vertex in ! length of path = sum of edge weights in path 23 2 3 9 s Cost of path s-2-3-4-t 18 14 6 = 9 + 23 + 6 + 6 2 6 = 44. 30 4 19 11 5 15 5 6 20 16 t 7 44

  3. Dijkstra’s Algorithm Dijkstra( !, #, $ ) { Initialize set of explored nodes % ← {$} // Maintain distance from $ to each vertices in % ) $ ← * while ( % ≠ , ) { Pick an edge (., /) such that . ∈ % and / ∉ % and )[.] + # (.,/) is as small as possible. Add / to % and define )[/] = )[.] + # (.,/) . 789:;< / ← . . } Theorem: ) / is the length of shortest path from $ to /

  4. Remarks on Dijkstra’s Algorithm Algorithm produces a tree of shortest paths to ! following • Parent links (for undirected graph) • Algorithm works on directed graph (with nonnegative weights) • The algorithm fails with negative edge weights. • Why does it fail?

  5. Implementing Dijkstra’s Algorithm Trivial implementation Dijkstra( !, #, $ ) { Initialize set of explored nodes % ← {$} // Maintain distance from $ to each vertices in % ) $ ← * while ( % ≠ , ) O ( m ) { time Pick an edge (., /) such that . ∈ % and / ∉ % and )[.] + # (.,/) is as small as possible. O ( n ) O ( 1 ) iterations time Add / to % and define )[/] = )[.] + # (.,/) . 789:;< / ← . . } Overall: O ( nm ) time

  6. Implementing Dijkstra’s Algorithm Better idea: for each ! ∉ # , maintain '∈): ',, ∈- .[0] + 3 (0,!) min 0 s 2 4 2 4 5 7 3 9 1 4 7 5 6 3 5 15 8 8 1 9 13 8 ¥ 16 4 10 2 5 10 ¥

  7. Implementing Dijkstra’s Algorithm Better idea: for each ! ∉ # maintain '∈): ',, ∈- .[0] + 3 (0,!) min Dijkstra( 6, 3, 7 ) { Initialize set of explored nodes # ← {7} // Maintain distance from 7 to each vertices in # . 7 ← ; while ( # ≠ = ) O ( n ) { time Pick an edge (0, !) such that 0 ∈ # and ! ∉ # and .[0] + 3 (0,!) is as small as possible. O ( 1 ) O ( n ) time Add ! to # and define .[!] = .[0] + 3 (0,!) . iterations ?@ABCD ! ← 0 . Overall: E∈): E,F ∈- .[@] + 3 (@,G) for each G ∉ # min Maintain O ( n 2 +m ) O ( deg(u) ) } time time

  8. Implementing Dijkstra’s Algorithm Priority Queue: Elements each with an associated key Operations • Insert • Find-min – Return the element with the smallest key • Delete-min – Return the element with the smallest key and delete it from the data structure • Decrease-key – Decrease the key value of some element Implementations Arrays: ,∈.: ,,1 ∈2 3[5] + 8 (5,9) min maintain !(#) time find/delete-min, • !(1) time insert/decrease key • for each 9 ∉ ; Binary Heaps: !(log #) time insert/decrease-key/delete-min, • !(1) time find-min • Fibonacci heap: Read wiki! !(1) time insert/decrease-key • !(log #) delete-min • • O(1) time find-min

  9. Dijkstra( !, #, $ ) { Initialize set of explored nodes % ← {$} @(A) of insert, // Maintain distance from $ to each vertices in % each in @(1) ) $ ← * Insert all neighbors + of s into a priority queue with value # ($,+) . while ( % ≠ / ) { Pick an edge (0, +) such that 0 ∈ % and + ∉ % and )[0] + # (0,+) is as small as possible. @(A) of delete min, v ¬ delete min element from 6 each in @(log A) Add + to % and define )[+] = )[0] + # (0,+) . 89:;<= + ← 0 . @(F) of decrease/insert key, foreach (edge ; = (+, >) incident to + ) each runs in @(1) if ( > ∉ % ) if ( > is not in the 6 ) Insert > into 6 with value ) + + # (+,>) else (the key of > > ) + + # (+,>) ) Overall: O (( nlog n)+m ) Decrease key of ? to )[+] + # (+,>) . time }

  10. Minimum Spanning Tree 10

  11. Spanning Tree Given a connected undirected graph ! = #, % . We call & is a spanning tree of ! if • All edges in & are from % . • & includes all of the vertices of ! . & ! 11

  12. Minimum Spanning Tree (MST) Given a connected undirected graph ! = ($, &) with real- valued edge weights ( ) ≥ 0 . An MST , is a spanning tree whose sum of edge weights is minimized. 4 24 4 9 23 6 9 6 18 5 5 11 11 16 8 8 7 7 14 10 21 ! = ($, &) ( , = - ( ) = 50 )∈/ 12

  13. Kruskal’s Algorithm [1956] Kruskal(G, c) { Sort edges weights so that ! " ≤ ! $ ≤ ⋯ ≤ ! & . ' ← ∅ foreach ( * ∈ , ) make a set containing singleton {*} for / = " to & Let *, 2 = 3 / if ( * and 2 are in different sets) { ' ← ' ∪ {3 / } merge the sets containing * and 2 } return ' } Kruskal Sort edges weight. Add edges whenever it does not create cycle.

  14. Cuts S V-S In a graph ! = ($, &) , a cut is a bipartition of V into disjoint sets (, $ − ( for some ( ⊆ $. We denote it by ((, $ − () . An edge , = {., /} is in the cut ((, $ − () if exactly one of ., / is in ( . x v u V-S S 14

  15. Properties of the OPT Simplifying assumption: All edge costs ! " are distinct. Cut property: Let # be any subset of nodes (called a cut), and let $ be the min cost edge with exactly one endpoint in # . Then every MST contains $ . Cycle property. Let % be any cycle, and let & be the max cost edge belonging to % . Then no MST contains & . 10 7 7 S V-S 3 2 4 5 5 red edge is in the MST Green edge is not in the MST 15

  16. Cut Property: Proof Simplifying assumption: All edge costs ! " are distinct. Cut property. Let # be any subset of nodes, and let $ be the min cost edge with exactly one endpoint in # . Then any MST % ∗ contains $ . Proof. By contradiction Suppose $ = {), +} does not belong to % ∗ . There is a path from ) to + in % ∗ Þ there exists another edge, say - , that leaves # . Adding $ to % ∗ creates a cycle . in % ∗ . (coz all tree has 4 − 1 edges) % = % ∗ È {$} − {-} is also a spanning tree. Since ! " < ! 1 , ! % < !(% ∗ ) . f This is a contradiction. S e v u 17 T*

  17. Cycle Property: Proof Simplifying assumption: All edge costs ! " are distinct. Cycle property: Let # be any cycle in $ , and let % be the max cost edge belonging to # . Then the MST & ∗ does not contain % . Proof. By contradiction Every connected graph has a spanning tree. Hence it has at least 1 − 1 edges. Suppose % belongs to & ∗ . Deleting % from & ∗ cuts & ∗ into two connected components. There exists another edge, say ( , that is in the cycle and connects the components. & = & ∗ È {(} − {%} is also a spanning tree. Since ! " < ! . , ! & < !(& ∗ ) . f This is a contradiction. S e 18 T*

  18. Proof of Correctness (Kruskal) Consider edges in ascending order of weight. Case 1: adding ! to " creates a cycle, ! is the maximum weight edge in that cycle. cycle property show ! is not in any minimum spanning tree. Case 2: ! = (%, ') is the minimum weight edge in the cut ) where ) is the set of nodes in % ’s connected component. So, ! is in all minimum spanning tree. v S e u ! Case 2 Case 1 This proves MST is unique if weights are distinct.

  19. Summary • Greedy algorithm: ‘Best’ current partial solution at each step • Design greedy algorithm: How to order your input Strategy for every step • Greedy Analysis Strategies Greedy algorithm stays ahead Structural Exchange argument 20

Recommend


More recommend