|
|
For a moment, let us step aside from UnionFind concept. Imagine you had a total of N=16 items. How many times can we halve these items until we reach a single item ? 1 : 16 / 2 = 8 2 : 8 / 2 = 4 3 : 4 / 2 = 2 4 : 2 / 2 = 1 From the abstract above, we see that we can halve these 16 items exactly 4 times, or the lg 16 = 4. This is the very definition of logarithm with base 2. In video materials, reverse argument is applied. How many times can we double items, starting from 1, until we reach N. As shown, we can only do this lgN times. |
In video materials, reverse argument is applied. How many times can we double items, starting from 1, until we reach N. As shown, we can only do this lgN times. |
but when we merge tree 1 with tree 2 we know for an indisputable fact that tree 2 must be the same size or bigger than tree 1 |
the proposition is prove that any node x's depth is at most log(base 2) N |
6 3 9 2 4 8 11 1 5 7 10 12 |
but when we merge tree 1 with tree 2 we know for an indisputable fact that tree 2 must be the same size or bigger than tree 1 Why? |
Propositon: Depth of node X increases by 1 when tree T1 containing x is merged into another tree T2. Consider following two trees: The very idea behind the algorithm is to merge smaller tree into larger tree. Why ? Well, all nodes in tree getting merged will now receive a new root, effectively increasing cost to reach them by 1. As you can see, before merging, we only needed one hop to reach 5 from 4. However, after merging sub-trees of 1 and 4, depth of nodes 4 and 5 increased by 1, now we need 2 jumps to reach node 5. Propositon: The size of the tree containing x at least doubles since | T 2 | ≥ | T 1 | When two trees are merged together, the size of a tree containing X will at least double with respect to size of the tree before merging. This one should be obvious, we are merging smaller tree into larger which has at least as many items as smaller tree. Propositon: Size of tree containing x can double at most lg N times. Why? You can find answer to the why in video materials but not in slides For a moment, let us step aside from UnionFind concept. Imagine you had a total of N=16 items. How many times can we halve these items until we reach a single item ? From the image above, we see that we can halve these 16 items exactly 4 times, or the lg 16 = 4. This is the very definition of logarithm with base 2. In video materials, reverse argument is applied. How many times can we double items, starting from 1, until we reach N. As shown, we can only do this lgN times. Putting it together Initially, each node is a root for itself. When we merge a node with another, we will increase depth of a node by 1 in merged tree. Since we can only merge a single node lgN times, we can also increase size only up to lgN times. Hope this helps |
|
|
|
|
|
|
if you have an unbalanced tree, all bets are off 1 2 3 4 5 6 7 8 9 10 //under 7 |
Depth of node X increases by 1 when tree T1 containing x is merged into another tree T2. |
Tree A Tree B 8 4 / \ 2 6 /\ /\ 1 3 5 7 |
4 / \ 2 6 /\ /\ 1 3 5 7 \ 8 |
RUNNING TIME find : takes time proportional to depth of p and q union : takes constant time given roots PROPOSITION - Depth of any node x is at most lg N ( log base 2(N) ) PROOF : when does depth of x increase? Increase by 1 when tree t1 containing x is merged with another tree t2 - The size of the tree containing x at least doubles since t2 >= t1 - Size of tree containing x can double at most lg N times. why? |
|
|
this one is simple :) the smallest base you can use in a proper tree is 2 (1 is a linked list of one child each, and 0 is just 1 data element no children). it could triple, but it at least doubles. Every time you add 1 depth to a tree, its maximum capacity (without increase of depth again) is increased this way. so if you have 5 children and have a root node, 5 to the 0 is 1 node. if you up the depth, its new max capacity is 6: add 5 to the 1 power. increase depth again, and you added 25 (more than double, its base 5 here). Adding depth to unbalanced trees is meaningless for talking about such things, see the linked list example again. |
Propositon 1: Depth of node X increases by 1 when tree T1 containing x is merged into another tree T2. |
Propositon 2: The size of the tree containing x at least doubles since | T 2 | ≥ | T 1 | |
N3 = N1 + N2 N1 <= N2 (because we merge the smaller into the larger) N1+N1 <= N1 + N2 (add N1 to both sides) 2*N1 <= N3 (N3 is at least twice the size of N1) |
Propositon 3 Size of tree containing x can double at most lg N times. |
N <= N1*2lg(N) N <= N1*N 1 <= N1 |