forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogressive_set_intersection.py
More file actions
47 lines (35 loc) · 1.45 KB
/
progressive_set_intersection.py
File metadata and controls
47 lines (35 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""Progressive multi-set intersection optimized for imbalanced sets."""
from typing import Set
def progressive_set_intersection(*sets: Set) -> 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