Lessons 16–20 · About 60 min per lesson
Disjoint sets and connectivity
Handle connectivity and component sizes as edges are added.
Lesson breakdown
- Lesson 16
Start with one set per vertex and initialise parent pointers.
- Lesson 17
Implement find to locate a representative through parent pointers.
- Lesson 18
Merge by size and compress paths during lookups.
- Lesson 19
Track component count and recognise a redundant union.
- Lesson 20
Sort edges by weight and use disjoint sets to choose edges for a minimum spanning tree.
Worked example
With 4 nodes, add edges 1–2, 2–3 and 1–3. How many components remain after each?
How to solve it
The counts are 3, 2, 2. The third edge joins vertices already in the same component.
Assignment
After each edge insertion, report the component count and maximum component size. Allow repeated edges and self-loops.
Check your result
The example gives (3,2), (2,3), (2,3); adding 4–4 leaves (2,3).