cs 758 858 algorithms
play

CS 758/858: Algorithms http://www.cs.unh.edu/~ruml/cs758 - PowerPoint PPT Presentation

CS 758/858: Algorithms http://www.cs.unh.edu/~ruml/cs758 Topological Sorting Union-Find Wheeler Ruml (UNH) Class 14, CS 758 1 / 14 Topological Sorting The Problem Break Union-Find Topological Sorting Wheeler Ruml (UNH) Class


  1. CS 758/858: Algorithms http://www.cs.unh.edu/~ruml/cs758 Topological Sorting Union-Find Wheeler Ruml (UNH) Class 14, CS 758 – 1 / 14

  2. Topological Sorting ■ The Problem ■ Break Union-Find Topological Sorting Wheeler Ruml (UNH) Class 14, CS 758 – 2 / 14

  3. The Problem Given a set of pairwise orderings a ≺ b , find an ordering of all Topological Sorting the elements that respects them or detect that no such ordering ■ The Problem ■ Break is possible. Union-Find How long does this take? Wheeler Ruml (UNH) Class 14, CS 758 – 3 / 14

  4. Break asst 8 ■ Topological Sorting asst 9 ■ The Problem ■ ■ Break Union-Find Wheeler Ruml (UNH) Class 14, CS 758 – 4 / 14

  5. Topological Sorting Union-Find ■ Components ■ Union-Find ADT ■ Algorithm ■ Disjoint Sets ■ Speed-Ups ■ Pseudo-code ■ More Pseudo-code Union-Find ■ Strongly ■ EOLQs Wheeler Ruml (UNH) Class 14, CS 758 – 5 / 14

  6. Connected Components Problem: find components in an undirected graph and answer Topological Sorting membership queries Union-Find ■ Components ■ Union-Find ADT Two cases: static vs dynamic ■ Algorithm ■ Disjoint Sets ■ Speed-Ups How can we identify components in the static case? ■ Pseudo-code ■ More Pseudo-code ■ Strongly ■ EOLQs Wheeler Ruml (UNH) Class 14, CS 758 – 6 / 14

  7. Union-Find ADT Make-Set ( x ) makes new set containing x Topological Sorting Union-Find Union ( x, y ) combine the set containing x with the set ■ Components containing y ■ Union-Find ADT ■ Algorithm ■ Disjoint Sets Find-Set ( x ) return a representative of the set containing x ■ Speed-Ups ■ Pseudo-code ■ More Pseudo-code ■ Strongly ■ EOLQs Wheeler Ruml (UNH) Class 14, CS 758 – 7 / 14

  8. Connected Components Algorithm find-components Topological Sorting Union-Find 1. foreach vertex v ■ Components 2. Make-Set ( v ) ■ Union-Find ADT ■ Algorithm 3. for each edge ( u, v ) ■ Disjoint Sets 4. Union ( u , v ) ■ Speed-Ups ■ Pseudo-code ■ More Pseudo-code ■ Strongly in-same-component? ( u , v ) ■ EOLQs 5. is Find-Set ( u ) = Find-Set ( v )? Wheeler Ruml (UNH) Class 14, CS 758 – 8 / 14

  9. Disjoint Sets set is a tree rooted at representative Topological Sorting Union-Find ■ Components How to implement make, union, find? ■ Union-Find ADT ■ Algorithm ■ Disjoint Sets ■ Speed-Ups ■ Pseudo-code ■ More Pseudo-code ■ Strongly ■ EOLQs Wheeler Ruml (UNH) Class 14, CS 758 – 9 / 14

  10. Speed-Ups union by rank track approximate height, put shorter under Topological Sorting taller Union-Find ■ Components path compression after Find-Set , ensure touched nodes ■ Union-Find ADT point directly to root ■ Algorithm ■ Disjoint Sets ■ Speed-Ups ■ Pseudo-code ■ More Pseudo-code ■ Strongly ■ EOLQs Wheeler Ruml (UNH) Class 14, CS 758 – 10 / 14

  11. Pseudo-code Make-Set ( x ) Topological Sorting Union-Find 1. x.p ← x ■ Components 2. x .rank ← 0 ■ Union-Find ADT ■ Algorithm ■ Disjoint Sets 3. Union ( x , y ) ■ Speed-Ups ■ Pseudo-code 4. x ← Find-Set ( x ) ■ More Pseudo-code ■ Strongly 5. y ← Find-Set ( y ) ■ EOLQs 6. if x.rank > y.rank 7. y.p ← x 8. else 9. x.p ← y 10. if x.rank = y.rank 11. increment y.rank Wheeler Ruml (UNH) Class 14, CS 758 – 11 / 14

  12. More Pseudo-code Find-Set ( x ) Topological Sorting Union-Find 1. if x � = x.p ■ Components 2. x.p ← Find-Set ( x.p ) ■ Union-Find ADT ■ Algorithm 3. return x.p ■ Disjoint Sets ■ Speed-Ups ■ Pseudo-code ■ More Pseudo-code For m operations on n sets, worst-case time is O ( mα ( n )) . ■ Strongly ■ EOLQs α ( n ) is inverse of Ackermann’s function. It is ≤ 4 if n ≤ 2 2048 = 16 512 . Wheeler Ruml (UNH) Class 14, CS 758 – 12 / 14

  13. Strongly-Connected Components G T = G but with reversed arcs Topological Sorting Union-Find 1. DFS( G ), recording finishing times. ■ Components DFS( G T ), starting from vertices with higher finishing times 2. ■ Union-Find ADT ■ Algorithm first (in outer loop) ■ Disjoint Sets 3. each tree in second DFS is a SCC ■ Speed-Ups ■ Pseudo-code ■ More Pseudo-code let f ( C ) be max of any finishing time in C ■ Strongly G and G T have same SSCs. ■ EOLQs ■ If G has an arc from some u ∈ C i to some v ∈ C j , ■ f ( C i ) > f ( C j ) . If G has an arc from C i to C j , G T can’t have such an arc. ■ If there is an arc in G T from C j to C i , then according to ■ first DFS, f ( C i ) > f ( C j ) . When the second DFS is processing C j in G T , all vertices in ■ C i will already be finished. Wheeler Ruml (UNH) Class 14, CS 758 – 13 / 14

  14. EOLQs For example: Topological Sorting Union-Find What’s still confusing? ■ ■ Components What question didn’t you get to ask today? ■ ■ Union-Find ADT ■ Algorithm What would you like to hear more about? ■ ■ Disjoint Sets ■ Speed-Ups Please write down your most pressing question about algorithms ■ Pseudo-code and put it in the box on your way out. ■ More Pseudo-code ■ Strongly Thanks! ■ EOLQs Wheeler Ruml (UNH) Class 14, CS 758 – 14 / 14

Recommend


More recommend