Download presentation
Presentation is loading. Please wait.
Published byฮฮฑฮธฮฑฮฝฮฑฮฎฮป ฮฮณฮณฮตฮปฮฟฯฮฟฯฮปฮฟฯ Modified over 6 years ago
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.