From 154d954141c8da8f2ccab1bd800fd39a18f475ea Mon Sep 17 00:00:00 2001 From: Devanik <162272415+Devanik21@users.noreply.github.com> Date: Wed, 1 Apr 2026 13:39:56 +0530 Subject: [PATCH 1/4] Implement progressive set intersection function This function computes the intersection of multiple sets efficiently by sorting them by size and using early termination. --- .../progressive_set_intersection.py | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 data_structures/disjoint_set/progressive_set_intersection.py diff --git a/data_structures/disjoint_set/progressive_set_intersection.py b/data_structures/disjoint_set/progressive_set_intersection.py new file mode 100644 index 000000000000..eb202d77c99d --- /dev/null +++ b/data_structures/disjoint_set/progressive_set_intersection.py @@ -0,0 +1,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 From 05d06f5af8cba32fddc94260961ea972564ee7b7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 1 Apr 2026 08:18:30 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/disjoint_set/progressive_set_intersection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/disjoint_set/progressive_set_intersection.py b/data_structures/disjoint_set/progressive_set_intersection.py index eb202d77c99d..60e8205d7608 100644 --- a/data_structures/disjoint_set/progressive_set_intersection.py +++ b/data_structures/disjoint_set/progressive_set_intersection.py @@ -42,6 +42,6 @@ def progressive_set_intersection(*sets: Set) -> Set: for current_set in sorted_sets[1:]: if not result: return set() - result &= current_set # Efficient in-place intersection + result &= current_set # Efficient in-place intersection return result From a7638f30c0545f9e4c8f4a46bfa07a82dc196222 Mon Sep 17 00:00:00 2001 From: Devanik <162272415+Devanik21@users.noreply.github.com> Date: Wed, 1 Apr 2026 13:52:56 +0530 Subject: [PATCH 3/4] Update progressive_set_intersection.py --- .../disjoint_set/progressive_set_intersection.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/data_structures/disjoint_set/progressive_set_intersection.py b/data_structures/disjoint_set/progressive_set_intersection.py index 60e8205d7608..4abd67de5225 100644 --- a/data_structures/disjoint_set/progressive_set_intersection.py +++ b/data_structures/disjoint_set/progressive_set_intersection.py @@ -1,9 +1,6 @@ """Progressive multi-set intersection optimized for imbalanced sets.""" -from typing import Set - - -def progressive_set_intersection(*sets: Set) -> Set: +def progressive_set_intersection(*sets: set) -> set: """ Compute the intersection of multiple sets efficiently. @@ -17,6 +14,7 @@ def progressive_set_intersection(*sets: Set) -> Set: heuristic for educational purposes. Time Complexity: Better than naive in practice due to early pruning. + Space Complexity: O(size of smallest set) Examples: >>> progressive_set_intersection({1, 2, 3}, {2, 3, 4}, {2, 5}) @@ -42,6 +40,6 @@ def progressive_set_intersection(*sets: Set) -> Set: for current_set in sorted_sets[1:]: if not result: return set() - result &= current_set # Efficient in-place intersection + result &= current_set # Efficient in-place intersection return result From 26051a054f0b5a4195d8e4e121c772293d8621e3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 1 Apr 2026 08:25:06 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/disjoint_set/progressive_set_intersection.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/data_structures/disjoint_set/progressive_set_intersection.py b/data_structures/disjoint_set/progressive_set_intersection.py index 4abd67de5225..a08d9657dd32 100644 --- a/data_structures/disjoint_set/progressive_set_intersection.py +++ b/data_structures/disjoint_set/progressive_set_intersection.py @@ -1,5 +1,6 @@ """Progressive multi-set intersection optimized for imbalanced sets.""" + def progressive_set_intersection(*sets: set) -> set: """ Compute the intersection of multiple sets efficiently. @@ -40,6 +41,6 @@ def progressive_set_intersection(*sets: set) -> set: for current_set in sorted_sets[1:]: if not result: return set() - result &= current_set # Efficient in-place intersection + result &= current_set # Efficient in-place intersection return result