List Intersection
List intersection computes the sorted list of elements common to two (or more) sorted lists, e.g. the doc IDs matching all keywords of a query, in linear time with the zipper algorithm or faster with binary or galloping search when one list is much shorter.
Formula#
| Algorithm | Runtime | Best when |
|---|---|---|
| Zipper (linear) | ||
| Binary search | ||
| Galloping search | (tighter bound) | |
| -way intersect/merge of | many lists at once |
Properties#
1.Zipper (Linear Merge Intersection)#
Walk through both sorted lists simultaneously with two pointers. Advance the pointer pointing to the smaller element; when both point to equal elements, emit that element to the result (Slides 01, p. 18).
def intersect(list1, list2):
result = []
i, j = 0, 0
while i < len(list1) and j < len(list2):
if list1[i] < list2[j]:
i += 1
elif list1[i] > list2[j]:
j += 1
else:
result.append(list1[i])
i += 1
j += 1
return result
Runtime: : every step advances at least one pointer.
2.Binary Search Intersection#
For each element of the shorter list, perform a binary search in the longer list.
def intersect_binary(list1, list2):
if len(list1) > len(list2):
list1, list2 = list2, list1
result = []
for x in list1:
lo, hi = 0, len(list2)
while lo < hi:
mid = (lo + hi) // 2
if list2[mid] < x:
lo = mid + 1
elif list2[mid] > x:
hi = mid
else:
result.append(x)
break
return result
Runtime: : binary searches of each.
3.Galloping (Exponential) Search Intersection#
A refinement of binary search: for each element of the shorter list, run an exponential (galloping) search in the longer list: find a range in which could lie by doubling the index, then binary search within that range.
Runtime:
Galloping is never worse than the zipper#
Using , i.e. , with :
and differ only by a constant factor, hence (DBIS Exam WS1819 (not published), Task 1.2).
Practical choice#
Galloping search has a larger constant factor due to its more complex algorithm. When is close to 1, the zipper’s simplicity wins in practice. A common heuristic: use galloping when , otherwise use the zipper (DBIS Exam WS1819 (not published), Task 1.3).
Multi-List Intersection ( lists)#
Given inverted lists for query terms (Slides 01, p. 19):
- Sequential pairwise: Intersect , then , etc.
- Optimization: Sort lists by length () to minimize intermediate result sizes.
- -way intersection using a priority queue: .
Example#
Lists for query “spielberg dinosaurs” (Slides 01, p. 18):
The zipper walks both lists left to right: ’s pointer advances over (all ), both pointers meet at (emit), advances over (all ), both meet at (emit), and so on.
Sources#
- Slides 01, p. 18 (two keywords, zipper), Slides 01, p. 19 ( keywords)
- DBIS_VL01 > Query Processing
- Slides 02, p. 8, DBIS_VL02 > Ranking: Basic Idea (merging scored lists)
- Slides 05, p. 13, DBIS_VL05 > Merge join (merge join = zipper)
- Past exams: DBIS Exam WS1819 (not published) Task 1 (binary search intersection, galloping vs. zipper), DBIS Exam WS2324 (not published) Task 1.2 (merge)