TExcelTAD
Home › Linear Programming & Duality for Algorithms › PSD Matrices and Vector Programs

PSD Matrices and Vector Programs

An LP relaxation assigns each vertex of a graph a number. A semidefinite program (SDP) assigns each vertex a vector, and measures relationships between vertices by inner products. The extra geometry is what lets SDPs beat LPs on problems like max-cut, where the LP gap is 2. This lesson collects the facts about positive semidefinite matrices that algorithm design needs, defines SDPs and their equivalent vector programs, explains in what sense they are solvable in polynomial time, and works out the classic first example, the Lovász theta number of the 5-cycle, by hand. The conic-programming view is in Second-Order Cone and Semidefinite Programs, and the linear algebra in Positive Semidefinite and Positive Definite Matrices.

Positive semidefinite matrices

A symmetric matrix X∈Rn×nX \in \mathbb{R}^{n \times n} is positive semidefinite (PSD), written X⪰0X \succeq 0, if any of these equivalent conditions holds:

  1. aTXa≥0a^T X a \ge 0 for every a∈Rna \in \mathbb{R}^n;
  2. every eigenvalue of XX is non-negative;
  3. X=VTVX = V^T V for some matrix VV, that is, Xij=vi⋅vjX_{ij} = v_i \cdot v_j for vectors v1,…,vnv_1, \ldots, v_n (the columns of VV): XX is a Gram matrix.

The third condition is the one that matters for algorithms. A factorization can be computed from the eigendecomposition X=QΛQTX = Q \Lambda Q^T as V=Λ1/2QTV = \Lambda^{1/2} Q^T, or by an incomplete Cholesky factorization, in O(n3)O(n^3) time; the vectors live in Rr\mathbb{R}^r with r=rank⁡Xr = \operatorname{rank} X. The program checks two matrices and recovers vectors from the PSD one.

python · try it yourself
import numpy as np

def analyse(name, X):
    X = np.array(X, dtype=float)
    lam, Q = np.linalg.eigh(X)
    print(f"{name}: eigenvalues {[round(float(v), 4) + 0.0 for v in lam]}")
    if lam.min() < -1e-9:
        a = Q[:, 0]                                  # eigenvector of the most negative eigenvalue
        a = a if a[np.argmax(np.abs(a))] > 0 else -a  # fix the sign, which LAPACK leaves open
        print(f"  not PSD: a^T X a = {float(a @ X @ a):.4f} < 0 for a = {[round(float(v), 4) for v in a]}")
        return
    keep = lam > 1e-9
    V = np.sqrt(lam[keep])[:, None] * Q[:, keep].T   # columns are the vectors v_i
    print(f"  PSD of rank {int(keep.sum())}; Gram vectors reproduce X: {bool(np.allclose(V.T @ V, X))}")
    print(f"  pairwise angles (degrees): " + ", ".join(
        f"v{i}v{j} {np.degrees(np.arccos(np.clip(X[i, j] / np.sqrt(X[i, i] * X[j, j]), -1, 1))):.1f}"
        for i in range(len(X)) for j in range(i + 1, len(X))))

analyse("X1", [[1, -0.5, -0.5], [-0.5, 1, -0.5], [-0.5, -0.5, 1]])
analyse("X2", [[1, -1, -1], [-1, 1, -1], [-1, -1, 1]])
X1: eigenvalues [0.0, 1.5, 1.5]
  PSD of rank 2; Gram vectors reproduce X: True
  pairwise angles (degrees): v0v1 120.0, v0v2 120.0, v1v2 120.0
X2: eigenvalues [-1.0, 2.0, 2.0]
  not PSD: a^T X a = -1.0000 < 0 for a = [0.5774, 0.5774, 0.5774]

X1X_1 is the Gram matrix of three unit vectors at 120120 degrees to each other in a plane (rank 2). X2X_2 asks for three unit vectors that are pairwise opposite, which is impossible, and the negative eigenvalue comes with a vector aa that proves it: aTX2a<0a^T X_2 a < 0. That vector is a separating hyperplane for the PSD cone (aTXa≥0a^T X a \ge 0 is a linear inequality in XX valid for all PSD matrices and violated by X2X_2), which is exactly what the ellipsoid method needs.

