TExcelTAD
Home › Parameterized & Fine-Grained Complexity › APSP, Negative Triangle and Subcubic Equivalence

APSP, Negative Triangle and Subcubic Equivalence

All-pairs shortest paths on an nn-vertex graph with real or integer weights takes O(n3)O(n^3) time with Floyd-Warshall (The Floyd-Warshall Algorithm) or nn runs of Dijkstra on dense graphs. Decades of work brought only subpolynomial improvements: the fastest algorithm, by Williams (2014), runs in n3/2Ω(log⁡n)n^3 / 2^{\Omega(\sqrt{\log n})} time. The third pillar of fine-grained complexity is the conjecture that no truly subcubic algorithm exists, and the remarkable discovery that many problems that look easier, some with a yes/no answer, are equivalent to APSP under subcubic reductions.

The APSP conjecture

APSP conjecture. For every ε>0\varepsilon > 0, APSP on nn-vertex graphs with integer edge weights in {−nc,…,nc}\{-n^{c}, \ldots, n^{c}\} (for some constant cc) and no negative cycles has no O(n3−ε)O(n^{3 - \varepsilon}) algorithm.

The weights matter: with small integer weights (bounded by a constant), fast matrix multiplication gives subcubic algorithms (for undirected unweighted graphs, Seidel, 1995: O(nωlog⁡n)O(n^{\omega} \log n), where ω<2.372\omega < 2.372 is the matrix multiplication exponent). The conjecture is about the general weighted case, where the algebra of (min⁡,+)(\min, +) resists the tricks of ordinary matrix multiplication.

The min-plus product

The min-plus (distance) product of n×nn \times n matrices AA and BB is

(A⋆B)ij=min⁡k(Aik+Bkj).(A \star B)_{ij} = \min_{k} \left( A_{ik} + B_{kj} \right).

It is the matrix multiplication of the (min⁡,+)(\min, +) semiring, computed naively in O(n3)O(n^3) time, and APSP is equivalent to it (Shortest Paths as Matrix Multiplication):

  • APSP from min-plus: with WW the weight matrix (0 on the diagonal), W⋆2tW^{\star 2^t} holds shortest paths of at most 2t2^t edges, so ⌈log⁡2n⌉\lceil \log_2 n \rceil squarings give all distances. A subcubic min-plus product gives subcubic APSP (a log⁡n\log n factor is absorbed by the ε\varepsilon).
  • Min-plus from APSP: build a three-layer graph with vertices I={i}I = \{i\}, K={k}K = \{k\}, J={j}J = \{j\}, edges i→ki \to k of weight AikA_{ik} and k→jk \to j of weight BkjB_{kj}; the distance from ii to jj is (A⋆B)ij(A \star B)_{ij}. The graph has 3n3n vertices.

The program checks both directions on small random matrices.

python · try it yourself
import random

INF = float("inf")

def min_plus(A, B):
    n = len(A)
    return [[min(A[i][k] + B[k][j] for k in range(n)) for j in range(n)] for i in range(n)]

def floyd_warshall(W):
    n = len(W)
    D = [row[:] for row in W]
    for k in range(n):
        for i in range(n):
            for j in range(n):
                if D[i][k] + D[k][j] < D[i][j]:
                    D[i][j] = D[i][k] + D[k][j]
    return D

rng = random.Random(4)
n = 12
W = [[0 if i == j else (rng.randint(1, 20) if rng.random() < 0.4 else INF) for j in range(n)] for i in range(n)]
D, squarings = W, 0
while 2 ** squarings < n - 1:                   # paths of at most 2^t edges after t squarings
    D = min_plus(D, D)
    squarings += 1
print("APSP by", squarings, "min-plus squarings equals Floyd-Warshall:", D == floyd_warshall(W))

A = [[rng.randint(-9, 9) for _ in range(n)] for _ in range(n)]
B = [[rng.randint(-9, 9) for _ in range(n)] for _ in range(n)]
G = [[INF] * (3 * n) for _ in range(3 * n)]     # layers I = 0..n-1, K = n..2n-1, J = 2n..3n-1
for v in range(3 * n):
    G[v][v] = 0
