Presentation is loading. Please wait.

Presentation is loading. Please wait.

Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee

Similar presentations


Presentation on theme: "Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee"โ€” Presentation transcript:

1 Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
CSE 421 Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee

2 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 ๐บ. ๐‘‡ ๐บ

3 Why spanning tree? Many problems is easy for tree. General framework:
Approximate the graph by a tree. Solve the problem on a tree. We have covered different tree: BFS tree / Dijkstra tree Remember all shortest paths from ๐‘ . DFS tree (see Trรฉmaux tree in wiki) Every two adjacent vertices in G are related to each other as an ancestor and descendant in the tree There are many other different trees depending on applications.

4 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. 24 4 4 23 9 6 9 6 18 5 5 11 11 16 8 8 7 7 10 14 21 ๐‘ ๐‘‡ = ๐‘’โˆˆ๐‘‡ ๐‘ ๐‘’ =50 ๐บ = (๐‘‰, ๐ธ) See wiki for applications

5 Kruskalโ€™s Algorithm [1956]
Kruskal(G, c) { Sort edges weights so that ๐’„ ๐Ÿ โ‰ค ๐’„ ๐Ÿ โ‰คโ‹ฏโ‰ค ๐’„ ๐’Ž . ๐‘ปโ†โˆ… foreach (๐’–โˆˆ๐‘ฝ) make a set containing singleton {๐’–} for ๐’Š=๐Ÿ to ๐’Ž Let ๐’–,๐’— = ๐’† ๐’Š if (๐’– and ๐’— are in different sets) { ๐‘ปโ† ๐‘ปโˆช{ ๐’† ๐’Š } merge the sets containing ๐’– and ๐’— } return ๐‘ป Kruskal Prim add the cheapest edge from the tree to another vertex. Sort edges weight. Add edges whenever it does not create cycle. The proof is easier. Exercise.

6 S V-S Cuts In a graph ๐บ=(๐‘‰,๐ธ), a cut is a bipartition of V into disjoint sets ๐‘†,๐‘‰โˆ’๐‘† for some ๐‘†โŠ†๐‘‰. We show it by (๐‘†,๐‘‰โˆ’๐‘†). An edge ๐‘’={๐‘ข,๐‘ฃ} is in the cut (๐‘†,๐‘‰โˆ’๐‘†) if exactly one of ๐‘ข,๐‘ฃ is in ๐‘†. x v u S V-S

7 Green edge is not in the MST
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

8 Cycles and Cuts Claim. A cycle crosses a cut (from ๐‘† to ๐‘‰โˆ’๐‘†) an even number of times. Proof. (by picture) u S V - S C Every time the cycle crosses a cut, it goes from ๐‘† to ๐‘‰โˆ’๐‘† or from ๐‘‰โˆ’๐‘† to ๐‘†.

9 (coz all tree has ๐‘›โˆ’1 edges)
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 the ๐‘‡ โˆ— contains ๐‘’. Proof. By contradiction Suppose ๐‘’ = {๐‘ข,๐‘ฃ} does not belong to ๐‘‡ โˆ— . Adding ๐‘’ to ๐‘‡ โˆ— creates a cycle ๐ถ in ๐‘‡ โˆ— . There is a path from ๐‘ข to ๐‘ฃ in ๐‘‡ โˆ— ๏ƒž there exists another edge, say ๐‘“, that leaves ๐‘†. ๐‘‡ = ๐‘‡ โˆ— ๏ƒˆ {๐‘’} โˆ’ {๐‘“} is also a spanning tree. Since ๐‘ ๐‘’ < ๐‘ ๐‘“ , ๐‘ ๐‘‡ <๐‘( ๐‘‡ โˆ— ). This is a contradiction. (coz all tree has ๐‘›โˆ’1 edges) f S v u e T*

10 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 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 ๐‘ ๐‘’ < ๐‘ ๐‘“ , ๐‘ ๐‘‡ <๐‘( ๐‘‡ โˆ— ). This is a contradiction. Every connected graph has a spanning tree. Hence it has at least ๐‘›โˆ’1 edges. f S e T*

11 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 u Case 2 e S Case 1 ๐‘’ This proves MST is unique if weights are distinct.

12 Implementation: Kruskalโ€™s Algorithm
Implementation. Use the union-find data structure. Build set ๐‘‡ of edges in the MST. Maintain a set for each connected component. O(๐‘š logโก๐‘›) for sorting and O(๐‘š logโก๐‘›) for union-find Kruskal(G, c) { Sort edges weights so that ๐’„ ๐Ÿ โ‰ค ๐’„ ๐Ÿ โ‰คโ‹ฏโ‰ค ๐’„ ๐’Ž . ๐‘ปโ†โˆ… foreach (๐’–โˆˆ๐‘ฝ) make a set containing singleton {๐’–} for ๐’Š=๐Ÿ to ๐’Ž Let ๐’–,๐’— = ๐’† ๐’Š if (๐’– and ๐’— are in different sets) { ๐‘ปโ† ๐‘ปโˆช{ ๐’† ๐’Š } merge the sets containing ๐’– and ๐’— } return ๐‘ป

13 Union Find Data Structure
Each set is represented as a tree of pointers, where every vertex is labeled with longest path ending at the vertex To check whether A,Q are in same connected component, follow pointers and check if root is the same. D,2 W,1 A,1 B,0 P,0 Q,0 C,0 {W,P,Q} {A,B,C,D}

14 Union Find Data Structure
Merge: To merge two connected components, make the root with the smaller label point to the root with the bigger label (adjusting labels if necessary). Runs in ๐‘‚(1) time D,2 A,1 B,0 C,0 W,1 P,0 Q,0 D,2 W,1 A,1 B,0 P,0 Q,0 C,0 At most one label must be adjusted W,2 Q,0 D,1 A,0 W,1 Q,0 D,1 A,0

15 Depth vs Size Claim: If the label of a root is ๐‘˜, there are at least 2 ๐‘˜ elements in the set. Therefore, the depth of any tree in algorithm is at most log 2 โก๐‘› So, we can check if ๐‘ข,๐‘ฃ are in the same component in time ๐‘‚(log ๐‘›) D,2 A,1 B,0 C,0 W,1 P,0 Q,0

16 Depth vs Size: Correctness
Claim: If the label of a root is ๐‘˜, there are at least 2 ๐‘˜ elements in the set. Proof: By induction on ๐‘˜. Base Case (๐‘˜=0): this is true. The set has size 1. Inductive Step: If we merge roots with labels ๐‘˜ 1 > ๐‘˜ 2 , the number of vertices only increases while the label stays the same. If ๐‘˜ 1 = ๐‘˜ 2 , the merged tree has label ๐‘˜ 1 +1, and by induction, it has at least 2 ๐‘˜ ๐‘˜ 2 = 2 ๐‘˜ 1 +1 elements.

17 Kruskalโ€™s Algorithm with Union Find
Implementation. Use the union-find data structure. Build set ๐‘‡ of edges in the MST. Maintain a set for each connected component. O(m log n) for sorting and O(m log n) for union-find Kruskal(G, c) { Sort edges weights so that ๐’„ ๐Ÿ โ‰ค ๐’„ ๐Ÿ โ‰คโ‹ฏโ‰ค ๐’„ ๐’Ž . ๐‘ปโ†โˆ… foreach (๐’–โˆˆ๐‘ฝ) make a set containing singleton {๐’–} for ๐’Š=๐Ÿ to ๐’Ž Let ๐’–,๐’— = ๐’† ๐’Š if (๐’– and ๐’— are in different sets) { ๐‘ปโ† ๐‘ปโˆช{ ๐’† ๐’Š } merge the sets containing ๐’– and ๐’— } return ๐‘ป Find roots and compare Merge at the roots


Download ppt "Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee"

Similar presentations


Ads by Google