Lecture 14: Disjoint Sets

9/28/2020

Intro to Disjoint Sets

Meta-goals of the Coming Lectures: Data Structure Refinement

The Disjoint Sets Data Structure

Disjoint Sets on Integers

The Disjoint Sets Interface

public interface DisjointSets { /** Connects two items P and Q */ void connect(int p, int q); /** Checks to see if two items are connected */ boolean isConnected(int p, int q); }

The Naive Approach

A Better Approach: Connected Components

Quick Find

Performance Summary

My Approach: Just use a list of integers

Quick Union

Improving the Connect Operation

Set Union Using Rooted-Tree Representation

Weighted Quick Union

Weighted Quick Union

Implementing WeightedQuickUnion

Weighted Quick Union Performance

Performance Summary

Why Weights Instead of Heights?

Path Compression (CS 170 Spoiler) (Can we do better?)

What We've Achieved

CS 170 Spoiler: Path Compression: A Clever Idea

A Summary of Our Iterative Design Process

Summary From Discussion

Disjoint Sets