I have a table with eachof the 500 person's name on the side and the top and it tells me each of their compatibility with each other as a number from 0 to 1.
I want to assign them into pairs so that I maximize their total compatibility of every pair that I assign.
Is this a graph theory problem? Does it have a solution?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
You’ve hit on a classic in graph‐theory/optimization: “Given 500 nodes, each edge (i,j) has weight wᵢⱼ ∈ [0,1]. Find 250 disjoint edges whose total weight is maximum.”
In graph‐theory terms this is the maximum‐weight perfect matching problem in a (complete) undirected graph. It is solvable in polynomial time (no NP‐hardness here!) using Edmonds’ blossom algorithm or one of its modern implementations.
1. Why it’s a graph problem
Model each person as a vertex in an undirected graph G=(V,E), |V|=500.
Every pair {i,j} has an edge with weight wᵢⱼ (= compatibility).
A perfect matching is a set of 250 edges so each vertex appears exactly once.
We want the matching whose sum of weights is maximum.
2. Polynomial‐time algorithms
Edmonds’ Blossom algorithm (originally O(n⁴), improved to O(n³))
Modern solvers / libraries:
Blossom V (Kolmogorov’s C++ implementation)
Lemon (C++ library for graph algorithms)
Google OR-Tools (via CP-SAT or linear‐assignment modelling)
NetworkX in Python
If your graph were bipartite, you could use the Hungarian (Kuhn–Munkres) algorithm, but here it’s a general graph so you need “blossom.”
3. A quick Python example with NetworkX
python
import networkx as nx
# Suppose compat is a 500×500 numpy array with zeros on the diagonal:
# compat[i][j] = compat[j][i] = compatibility between i and j.
G = nx.Graph()
n = compat.shape[0]
# add all edges
for i in range(n):
for j in range(i+1, n):
G.add_edge(i, j, weight=compat[i][j])
# max_weight_matching returns a set of node‐pairs
matching = nx.algorithms.matching.max_weight_matching(G, maxcardinality=True)
print("Total pairs formed:", len(matching))
print("Sum of compatibilities:",
sum(compat[i][j] for i,j in matching))
That function uses a variant of Edmonds’ blossom under the hood and will handle n=500 in a few seconds.
4. Practical tips
Make sure all your weights are nonnegative (0–1 is perfect).
If your data are floats, you may scale them to integers (e.g. multiply by 1,000,000) if needed by some solvers.
If you ever need to forbid certain pairings, just omit those edges or give them weight = −∞ (or a large negative).
In a nutshell
Yes — it’s the maximum‐weight perfect matching problem in a complete graph, and it does have an efficient polynomial‐time solution. All you need is any implementation of Edmonds’ blossom algorithm (e.g. Blossom V, NetworkX, OR-Tools) and you’re off to the races. 🚀