Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions data_structures/disjoint_set/progressive_set_intersection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""Progressive multi-set intersection optimized for imbalanced sets."""

from typing import Set

Check failure on line 3 in data_structures/disjoint_set/progressive_set_intersection.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (UP035)

data_structures/disjoint_set/progressive_set_intersection.py:3:1: UP035 `typing.Set` is deprecated, use `set` instead

Check failure on line 3 in data_structures/disjoint_set/progressive_set_intersection.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (UP035)

data_structures/disjoint_set/progressive_set_intersection.py:3:1: UP035 `typing.Set` is deprecated, use `set` instead


def progressive_set_intersection(*sets: Set) -> Set:

Check failure on line 6 in data_structures/disjoint_set/progressive_set_intersection.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (UP006)

data_structures/disjoint_set/progressive_set_intersection.py:6:49: UP006 Use `set` instead of `Set` for type annotation help: Replace with `set`

Check failure on line 6 in data_structures/disjoint_set/progressive_set_intersection.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (UP006)

data_structures/disjoint_set/progressive_set_intersection.py:6:41: UP006 Use `set` instead of `Set` for type annotation help: Replace with `set`

Check failure on line 6 in data_structures/disjoint_set/progressive_set_intersection.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (UP006)

data_structures/disjoint_set/progressive_set_intersection.py:6:49: UP006 Use `set` instead of `Set` for type annotation help: Replace with `set`

Check failure on line 6 in data_structures/disjoint_set/progressive_set_intersection.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (UP006)

data_structures/disjoint_set/progressive_set_intersection.py:6:41: UP006 Use `set` instead of `Set` for type annotation help: Replace with `set`
"""
Compute the intersection of multiple sets efficiently.

This function sorts the input sets by size (smallest first) and
progressively intersects them. It includes early termination when
the result becomes empty, which is very effective when dealing with
many sets or highly imbalanced sizes (e.g., one small set + many large ones).

Python's built-in `set.intersection(*sets)` is already optimized in C,
but this implementation demonstrates the "smallest-first + prune early"
heuristic for educational purposes.

Time Complexity: Better than naive in practice due to early pruning.

Examples:
>>> progressive_set_intersection({1, 2, 3}, {2, 3, 4}, {2, 5})
{2}
>>> progressive_set_intersection({1, 2}, {3, 4})
set()
>>> progressive_set_intersection({10, 20, 30})
{10, 20, 30}
>>> progressive_set_intersection()
set()
"""
if not sets:
return set()

if len(sets) == 1:
return sets[0].copy()

# Sort by length (smallest first) for optimal pruning
sorted_sets = sorted(sets, key=len)

result = sorted_sets[0].copy()

for current_set in sorted_sets[1:]:
if not result:
return set()
result &= current_set # Efficient in-place intersection

return result
Loading