for i in range(n):
    for k in range(n):
        G[i][n + k] = A[i][k]
        G[n + i][2 * n + k] = B[i][k]
D3 = floyd_warshall(G)
print("min-plus product read off a 3-layer APSP instance:",
      all(D3[i][2 * n + j] == min_plus(A, B)[i][j] for i in range(n) for j in range(n)))
APSP by 4 min-plus squarings equals Floyd-Warshall: True
min-plus product read off a 3-layer APSP instance: True

The three-layer graph has negative edges but no cycles at all, so shortest paths are well defined.

Negative triangle

NEGATIVE TRIANGLE. Given an nn-vertex graph with integer edge weights, are there vertices i,j,ki, j, k with w(i,j)+w(j,k)+w(k,i)<0w(i, j) + w(j, k) + w(k, i) < 0?

It is a yes/no question, and it looks easier than APSP: one bit of output instead of n2n^2 numbers. The easy direction is clear: compute C=W⋆WC = W \star W, then check whether Cij+w(j,i)<0C_{ij} + w(j, i) < 0 for some pair. The surprise is the other direction.

Theorem (Vassilevska Williams and Williams, 2010; stated). NEGATIVE TRIANGLE has an O(n3−ε)O(n^{3 - \varepsilon}) algorithm for some ε>0\varepsilon > 0 if and only if APSP does (with polynomially bounded weights).

Idea of the hard direction (sketch). To compute the min-plus product C=A⋆BC = A \star B, it suffices to find, for every pair (i,j)(i, j), the value CijC_{ij}. Guess thresholds TijT_{ij} and ask, for all pairs at once, whether min⁡k(Aik+Bkj)<Tij\min_k (A_{ik} + B_{kj}) < T_{ij}: with w(i,k)=Aikw(i, k) = A_{ik}, w(k,j)=Bkjw(k, j) = B_{kj}, w(j,i)=−Tijw(j, i) = -T_{ij} this is a negative triangle question about the pair (i,j)(i, j). A parallel binary search over the thresholds needs O(log⁡W)O(\log W) rounds. The key step is to find all pairs involved in negative triangles, not just one, using a subcubic detection algorithm on many small subgraphs: split the vertex set into blocks of size n1/3n^{1/3} and repeatedly detect and remove triangles block by block. The bookkeeping keeps the total time subcubic.

The program checks the easy direction against brute force, counting the basic operations of each.

python · try it yourself
import random

def brute_negative_triangle(W):
    n, ops = len(W), 0
    for i in range(n):
        for j in range(i + 1, n):
            for k in range(j + 1, n):
                ops += 1
                if W[i][j] + W[j][k] + W[k][i] < 0:
                    return (i, j, k), ops
    return None, ops

def via_min_plus(W):
    n, ops = len(W), 0
    for i in range(n):
        for j in range(n):
            if i == j:
                continue
            best = None
            for k in range(n):
                if k != i and k != j:
                    ops += 1
                    s = W[i][k] + W[k][j]
                    best = s if best is None or s < best else best
            if best + W[j][i] < 0:                # C_ij + w(j, i) < 0
                return (i, j), ops
    return None, ops

rng = random.Random(7)
print("  n  shift  brute force          via min-plus")
for n, shift in [(20, 0), (20, 70), (30, 85), (30, 100)]:
    W = [[0] * n for _ in range(n)]
    for i in range(n):
        for j in range(i + 1, n):
            W[i][j] = W[j][i] = rng.randint(-100, 100) + shift
    t1, o1 = brute_negative_triangle(W)
    t2, o2 = via_min_plus(W)
    print(f"{n:3} {shift:5}  {str(t1):12} {o1:6}  {str(t2):9} {o2:6}")
  n  shift  brute force          via min-plus
 20     0  (0, 1, 2)         1  (0, 1)        18
 20    70  (0, 4, 19)       66  (0, 4)        72
 30    85  (0, 16, 26)     325  (0, 16)      448
 30   100  None           4060  None       24360

The shift makes all weights larger, so negative triangles become rare; at shift 100 three weights of at least 0 cannot sum below zero, none exists and both methods do their full cubic work. The min-plus version visits ordered triples, six times as many as brute force, but it computes all of CC on the way, which is what the reductions exploit. Both answers always agree on whether a negative triangle exists.

