DATA STRUCTURES AND ALGORITHMS CS 3139
Lecture: April 8st, 1999
ANNOUNCEMENTS
HW4 out on web, due 04/20/1999 in class
Reading - Chapter 9
REVIEW
Disjoint set - solving equivalence problem
What is equivalence problem? Given equivalence relation ~ defined
on set S, a, bES, a~b?
What is equivalence relation?
-
Reflexive aRa
-
Symmetric aRb iff bRa
-
Transitive aRb iff bRc
Strategy - Define equivalence class a~b if a, b belong to same equivalence
class
Approach
-
Elements all suparate disjoint set
-
State equivalencee ==> union
-
Do find - find(a) == find(b)? a~b?
Idea
-
Use trees to represent sets
-
Root is used to name set
-
Only need parent info -> can use an array
-
S[i] = parent of element i = -1 if i == root (special case)
TODAY
Implement unions (find)
Smarter algorithm
Running time
Example
Union by size - smaller tree subtree of larger tree, S[root] = -size
[ We provided the code to implement this algorithm, as went through
an illustrative example ]
Alternative - union by height
-
Make shallower tree subtree of larger tree
-
Height only changes when 2 equally deep trees joined
-
Store 1-height in root
Shorter way Number 2
-
Path compression
-
When do find(x), tough call nodes from x to root
-
Change every nodes parent to be root
-
We need to modify find
[ We provided the code to implement this algorithm, as went through an
illustrative example ]
OPTIMIZATIONS
Union by size
Union by height
Path compression
Can we combine these methods?
-
Union by size and path compression compatable
-
Union by height and path compressino not compatable because we change height
Union by rank - Use height as estimate
ANALYSIS
Analysis of union/find algorithm
-
Hard
-
Algorithm almost linear (O(M)) in worst case
-
Specifically O(M*p*(M,N)) where p*(M,N) is the functional inverse
of Akerman's function
Akerman's Function
-
A(i,j) = 2^j for j>=1
-
A(i,1) = A(i-1,2) for i>=2
-
A(i,j) = A(i-1, A(i,j-1)) for i,j >= 2
[ We went through an example that illustrates Akerman's function ]
Grows very fast, thus inverse grows very slowly
p*(M,N) grows slower than log*N, where log*N
is the number of times you log N until N<=1
-
log*(2^6536) = 5, even though it is a 20,000 digit number, but
p*(M,N) grows slower
BUILDING MAZES
-
A by B maze
-
Each cell is an element
-
Initially walls between all elements
-
Randomly break down walls - union
-
Stop when find(first) = find(last)
Nhat Minh Dau, nmd13@columbia.edu