v9.1.1
Menu

Maximal Marginal Relevance Selection

mediumPROContext & Agent MemoryAI AgentsAI Agents 1

Problem Statement

Select a diverse yet relevant set of memories with Maximal Marginal Relevance (MMR): greedily pick items that are relevant to the query but not too similar to what you already picked.

Background

MMR selects the next item maximizing

textMMR=lambdacdotrel(i)−(1−lambda)maxjinSsim(i,j)\\text{MMR} = \\lambda \\cdot rel(i) - (1 - \\lambda) \\max_{j \\in S} sim(i, j)

where S is the already-selected set. The first pick is the highest-relevance item (the max-sim term is 0 when S is empty). Ties are broken by smaller index.

Your Task

Implement:

def mmr(relevance, similarity, lam, k):
  • relevance: list of floats, relevance[i] = rel of item i.
  • similarity: n x n nested list, similarity[i][j].
  • Return the list of selected indices in selection order, length min(k, n).

Input Format

  • relevance (list), similarity (nested list), lam (float), k (int).

Output Format

  • A list of ints (selected indices).

Sample

rel = [0.9, 0.8, 0.7]
sim = [[1,0.9,0.1],[0.9,1,0.2],[0.1,0.2,1]]
print(mmr(rel, sim, 0.5, 2))

Output:

[0, 2]

Example

Input
rel = [0.9, 0.8, 0.7]
sim = [[1,0.9,0.1],[0.9,1,0.2],[0.1,0.2,1]]
print(mmr(rel, sim, 0.5, 2))
Output
[0, 2]
Reasoning
  • First Selection (Empty Set): Since the selected set SS is empty, the similarity penalty is 0 for all items. We calculate the MMR score as λ⋅rel(i)\lambda \cdot rel(i) with λ=0.5\lambda = 0.5:

    • Item 0: 0.5⋅0.9=0.450.5 \cdot 0.9 = 0.45
    • Item 1: 0.5⋅0.8=0.400.5 \cdot 0.8 = 0.40
    • Item 2: 0.5⋅0.7=0.350.5 \cdot 0.7 = 0.35
    • Item 0 has the highest score, so it is selected first. S=[0]S = [0].
  • Second Selection (S = {0}): We evaluate the remaining items (1 and 2) using the formula MMR=0.5⋅rel(i)−0.5⋅max⁡j∈Ssim(i,j)\text{MMR} = 0.5 \cdot rel(i) - 0.5 \cdot \max_{j \in S} sim(i, j). We must find the maximum similarity between each candidate and the already selected item 0:

    • Item 1: sim(1,0)=0.9sim(1, 0) = 0.9. Score: 0.5⋅0.8−0.5⋅0.9=0.4−0.45=−0.050.5 \cdot 0.8 - 0.5 \cdot 0.9 = 0.4 - 0.45 = -0.05.
    • Item 2: sim(2,0)=0.1sim(2, 0) = 0.1. Score: 0.5⋅0.7−0.5⋅0.1=0.35−0.05=0.300.5 \cdot 0.7 - 0.5 \cdot 0.1 = 0.35 - 0.05 = 0.30.
  • Comparison and Selection: Comparing the scores from the second step, Item 2 (0.300.30) is greater than Item 1 (−0.05-0.05). Therefore, Item 2 is selected next. S=[0,2]S = [0, 2].

  • Termination: The target size k=2k=2 is reached, so the algorithm stops.

  • The final output is [0, 2]

Constraints

  • 0 <= lam <= 1, similarity is n x n.
  • First pick = argmax relevance (ties: smaller index).
  • Each later pick maximizes lam*rel - (1-lam)*max sim to selected; ties: smaller index.

Learn this in

More from AI Agents 1

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.