coursera algorithms 1
Dynamic Connectivity: quick union
"lazy approach" Data structure: - Integer array id[] of length N - Interpretation: id[i] is parent of i - Root of i is id[id[id...id[i]...]]] ( Keep going until id doesnt change (this ensures no cycles) 0:0, 1:1, 2:9, 3:4, 4:9, 5:6, 6:6, 7:7, 8:8, 9:9
How many connected components result after performing the following sequence of union operations on a set of 10 items? 1-2 3-4 5-6 7-8 7-9 2-8 0-5 1-9
3. The connected components are {0,5,6}, {3,4}, and {1,2,7,8,9}.
Dynamic Connectivity: find query
Check if two objects are in the same component.
Dynamic connectivity: Weighted quick Union
Data structure: same as quick-union, but maintain extra array sz[i] to count number of objects in the tree rooted at i Find: Identical to quick union Union: - Link root of smaller tree to root of larger tree - update sz[] array - Modify quick union to avoid tall trees - Keep track of size of each tree (number of objects) - Balance by linking root of smaller tree to root of larger tree( reasonable alternatives: union by height or "rank).
Dynamic Connectivity: quick find
Eager Approach Data structure: - Integer array id[] of length N. - Interpretation: p and q are connected iff they have the same id. Find. Check if p and q have the same id. Union. To merge components containing p and q, change all entries whose id equals id[p] to id[q] 0:0, 1:1, 2:1, 3:8, 4:8, 5:0, 6:0, 7:1, 8:8, 9:8
Dynamic Connectivity Problem
Given a set of points, some connected and some not, what is the path to get from point p to point q? Model of the problem for Union Find.
Transitive Property
If a=b and b=c, then a=c
Dynamic Connectivity: connected components
Maximal set of objects that are mutually connected e.g. { 0 } { 1 4 5 } { 2 3 6 7 } <- 3 connected components
Algorithm
Method to Solve a problem
Data Structure
Method to store information
Steps to developing a usable algorithm
Model the problem. Find an algorithm to solve it. Fast enough? Fits in memory? If not, figure out why. Find a way to address the problem. Iterate until satisfied
Dynamic Connectivity: Quick Find Defects
Number of array accesses (for read or write) is too many. Union is too expensive. It takes N^2 array accesses to process a sequence of N union commands on N objects. Trees are flat, but too expensive to keep them flat. order of growth of number of array accesses... initialize: N union: N find: 1
Name some applications of the dynamic connectivity problem
Pixels in a digital photo. Computers in a network. Friends in a social network. Transistors in a computer chip. Elements in a mathematical set. Variable names in Fortran program. Metallic sites in a composite system.
Union command.
Replace components containing two objects with their union.
Do quadratic algorithms scale? Explain why.
Rough standard (for now) 10^9 operations per second, and 10^9 words of main memory. Therefore, you can all words in approximately 1 second. Ex. Huge problem for quick-find. 10^9 union commands on 10^9 objects. Quick-find takes more than 10^18 operations. 30+ years of computer time.
Dynamic Connectivity: Weighted quick union analysis. Cite proof.
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 Pf: When does depth of x increase? Increase by 1 when T1 containing x is merged into another Tree T2. - The size of the tree containing x at lease doubles since |T2| >= |T1| - Size of tree containing x can double at most lg N times.
Dynamic Connectivity: quick union, union operation
To merge components containing p and q, set the id of p's root to the id of q's root.
Dynamic Connectivity: quick union defects
Trees can get tall find too expensive (could be N array accesses)
Reflexive Property
a=a
lg N
base-2 logarithm
Symmetric Property
if a=b, then b=a