Doing a maximum weight perfect matching on a set of 2000 nodes (i.e., 1000 pairs) is computationally feasible, especially if you use good libraries with optimized implementations.
✅ Summary:
- Problem size:
n = 2000 (even), complete compatibility matrix of shape [2000, 2000].
- Goal: Pair each person with another to maximize total compatibility.
- Solution: Hungarian Algorithm (efficient and available via
scipy.optimize.linear_sum_assignment) is well-suited and faster than Blossom for this size due to matrix-based optimization.
💡 Algorithm Selection:
You can use the Hungarian algorithm, typically used for assignment problems, and adapt your data for that.
But the Hungarian algorithm expects the number of rows and columns to be the same size, so we can formulate the maximum weight perfect matching using the compatibility matrix directly.
🔧 Note:
scipy.optimize.linear_sum_assignment minimizes the cost instead of maximizing the weight. So we need to negate the compatibility scores or subtract them from a constant to turn the problem into a minimization one.
✅ Example Python Code
import numpy as np
from scipy.optimize import linear_sum_assignment
import time
# Number of people
n = 2000 # Must be even
# Generate a random compatibility matrix (symmetric, values between 0 and 1)
np.random.seed(42)
compatibility = np.random.rand(n, n)
# Ensure the matrix is symmetric and has zeros on its diagonal (compatibility with self)
compatibility = (compatibility + compatibility.T) / 2
np.fill_diagonal(compatibility, 0)
# Hungarian Algorithm requires a minimization problem
# So we convert this to a "cost matrix" by negating the values
cost_matrix = -compatibility
start = time.time()
# Using the Hungarian algorithm from SciPy
row_ind, col_ind = linear_sum_assignment(cost_matrix)
end = time.time()
print(f"Time taken: {end - start:.2f} seconds")
# Since the assignment is bidirectional, only take one direction to list pairs
paired = set()
pairs = []
total_compatibility = 0.0
for i, j in zip(row_ind, col_ind):
if i < j and (i not in paired and j not in paired):
pairs.append((i, j))
total_compatibility += compatibility[i, j]
paired.add(i)
paired.add(j)
print(f"Number of pairs: {len(pairs)}")
print(f"Total compatibility: {total_compatibility:.4f}")
🧠 What This Code Does
- Creates a symmetric compatibility matrix.
- Uses the Hungarian algorithm to find the best pairing to minimize the (negated) cost → effectively maximizing compatibility.
- Converts the result into ordered (i, j) pairs for clarity.
- Measures execution time.
⚡️ Performance
With n = 2000, this will:
- Use about 2000 x 2000 x 8 bytes ≈ 32 MB of RAM for the matrix.
- Hungarian algorithm has O(n³) worst-case complexity, but it’s optimized and runs well in practice.
- Expect completion on a modern machine in < 1 minute, often within 10–20 seconds depending on hardware.
✅ Optional: Parallelized Libraries / C++ Backend
- If it's still slow for larger sizes (e.g., n=5000), consider:
- Using Numba or Cython to speed up parts of processing.
- Use compiled matchers like Jonker-Volgenant algorithm which are faster than Hungarian in practice.
Let me know if you want to visualize the pairs or save the output to a file!