In C++

Negative triangle detection with the min-plus formulation on a 300-vertex complete graph, weights uniform in [−1000,1000][-1000, 1000] plus a shift that makes negative triangles rare: the scan over (i,j)(i, j) pairs with an inner minimum over kk is the cubic loop that the APSP conjecture says cannot be beaten by a polynomial factor.

cpp · try it yourself
#include <algorithm>
#include <climits>
#include <cstdio>
#include <vector>

int main() {
    const int n = 300;
    unsigned s = 5;
    auto rnd = [&](int m) { s = s * 1103515245u + 12345u; return int((s >> 8) % m); };
    for (int shift : {800, 930, 1000}) {
        std::vector<std::vector<int>> w(n, std::vector<int>(n, 0));
        for (int i = 0; i < n; i++)
            for (int j = i + 1; j < n; j++) w[i][j] = w[j][i] = rnd(2001) - 1000 + shift;
        long long ops = 0;
        int fi = -1, fj = -1, fk = -1;
        for (int i = 0; i < n && fi < 0; i++)
            for (int j = 0; j < n && fi < 0; j++) {
                if (i == j) continue;
                int best = INT_MAX, arg = -1;
                for (int k = 0; k < n; k++) {
                    if (k == i || k == j) continue;
                    ops++;
                    if (w[i][k] + w[k][j] < best) { best = w[i][k] + w[k][j]; arg = k; }
                }
                if (best + w[j][i] < 0) { fi = i; fj = j; fk = arg; }
            }
        if (fi >= 0)
            std::printf("shift %d: negative triangle (%d, %d, %d) of weight %d after %lld steps\n", shift, fi, fk, fj,
                        w[fi][fk] + w[fk][fj] + w[fj][fi], ops);
        else
            std::printf("shift %d: no negative triangle after %lld steps (n^3 = %lld)\n", shift, ops, (long long)n * n * n);
    }
}
shift 800: negative triangle (0, 92, 2) of weight -1 after 596 steps
shift 930: negative triangle (0, 103, 26) of weight -31 after 7748 steps
shift 1000: no negative triangle after 26730600 steps (n^3 = 27000000)

The APSP equivalence class

Vassilevska Williams and Williams (2010), with later additions (stated), showed that the following are equivalent under subcubic reductions: each has an O(n3−ε)O(n^{3 - \varepsilon}) algorithm iff all do.

Problem Output
APSP; min-plus product n2n^2 numbers
negative triangle one bit
metricity: does a matrix satisfy the triangle inequality? one bit
second shortest simple path between two vertices one number
replacement paths: shortest ss-tt path avoiding each edge of a shortest path nn numbers
minimum weight cycle in a directed or undirected graph (Roditty and Vassilevska Williams, 2011) one number
radius; median (Abboud, Grandoni and Vassilevska Williams, 2015) one number

Diameter is notably not known to be in this class: it reduces to APSP, but no reduction in the other direction is known. Its approximation hardness instead comes from SETH via orthogonal vectors (Roditty and Vassilevska Williams, 2013), a reminder that the different conjectures govern different problems.

Test yourself

How does APSP reduce to the min-plus product?

  1. By one product W⋆WW \star W
  2. By ⌈log⁡2n⌉\lceil \log_2 n \rceil repeated squarings of the weight matrix
  3. By nn products
  4. It does not

Why is the equivalence of negative triangle and APSP surprising?

  1. Because negative triangle is NP-hard
  2. Because a one-bit decision problem turns out to be exactly as hard, up to subcubic factors, as computing n2n^2 distances
  3. Because APSP is linear
  4. Because triangles have three vertices

How is negative triangle solved from the min-plus product C=W⋆WC = W \star W?

  1. Check whether some Cij+w(j,i)<0C_{ij} + w(j, i) < 0
  2. Check whether CC is symmetric
  3. Check the diagonal of WW
  4. Compute the determinant

Which statement about the APSP conjecture is correct?

  1. It is proven
  2. It concerns graphs with polynomially bounded integer weights; small constant weights allow subcubic algorithms via fast matrix multiplication
  3. It implies P ≠\ne NP
  4. It forbids n3/log⁡nn^3 / \log n algorithms