Sei sulla pagina 1di 2

Sean Li CS 4820 Notes Spring 2013 Introduction to Design and Analysis of Algorithms Lecture 7 2/4/13 Ecient Implementation of Kruskals

ls Algorithm. Can be done in O(n(n)), where (n) is the inverse of Ackermanns function A(n). Since A(n) grows enormously fast, (n) is eectively bounded by 4. Need an ecient way to detect whether two nodes are in the same component. Want to have a data structure represent each component. This way, to test whether two nodes are in the same component, just need to test whether their sets are the same. Call this set f ind(x). Want to be able to quickly merge two sets, union(x1 , x2 ). Use auxiliary trees, which have nothing to do with the current trees. Each auxiliary tree represents a component. To check whether two elements are in the same component, check that they ahve the same root. To merge, just set the root of one tree to point to the other. Heuristic 1. In particular, merge the smaller tree into the bigger tree. This can be eciently checked by maintaining the size of the tree at the root. Lemma. For any tree of size n and height h, n 2h . Base step is true. Suppose we have trees t1 , t2 , with sizes s1 , s2 and heights h1 , h2 . Proof. Suppose s1 2h1 and s2 2h2 . Assume without loss of generality that s1 s2 , so that we merge t2 into t1 . Let t3 be the resulting tree. Case 1: h1 > h2 . Then h3 = h1 and we are done. Case 2: h1 = h2 . Then s3 = s1 + s2 2h1 + 2h2 = 2h1 +1 by the induction hypothesis. We are done here as well. Since log t1 log n, the algorithm is O(n log n). Heuristic 2: Path compression. After going once, each node of a tree points directly to the root. This gives O(n(n)). Ackermanns Function. Successor, addition, multiplication, exponentiation, etc. The function grows extremely fast, said Kozen, Even at n = 4 it is higher than Hopcrofts IQ. Proof of Kruskals Algorithm. By exchange. Suppose we have T from Kruskals algo-

rithm and T is an optimal tree. Order the edges in weight. Let e be the rst edge on which T and T dier. Then e could not have been taken by T and not by T , since if it did not form a cycle, Kruskals algorithm would have taken it. Hence e must be taken by T but not by T . Then the optimal tree must at some point connected the components connected by e, let this edge be e . Since the edges were sorted, this means w(e) w(e ), so Kruskals is not worse. Go through all the edges. If we assumed total ordering, the Kruskals tree is the optimal tree. With a partial order, there may be multiple optimal trees, and Kruskals gives one of them.

Page 2

Potrebbero piacerti anche