Skip to content

[Model] ThreeDimensionalMatching #806

@isPANN

Description

@isPANN

Motivation

3-DIMENSIONAL MATCHING (3DM) (P128) from Garey & Johnson, A3 SP1. One of Karp's 21 NP-complete problems, 3DM is a foundational problem for reductions to partitioning and scheduling problems. It occupies a central position in the G&J reduction hierarchy, serving as the source for many strong NP-completeness proofs.

Associated rules:

Definition

Name: ThreeDimensionalMatching
Reference: Garey & Johnson, Computers and Intractability, A3 SP1

Mathematical definition:

INSTANCE: Set M ⊆ W×X×Y, where W, X, and Y are disjoint sets having the same number q of elements.
QUESTION: Does M contain a matching, i.e., a subset M' ⊆ M such that |M'| = q and no two elements of M' agree in any coordinate?

Variables

  • Count: m = |M| (one binary variable per candidate triple)
  • Per-variable domain: {0, 1} — 0 means the triple is excluded, 1 means it is selected
  • Meaning: x_i = 1 if triple t_i ∈ M is included in the matching M'. A valid matching requires exactly q selected triples, each covering a distinct element from W, X, and Y.

Schema (data type)

Type name: ThreeDimensionalMatching
Variants: none

Field Type Description
universe_size usize q =
triples Vec<(usize, usize, usize)> Candidate triples (w, x, y) where w ∈ {0..q-1}, x ∈ {0..q-1}, y ∈ {0..q-1}

Notes:

  • This is a feasibility (decision) problem: Value = Or.
  • Key getter methods: universe_size() (= q), num_triples() (= |M|).
  • W, X, Y are implicitly {0, ..., q-1}; no need to store them explicitly.

Complexity

  • Decision complexity: NP-complete (Karp, 1972; transformation from 3SAT). Remains NP-complete when no element occurs in more than 3 triples. Solvable in polynomial time when no element occurs in more than 2 triples.
  • Best known exact algorithm: O*(2^m) by brute-force enumeration of all subsets of size q from the m triples. No significantly faster exact algorithm is known for the general case.
  • Concrete complexity expression: "2^num_triples" (for declare_variants!)
  • Related: 2-Dimensional Matching (M ⊆ W×X) is solvable in polynomial time.
  • References:
    • R.M. Karp (1972). "Reducibility Among Combinatorial Problems." In Complexity of Computer Computations, pp. 85-103.
    • M.R. Garey and D.S. Johnson (1979). Computers and Intractability. W.H. Freeman.

Extra Remark

Full book text:

INSTANCE: Set M ⊆ W×X×Y, where W, X, and Y are disjoint sets having the same number q of elements.
QUESTION: Does M contain a matching, i.e., a subset M' ⊆ M such that |M'| = q and no two elements of M' agree in any coordinate?
Reference: [Karp, 1972]. Transformation from 3SAT (see Section 3.1.2).
Comment: Remains NP-complete if M is "pairwise consistent," i.e., if for all elements a, b, c, whenever there exist elements w, z, and y such that (a,b,w) ∈ M, (a,x,c) ∈ M, and (y,b,c) ∈ M, then (a,b,c) ∈ M (this follows from the proof of Theorem 3.1.2). Also remains NP-complete if no element occurs in more than three triples, but is solvable in polynomial time if no element occurs in more than two triples [Garey and Johnson, ——]. The related 2-DIMENSIONAL MATCHING problem (where M ⊆ W×X) is also solvable in polynomial time (e.g., see [Lawler, 1976a]).

How to solve

  • It can be solved by (existing) bruteforce. (Enumerate all subsets of triples of size q; check if any forms a perfect matching.)
  • It can be solved by reducing to integer programming. (Binary ILP: x_i ∈ {0,1} for each triple, Σ x_i = q, and for each element in W/X/Y, the sum of x_i over triples containing that element equals 1. Companion rule issue to be filed separately.)
  • Other: (none known beyond brute force and ILP)

Example Instance

Input:
W = X = Y = {0, 1, 2} (q = 3)
Triples (m = 5):

  • t_0 = (0, 1, 2)
  • t_1 = (1, 0, 1)
  • t_2 = (2, 2, 0)
  • t_3 = (0, 0, 0)
  • t_4 = (1, 2, 2)

Valid matching: M' = {t_0, t_1, t_2} = {(0,1,2), (1,0,1), (2,2,0)}:

  • W elements: {0, 1, 2} — all distinct ✓
  • X elements: {1, 0, 2} — all distinct ✓
  • Y elements: {2, 1, 0} — all distinct ✓

Answer: YES — this is the unique valid matching (1 out of C(5,3) = 10 subsets of size 3).

Non-trivial structure: The other 9 size-3 subsets all fail. For example:

  • {t_1, t_2, t_3} = {(1,0,1), (2,2,0), (0,0,0)}: X = {0, 2, 0} ✗ (element 0 repeated)
  • {t_0, t_2, t_4} = {(0,1,2), (2,2,0), (1,2,2)}: X = {1, 2, 2} ✗ (element 2 repeated)

Negative modification: Remove t_2 = (2,2,0). Now M = {t_0, t_1, t_3, t_4}. The only size-3 subsets are {t_0,t_1,t_3}, {t_0,t_1,t_4}, {t_0,t_3,t_4}, {t_1,t_3,t_4} — none has all three W-coordinates distinct (W-coordinates of t_3 and t_0 both equal 0; t_1 and t_4 both equal 1). Answer: NO.

Python validation script
from itertools import combinations

triples = [(0,1,2), (1,0,1), (2,2,0), (0,0,0), (1,2,2)]
q = 3

# Positive: find all valid matchings
valid = []
for combo in combinations(range(len(triples)), q):
    sel = [triples[i] for i in combo]
    ws = [t[0] for t in sel]
    xs = [t[1] for t in sel]
    ys = [t[2] for t in sel]
    if len(set(ws)) == q and len(set(xs)) == q and len(set(ys)) == q:
        valid.append(combo)

assert len(valid) == 1
assert valid[0] == (0, 1, 2)
print(f"Positive: {len(valid)}/10 valid matchings (unique)")

# Negative: remove t2
triples_neg = [(0,1,2), (1,0,1), (0,0,0), (1,2,2)]
valid_neg = []
for combo in combinations(range(len(triples_neg)), q):
    sel = [triples_neg[i] for i in combo]
    ws = [t[0] for t in sel]
    xs = [t[1] for t in sel]
    ys = [t[2] for t in sel]
    if len(set(ws)) == q and len(set(xs)) == q and len(set(ys)) == q:
        valid_neg.append(combo)

assert len(valid_neg) == 0
print(f"Negative: {len(valid_neg)}/4 valid matchings (correct: NO)")

Metadata

Metadata

Assignees

No one assigned

    Labels

    GoodAn issue passed all checks.modelA model problem to be implemented.

    Type

    No type

    Projects

    Status

    Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions