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 is positive semidefinite (PSD), written , if any of these equivalent conditions holds:
- for every ;
- every eigenvalue of is non-negative;
- for some matrix , that is, for vectors (the columns of ): is a Gram matrix.
The third condition is the one that matters for algorithms. A factorization can be computed from the eigendecomposition as , or by an incomplete Cholesky factorization, in time; the vectors live in with . The program checks two matrices and recovers vectors from the PSD one.
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]
is the Gram matrix of three unit vectors at degrees to each other in a plane (rank 2). asks for three unit vectors that are pairwise opposite, which is impossible, and the negative eigenvalue comes with a vector that proves it: . That vector is a separating hyperplane for the PSD cone ( is a linear inequality in valid for all PSD matrices and violated by ), which is exactly what the ellipsoid method needs.
In C++
A PSD test by Cholesky factorization with a tolerance: succeeds (with zero pivots allowed only when the rest of the column is zero too) exactly when is PSD.
#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 :
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 to optimize a linear function of the inner products subject to linear constraints on them. The dimension comes for free (any Gram matrix of vectors is realised in ), and that freedom is essential: restricting the vectors to 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 , a solution within of the optimum, in time polynomial in the input size and , 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 ().
Duality. The dual of the SDP above is . Weak duality always holds (, 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 ); 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 assigns unit vectors to the vertices so that non-adjacent vertices get orthogonal vectors. Lovász (1979) defined
over orthonormal representations and unit "handle" vectors , and proved , where is the largest independent set and the clique cover number. can be written as an SDP and computed to any precision, while and 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 on a cone around the handle , at horizontal angle , with the opening chosen so that (vertices two apart are non-adjacent).
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 , strictly between and ; Lovász proved that the umbrella is optimal, and used 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 the Gram matrix of some vectors, ?
- (all eigenvalues non-negative)
- All entries of are non-negative
- is diagonal
How does the ellipsoid method separate a symmetric matrix that is not PSD?
- It cannot
- With an eigenvector of a negative eigenvalue: violates the valid linear inequality
- By checking the diagonal
- By rounding
Why are SDPs solved only to within rather than exactly?
- Because SDP is NP-hard
- Because the optimum can be irrational even for rational data, e.g.
- Because SDPs have no dual
- Because the PSD cone is not convex