How to compute intersection of N sorted sets?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Computing the intersection of multiple sorted sets is a common problem in computer science with applications across various domains, such as database querying, search engines, and data analysis. In this article, we will explore an efficient approach to compute the intersection of sorted sets. We will examine a step-by-step method, discuss its computational complexity, and present examples to demonstrate its efficacy. For the sake of clarity, "sets" here refer to collections of distinct elements arranged in sorted order.
Problem Statement
Given sorted sets, our goal is to determine the set of elements that are present in all of these sets — the intersection of these sets. Each set is sorted in non-decreasing order.
Approach
Two-Pointer Technique
To efficiently compute the intersection of multiple sorted sets, we can extend the two-pointer technique, which is typically used for two sorted arrays, to sorted sets.
Algorithm
- Initialize Pointers:
- For each sorted set, initialize a pointer (or index) at the start of the set.
- Iterate Across all Sets:
- Compare the elements pointed to by the pointers in each set.
- If all pointers point to the same element, it is part of the intersection. Record this element and move all pointers to the next element in their respective sets.
- If there is a discrepancy among the elements pointed to, determine the smallest element among the current elements. Advance the pointer(s) pointing to this smallest element.
- Repeat this process until one or more pointers reach the end of their respective sets.
- Complexity Analysis:
- For sorted sets where the maximum size of a set is , the time complexity of the intersection operation is . This is due to potentially checking each element in the largest set times.
Example
Consider three sorted sets:
- Set A: `[1, 3, 4, 5, 7]`
- Set B: `[2, 3, 5, 6]`
- Set C: `[1, 3, 5, 8, 9]`
The algorithm proceeds as follows:
- Initialize three pointers `i`, `j`, and `k` for sets A, B, and C respectively:
- A[i], B[j], C[k] = 1, 2, 1
- Current minimum is 1 (A[i] and C[k]), advance the pointer i to 3:
- A[i], B[j], C[k] = 3, 2, 1
- Current minimum is 1 (still C[k]), advance pointer k to 3:
- A[i], B[j], C[k] = 3, 2, 3
- Current minimum is 2, advance pointer j to 3:
- A[i], B[j], C[k] = 3, 3, 3
- All pointers now point to the same value, 3, add to intersection and advance all pointers:
- Intersection = [3]
- A[i], B[j], C[k] = 4, 5, 5
- Current minimum is 4, advance pointer i to 5:
- A[i], B[j], C[k] = 5, 5, 5
- All pointers point to 5, add to intersection and advance all pointers:
- Intersection = [3, 5]
- A[i], B[j], C[k] = 7, EndOfSet, 8
- One pointer has reached the end, stop.
Final Intersection: `[3, 5]`
Efficiency
The efficiency of this method comes from the fact that it capitalizes on the sorted nature of the sets. By only advancing pointers through individual sets when necessary, it minimizes redundant checks.
Additional Considerations
Memory Considerations
The auxiliary space required, aside from input storage, consists of the intersection result and the pointers used for traversal. This makes the process very memory-efficient, especially suitable for cases when inputs are massive but fit within the in-memory capacity.
Variations & Extensions
- Lazy Evaluation: If the intersection does not need to be stored completely but merely evaluated element by element, this algorithm can easily be adapted for lazy evaluation.
- Handling Large Data Sets: For very large data sets, consider dividing data across multiple cores or machines (parallel processing) and merging the results to improve performance.
- Use of Advanced Data Structures: Utilizing data structures like heaps or trees could further optimize specific scenarios, especially when dealing with streaming or dynamically changing data.
Summary
Here's a table summarizing the key points regarding the efficient computation of the intersection of N sorted sets:
| Factor | Description |
| Algorithm | Two-pointer technique, generalized for N-sets |
| Time Complexity | |
| Memory Requirement | Low, aside from input storage |
| Key Operations | Pointer advancement, comparison |
| Best Suited For | Moderate-sized sets, whenever sorting is already guaranteed |
| Extensions & Variations | Lazy evaluation, parallel processing |
By leveraging sorted order in input sets and efficiently managing pointer advancements, this approach optimizes the process of finding intersections in computationally intensive environments.

