Recognizing Partial Cubes in Quadratic Time

David Eppstein Computer Science Dept. Univ. of California, Irvine

ACM-SIAM Symposium on Discrete Algorithms, 2008 Context: Geometric graphs and metric embedding

Graph theory: Geometry:

Unweighted graphs Real vector spaces

Weighted graphs Integer lattices

Finite metric spaces Euclidean distances

L1 distances

L∞ distances Probabilistic embedding

Bourgain’s theorem

Johnson-Lindenstrauss lemma ... Context: Geometric graphs and metric embedding

Graph theory: Geometry:

Unweighted graphs Real vector spaces

Weighted graphs Integer lattices

Finite metric spaces Euclidean distances

L1 distances

L∞ distances Probabilistic tree embedding

Bourgain’s theorem

Johnson-Lindenstrauss lemma ... Partial cubes as geometric graphs

111111 111110 011111 Partial cube: 011110

Undirected graph that can be 110111 111100 001111 embedded into an integer lattice 011010 so that graph distance = L1 distance 110101 100111 110100 000111

At expense of high dimension 111000 can restrict coordinates to 0 or 1 011000 001010 001011 100101 L1 distance = Hamming distance: isometric hypercube subgraph 110000 000011 001000 100001

100000 000001

000000 Example: permutahedron (vertices = permutations of 4 items edges = flips of adjacent items) Application: Preference modeling in mathematical behavioral sciences

Given a fixed set of candidates a < b < c

Model voter states as vertices a < b a < c a < c b < c Possible state transitions as edges a < c < b b < a < c

Several natural families of orderings define partial cubes in this way: a < b Ø b < a c < b b < c • total orderings c < a < b b < c < a c < a b < a • partial orderings c < b c < a

• weak orderings c < b < a (total orders with ties) Application: Modeling knowledge of students

ABCDEF State of knowledge = set of concepts the student understands BCDEF

Assume: ABCDF

Any state can be reached by learning ABDF BCDF one concept at a time ABCDE

Union of two states is another state ABCD BCDE

ABCEF Then family of states is an , ABD BCD a special case of a partial cube BD ABCE

BCE This theory is used by ALEKS Corp. in their ABC educational software for high school mathematics AB BC

B

AC

A

Ø Application: flip distances in computational geometry

Vertices = triangulations (here, of 3x3 grid)

Edges = change triangulation by one edge (“flip”)

Important open problem in algorithms: compute flip distance

Flip graph is a partial cube iff no empty pentagon, polynomial time in this case For more applications... Algorithmic problem: efficiently recognize partial cubes

Given as input an undirected graph, produce as output a labeling, and check that the labeling preserves distances

Known: O(nm) time [Aurenhammer and Hagauer, 1995] Note that O(nm) is O(n2 log n) because partial cubes have O(n log n) edges

Lower bound: output may have Ω(n2) bits (e.g. when input is a tree)

New result: O(n2) time Graph-theoretic characterization

Djokovic–Winkler relation on graph edges [Djokovic 1973, Winkler 1984]:

(p,q) ~ (r,s) iff d(p,r) + d(q,s) ≠ d(p,s) + d(q,r) D D

D D + 2

related edges unrelated edges

G is a partial cube iff it is bipartite and DW-relation is an equivalence relation

Equivalence classes cut graph into two connected subgraphs

0-1 lattice embedding: coordinate per class, 0 in one subgraph, 1 in the other unique up to hypercube symmetries Partial cube as finite state machine

Input token (i,j): set ith bit to j, if possible otherwise, leave state unchanged

001 (2,1) (0,1) (0,0) (2,0) (3,1) 000 011 111 (2,1) (0,1) (3,0) (2,0) (0,0) 010 Automaton-theoretic characterization

Medium [e.g. Falmagne and Ovchinnikov 2002]:

System of states and transformations of states (“tokens”)

Every token τ has a “reverse” τR: for any two states S ≠ V, Sτ = V iff VτR = S

Any two states can be connected by a “concise message”: sequence of at most one from each token-reverse pair

If a sequence of effective tokens returns a state to itself then its tokens can be matched into token-reverse pairs

States and adjacencies between states form vertices and edges of a partial cube Fundamental components of a partial cube

Vertices and edges, as in any graph, but also: equivalence classes of DW-relation (“zones”)

alternatively: 111111 set second coord to 1 111110 110111 011111 tokens or token-reverse pairs 011110 111100 110101 coordinates of cube embedding 100111 001111 110100 000111 semicubes (subgraphs cut by 011010 100101 111000 equivalence classes) 001011 110000 011000 100001 001010 000011

100000 001000 set second 000001 coord to 0 000000 The Algorithm — overall outline

I. Find a labeling (distance-preserving iff the input is a partial cube)

Uses Djokovic–Winkler relation

Sped up by bit-parallel programming techniques

II. Check whether it’s distance-preserving

Based on fast all-pairs shortest path algorithm for media

Uses media-theoretic characterization The Algorithm — finding a labeling

Perform a breadth first search from a high-degree root vertex

Label each node by a bitvector Indicating which neighbors of root it can connect through

Label edge by exclusive or of endpoint labels (should be either zero or single bit)

Sets of edges with same nonzero labels = Djokovic-Winkler classes

Contract labeled edges and continue in remaining graph The Algorithm — finding a labeling

Example:

q 0 [pq1] 1000 1100

[pq2] [pq2] [pq0] [pq0]

q1 1010 [pq1] 1110 0110 [pq1] 0100

[pq0] p [pq2] [pq0] 0000 [pq2] q2 0110 0110 [pq3] 0111 [pq ] 3 0010 [pq1] [pq3] [pq3]

q3 [pq ] [pq1] 0001 2 0011 0111 0111 The Algorithm — checking the labeling

Perform a depth-first traversal of the graph, maintaining:

• a list of tokens on shortest paths to the current vertex (one token from each token-reverse pair)

• for each other vertex, the first effective token on the list

When the depth-first traversal moves to another vertex:

• remove the corresponding token from the list, and add its reverse to the end of the list

• for each vertex pointing to the removed token, search forwards for the next effective token

If the search runs off the end of the list, the graph is not a partial cube The Algorithm — analysis

I. Finding the labeling

Search from degree d vertex finds d ≥ m/n tokens using O(m) bitvector operations taking time O(1 + d/log n) per bitvector operation

Total per token: O(m/d + m/log n) = O(n)

Whole graph has O(n) tokens, so O(n2) total

II. Checking whether it’s distance-preserving

Total number of tokens added to end of list: O(n)

Each node scans list once, so O(n2) total The Algorithm — implementation

220 lines of Python (approximately 1/3 of which are unit tests) http://www.ics.uci.edu/~eppstein/PADS/PartialCube.py

Two problematic graphs (minor bugs in implementation, both fixed, no change to algorithm):

left: crashed the program right: incorrectly reported as a partial cube