- Quick Find
- Quick Union
Steps to developing a useful 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.
This is a scientific method to designing and analyzing algorithms.
Given a set of N objects (doesn't matter what they are).
- Union Command Connect two objects.
- Find/connected query: Is there a path connecting the two objects? (even an indirect path)
Applications involve manipulating objects of all types like pixels, computers in a network, friends in a network, transistors in a chip, etc. When programming, it's convenient to name objects 0 to N - 1.
- Use integers as array index.
- Suppress details not relevant to the union-find.
This way we can access information quickly. We assume "is connected to" is an equivalence relation:
- Reflective:
p
is connected top
. - Symmetric: if
p
is connected toq
. thenq
is connected top
. - Transitive: if
p
is connected toq
andq
is connected tor
, thenp
is connected tor
.
These properties are intuitive but important.
A connected component is maximal set of objects that are mutually connected (groups).
Find query. Check if 2 objects are in the same component.
Union command. Replace components containing two objects with their union.