Reciprocal Rank Fusion
Implement Reciprocal Rank Fusion (RRF) to combine multiple ranked lists.
RRF combines rankings from multiple retrieval systems:
where k is a constant (typically 60) and rank_r(d) is the 1-based rank of document d in ranking r. Documents not in a ranking are ignored for that ranking.
Input:
- Line 1: k (constant)
- Line 2: M (number of rankings)
- Next M lines: space-separated document IDs (in ranked order)
Output: Document IDs sorted by RRF score (descending), one per line. Break ties by lower ID first.
Example
60 2 A B C D B D A
A B D C
- We start by reading the input values: and ranked lists.
- For each document, we calculate the RRF score using the formula: . For example, .
- We calculate the RRF scores for all documents:
- We sort the documents by their RRF scores in descending order and break ties by their IDs: , but since and are very close, we need to compute them precisely to determine the order, which results in having a slightly higher score than due to the actual values of and .
Constraints
- k > 0 (typically 60)
- Rankings may have different lengths
- Rankings may contain different documents
- Round RRF scores to 6 decimal places for comparison