In C++

A PSD test by Cholesky factorization with a tolerance: X=LLTX = LL^T succeeds (with zero pivots allowed only when the rest of the column is zero too) exactly when XX is PSD.

cpp · try it yourself
#include <cmath>
#include <cstdio>
#include <vector>

using Mat = std::vector<std::vector<double>>;

// Cholesky with a tolerance for semidefinite matrices; returns false at the first negative pivot.
bool is_psd(Mat X, double eps = 1e-9) {
    int n = X.size();
    Mat L(n, std::vector<double>(n, 0));
    for (int j = 0; j < n; j++) {
        double d = X[j][j];
        for (int k = 0; k < j; k++) d -= L[j][k] * L[j][k];
        if (d < -eps) return false;
        L[j][j] = d > eps ? std::sqrt(d) : 0;
        for (int i = j + 1; i < n; i++) {
            double s = X[i][j];
            for (int k = 0; k < j; k++) s -= L[i][k] * L[j][k];
            if (L[j][j] == 0) { if (std::fabs(s) > eps) return false; }   // zero pivot needs a zero column
            else L[i][j] = s / L[j][j];
        }
    }
    return true;
}

int main() {
    Mat X1 = {{1, -0.5, -0.5}, {-0.5, 1, -0.5}, {-0.5, -0.5, 1}};
    Mat X2 = {{1, -1, -1}, {-1, 1, -1}, {-1, -1, 1}};
    Mat X3 = {{1, 1, 0}, {1, 1, 0}, {0, 0, 2}};                    // rank 2, a zero pivot appears
    std::printf("X1 PSD: %d\nX2 PSD: %d\nX3 PSD: %d\n", is_psd(X1), is_psd(X2), is_psd(X3));
}
X1 PSD: 1
X2 PSD: 0
X3 PSD: 1

Semidefinite programs and vector programs

A semidefinite program optimizes a linear function of a symmetric matrix variable subject to linear equations and the PSD constraint. With ⟨C,X⟩=∑ijCijXij\langle C, X \rangle = \sum_{ij} C_{ij} X_{ij}:

max⁡  ⟨C,X⟩s.t.⟨Ak,X⟩=bk  (k=1,…,m),X⪰0.\max \; \langle C, X \rangle \quad \text{s.t.} \quad \langle A_k, X \rangle = b_k \ \ (k = 1, \ldots, m), \qquad X \succeq 0 .

An LP is the special case where all matrices are diagonal. By condition 3, an SDP is the same as a vector program: choose vectors v1,…,vn∈Rnv_1, \ldots, v_n \in \mathbb{R}^n to optimize a linear function of the inner products vi⋅vjv_i \cdot v_j subject to linear constraints on them. The dimension nn comes for free (any Gram matrix of nn vectors is realised in Rn\mathbb{R}^n), and that freedom is essential: restricting the vectors to R1\mathbb{R}^1 gives back numbers, typically an NP-hard problem.

Solvability. The feasible set is convex and the PSD constraint has the polynomial-time separation oracle shown above, so the ellipsoid method (and, in practice, interior-point methods) solve SDPs approximately: for any ε>0\varepsilon > 0, a solution within ε\varepsilon of the optimum, in time polynomial in the input size and log⁡(1/ε)\log(1/\varepsilon), under mild conditions (the feasible set is contained in a ball of known radius and contains a ball of known radius, or a strictly feasible point is known). Exact solutions are not possible in general: the optimum of an SDP with rational data can be irrational, as the next example shows (5\sqrt{5}).

Duality. The dual of the SDP above is min⁡{bTy:∑kykAk−C⪰0}\min \{ b^T y : \sum_k y_k A_k - C \succeq 0 \}. Weak duality always holds (⟨C,X⟩≤bTy\langle C, X \rangle \le b^T y, because the inner product of two PSD matrices is non-negative). Strong duality needs a regularity condition, such as Slater's condition (a strictly feasible X≻0X \succ 0); without it the optimal values can differ, or the optimum need not be attained, which never happens for LPs.

