Lecture 2 - Notes
January 7, 2016
Encoding Trees
Mark internal nodes with ones and leaves with zeros. We then do a preorder traversal. This gives us a sequence, e.g.,
This tree sequence let's us create a well formed parenthesis string.
This parenthesis string is unique to that tree.
Union Find
Given a set of $N$ object we would like to,
- Connect two objects
- Test if theirs a path between two objects
To find out if a static graph is connected you could use DFS or BFS which is $O(|V| + |E|)$.
In this dynamic environment we are adding edges to the graph. We assume "is connected to" to be an equivalence relation. We will therefor use sets to encapsulate components, when creating edges we will check which set each node is in then join the two sets.
class UF
{
UF (int n)
void union(int p, int q)
int find(int p)
boolean connected(int p, int q)
}
Quick Find
Data structure
- Integer array
id[]
of lengthN
- Interpretation
id[p]
is the id of the component containingp
Example
0 1 2 3 4 5 6 7 8 9
id = [0,1,1,8,8,0,0,1,8,8]
So 0, 5 and 6 are connected, etc.
find(p)
: Lookupp
by indexconnected(p,q)
: Dop
andq
have the same id?union(p,q)
: Change all entries were id isid[p]
toid[q]
Quick Union
Data Structure
- Integer array
id[]
of lengthN
- Interpretation
id[i]
is the parent ofi
- Root of
i
isid[id[...id[i]...]]
0 1 2 3 4 5 6 7 8 9
id = [0,1,9,4,9,6,6,7,8,9]
find(p)
: What is the root ofp
connected(p,q)
: Dop
andq
have the same root?union(p,q)
: Set the id ofp
's root to the id ofq
's root
Issues
Both of the these operation are too expensive,
Algorithm | intialize | union | find | connected |
---|---|---|---|---|
Quick Find | $O(N)$ | $O(N)$ | $O(1)$ | $O(1)$ |
Quick Union | $O(N)$ | $O(N)$ | $O(N)$ | $O(N)$ |
Quick Find
- Unions are expensive
- Trees are flat but it's expensive to eep them flat
Quick Union
- Trees can get very tall
- Find/connect is expensive
Weighted Quick Union
- Modify Quick Union to avoid tall trees
- Keep track of the size of each tree
- Balance by linking smaller tree to root of larger tree
Data Structure
Same as Quick Union but maintain an extra array sz[i]
that counts the number of objects rooted at i
.
find(p)
/connected(p,q)
: Same as Quick UnionUnion(p,q)
: Link the smaller tree to the larger tree, updatesz[]
Efficiency
Algorithm | initialize | union | find | connected |
---|---|---|---|---|
Quick Find | $O(N)$ | $O(N)$ | $O(1)$ | $O(1)$ |
Quick Union | $O(N)$ | $O(N)$ | $O(N)$ | $O(N)$ |
Weighted Quick Union | $O(N)$ | $O(\lg{N})$ | $O(\lg{N})$ | $O(\lg{N})$ |