A worked example: the Lovász theta of the 5-cycle

An orthonormal representation of a graph GG assigns unit vectors uiu_i to the vertices so that non-adjacent vertices get orthogonal vectors. Lovász (1979) defined

ϑ(G)=min⁡c, (ui)  max⁡i1(c⋅ui)2,\vartheta(G) = \min_{c,\, (u_i)} \; \max_i \frac{1}{(c \cdot u_i)^2},

over orthonormal representations and unit "handle" vectors cc, and proved α(G)≤ϑ(G)≤χˉ(G)\alpha(G) \le \vartheta(G) \le \bar\chi(G), where α\alpha is the largest independent set and χˉ\bar\chi the clique cover number. ϑ\vartheta can be written as an SDP and computed to any precision, while α\alpha and χˉ\bar\chi are NP-hard; for perfect graphs the three are equal, which gives the only known polynomial-time algorithm for maximum independent sets in perfect graphs (Grötschel, Lovász and Schrijver, 1981).

For the 5-cycle, Lovász's umbrella is optimal: put uku_k on a cone around the handle c=e3c = e_3, at horizontal angle 2πk5\frac{2\pi k}{5}, with the opening chosen so that uk⊥uk+2u_k \perp u_{k+2} (vertices two apart are non-adjacent).

python · try it yourself
import math

z2 = -math.cos(4 * math.pi / 5)                   # u_k . u_(k+2) = 0 needs cos(144 deg) + z^2 = 0
u = []
for k in range(5):
    v = (math.cos(2 * math.pi * k / 5), math.sin(2 * math.pi * k / 5), math.sqrt(z2))
    norm = math.sqrt(sum(t * t for t in v))
    u.append(tuple(t / norm for t in v))
dot = lambda a, b: sum(x * y for x, y in zip(a, b))
print("max |u_k . u_(k+2)| over non-adjacent pairs:", round(max(abs(dot(u[k], u[(k + 2) % 5])) for k in range(5)), 12))
theta = max(1 / dot((0, 0, 1), uk) ** 2 for uk in u)
print("value of the umbrella:", round(theta, 6), " sqrt(5) =", round(math.sqrt(5), 6))
print("alpha(C5) = 2 <= theta <= 3 = clique cover number")
max |u_k . u_(k+2)| over non-adjacent pairs: 0.0
value of the umbrella: 2.236068  sqrt(5) = 2.236068
alpha(C5) = 2 <= theta <= 3 = clique cover number

The value is exactly 5≈2.236\sqrt{5} \approx 2.236, strictly between α(C5)=2\alpha(C_5) = 2 and χˉ(C5)=3\bar\chi(C_5) = 3; Lovász proved that the umbrella is optimal, and used ϑ(C5)=5\vartheta(C_5) = \sqrt{5} to settle Shannon's question about the zero-error capacity of the 5-cycle. An irrational optimum from integer data is typical of SDPs.

Test yourself

Which condition makes a symmetric matrix XX the Gram matrix of some vectors, Xij=vi⋅vjX_{ij} = v_i \cdot v_j?

  1. det⁡X>0\det X > 0
  2. X⪰0X \succeq 0 (all eigenvalues non-negative)
  3. All entries of XX are non-negative
  4. XX is diagonal

How does the ellipsoid method separate a symmetric matrix XX that is not PSD?

  1. It cannot
  2. With an eigenvector aa of a negative eigenvalue: aTXa<0a^T X a < 0 violates the valid linear inequality aTYa≥0a^T Y a \ge 0
  3. By checking the diagonal
  4. By rounding XX

Why are SDPs solved only to within ε\varepsilon rather than exactly?

  1. Because SDP is NP-hard
  2. Because the optimum can be irrational even for rational data, e.g. ϑ(C5)=5\vartheta(C_5) = \sqrt{5}
  3. Because SDPs have no dual
  4. Because the PSD cone is not convex