+ All Categories
Home > Documents > Recognizing Partial Cubes in Quadratic Time › pdf › 0705.1025.pdfRecognizing Partial Cubes in...

Recognizing Partial Cubes in Quadratic Time › pdf › 0705.1025.pdfRecognizing Partial Cubes in...

Date post: 29-Jun-2020
Category:
Upload: others
View: 4 times
Download: 0 times
Share this document with a friend
25
Journal of Graph Algorithms and Applications http://jgaa.info/ vol. 15, no. 2, pp. 269–293 (2011) Recognizing Partial Cubes in Quadratic Time David Eppstein 1 1 Computer Science Department, University of California, Irvine Abstract We show how to test whether a graph with n vertices and m edges is a partial cube, and if so how to find a distance-preserving embedding of the graph into a hypercube, in the near-optimal time bound O(n 2 ), improving previous O(nm)-time solutions. arXiv:0705.1025v2 [cs.DS] 19 Jul 2011
Transcript
Page 1: Recognizing Partial Cubes in Quadratic Time › pdf › 0705.1025.pdfRecognizing Partial Cubes in Quadratic Time David Eppstein1 1Computer Science Department, University of California,

Journal of Graph Algorithms and Applicationshttp://jgaa.info/ vol. 15, no. 2, pp. 269–293 (2011)

Recognizing Partial Cubes in Quadratic Time

David Eppstein 1

1Computer Science Department, University of California, Irvine

Abstract

We show how to test whether a graph with n vertices and m edges is apartial cube, and if so how to find a distance-preserving embedding of thegraph into a hypercube, in the near-optimal time bound O(n2), improvingprevious O(nm)-time solutions.

arX

iv:0

705.

1025

v2 [

cs.D

S] 1

9 Ju

l 201

1

Page 2: Recognizing Partial Cubes in Quadratic Time › pdf › 0705.1025.pdfRecognizing Partial Cubes in Quadratic Time David Eppstein1 1Computer Science Department, University of California,

270 Eppstein Recognizing Partial Cubes in Quadratic Time

1 Introduction

A partial cube is an undirected and unweighted graph that admits a simpledistance-labeling scheme: one can label its vertices by bitvectors in such a waythat the distance between any two vertices equals the Hamming distance be-tween the corresponding labels (Figure 1). That is, the graph can be isometri-cally embedded into a hypercube.

Graham and Pollak [26] were the first to discuss partial cubes, for an appli-cation involving communication networks. Since then, these graphs have beenshown to model a large variety of mathematical systems:

• In computational geometry, the adjacencies between the cells in any hyper-plane arrangements (represented as a graph with a vertex per cell and anedge between any two cells that share a facet) forms a partial cube [21,35].As a second geometric example, the flip graphs of triangulations of certainpoint sets also form partial cubes, a fact that can be used to compute flipdistance efficiently for these triangulations [19].

• In order theory, the family of total orders over a finite set (with adjacencydefined by transpositions), the family of linear extensions of a finite par-tially ordered set (again with adjacency defined by transpositions), thefamily of partial orders of a finite set (with adjacency defined by inclu-sion or removal of an order relation between a single pair of items), andthe family of strict weak orders on a finite set (with adjacency defined byinclusion or removal of a separation of the items into two subsets, one ofwhich is less than the other in the weak order) all form partial cubes [21].For instance, the permutohedron shown in Figure 1 can be interpreted asthe graph of total orders of a four-element set.

• In the combinatorial study of human learning, antimatroids (called in thiscontext “learning spaces”) form a standard model of the sets of conceptsthat a student could feasibly have learned: they are defined by the axiomsthat such a set may be learned a single concept at a time, and that theunion of two feasible sets is another feasible set. In this context, the statespace of a learner (a graph with a vertex for each feasible set and an edgeconnecting any two sets that differ in a single concept) forms a partialcube [13,21].

• In organic chemistry, the carbon backbones of certain benzenoid moleculesform partial cubes [36], and partial cube labelings of these graphs can beapplied in the calculation of their Wiener indices [31].

Partial cubes admit more efficient algorithms than arbitrary graphs for sev-eral important problems including unweighted all-pairs shortest paths [20], andare the basis for several graph drawing algorithms [14,16,18,22].

Page 3: Recognizing Partial Cubes in Quadratic Time › pdf › 0705.1025.pdfRecognizing Partial Cubes in Quadratic Time David Eppstein1 1Computer Science Department, University of California,

JGAA, 15(2) 269–293 (2011) 271

111111011111

011110

111110

111100

111000

110000

011000

110100

011010

110101

110111

100111

100101

001010

001000

100000

100001

000000

000001

000011

000111

001111

001011

Figure 1: A partial cube, with labeled vertices. The distance between any pairof vertices equals the Hamming distance between the corresponding labels, adefining property of partial cubes.

1.1 New Results

In this paper we study the problem of recognizing partial cubes and assigninglabels to their vertices. We show that both problems can be solved in timeO(n2), where n is the number of vertices in the input graph. Our algorithm hastwo phases:

• In the first phase, we assign bitvector labels to each vertex. It would bestraightforward, based on previously known characterizations of partialcubes, to assign a single coordinate of each of these labels by perform-ing a single breadth-first search of the graph; however, the labels mayrequire as many as n− 1 coordinates, and performing n− 1 breadth-firstsearches would be too slow. To speed this approach up, we use the bit-levelparallelism inherent in computer arithmetic to assign multiple coordinatevalues in a single breadth-first pass over the graph. This part of our al-gorithm depends on a RAM model of computation in which integers ofat least log n bits may be stored in a single machine word, and in whichaddition, bitwise Boolean operations, comparisons, and table lookups canbe performed on log n-bit integers in constant time per operation. Theconstant-time assumption is standard in the analysis of algorithms, andany machine model that is capable of storing an address large enough toaddress the input to our problem necessarily has machine words with atleast log n bits.

Page 4: Recognizing Partial Cubes in Quadratic Time › pdf › 0705.1025.pdfRecognizing Partial Cubes in Quadratic Time David Eppstein1 1Computer Science Department, University of California,

272 Eppstein Recognizing Partial Cubes in Quadratic Time

• In the second phase, we verify that the labeling we have constructed isindeed distance-preserving. The labels produced in the first phase can beguaranteed to have a Hamming distance that is either equal to the graphdistance, or an underestimate of the graph distance; therefore, in order toverify that the labeling is distance-preserving, it suffices to construct pathsbetween each pair of vertices that are as short as the Hamming distancebetween their labels. To find these paths, we modify an algorithm fromprevious work with the author and Falmagne [20] that computes all pairsshortest paths in unweighted partial cubes. The modified algorithm eitherproduces paths that are as short as the Hamming distance for each pairof vertices, verifying that the distance labeling is correct, or it detects aninconsistency and reports that the input graph is not a partial cube.

Our running time, O(n2), is in some sense close to optimal, as the output ofthe algorithm, a partial cube labeling of the input graph, may consist of Ω(n2)bits. For instance, labeling a tree as a partial cube requires n− 1 bits per label.However, in our computational model, such a labeling may be represented inO(n2/ log n) words of storage, so the trivial lower bound on the runtime of ourchecking algorithm is Ω(n2/ log n). Additionally, in the case of partial cubesthat have labelings with few bits per label, or other forms of output than anexplicit bitvector labeling of the vertices, even faster runtimes are not ruledout. We leave any further improvements to the running time of partial cuberecognition as an open problem.

1.2 Related Work

Partial Cube Recognition. Since the time they were first studied, it hasbeen of interest to recognize and label partial cubes. Djokovic [12] and Win-kler [38] provided mathematical characterizations of partial cubes in terms ofcertain equivalence relations on the edges; their results can also be used to de-scribe the bitvector labeling of the vertices of a partial cube, and to show that itis essentially unique when it exists. As Imrich and Klavzar [29] and Aurenham-mer and Hagauer [2] showed, these characterizations can be translated directlyinto algorithms for recognizing partial graphs in time O(mn), where m and nare respectively the number of edges and vertices in the given graph.1 Sincethen there has been no improvement to the O(mn) time bound for this problemuntil our work.

Special Subclasses of Partial Cubes. Several important families of graphsare subclasses of the partial cubes, and can be recognized more quickly thanarbitrary partial cubes:

• Every tree is a partial cube [34], and obviously trees can be recognized inlinear time.

1As we discuss later, for partial cubes, m ≤ n log2 n; the time bound claimed in the titleof Aurenhammer and Hagauer’s paper is O(n2 logn), which is therefore slower than O(mn),but it is not hard to see that their algorithm actually takes time O(mn).

Page 5: Recognizing Partial Cubes in Quadratic Time › pdf › 0705.1025.pdfRecognizing Partial Cubes in Quadratic Time David Eppstein1 1Computer Science Department, University of California,

JGAA, 15(2) 269–293 (2011) 273

• Squaregraphs are the planar graphs that can be drawn in the plane insuch a way that every bounded face has four sides and every vertex withdegree less than four belongs to the unbounded face. Every squaregraphis a partial cube, and squaregraphs may be recognized in linear time [4].

• A median graph is a graph in which, for every three vertices, there is aunique median vertex that belongs to shortest paths between each pair ofthe three vertices [3,9,33]. The graphs of distributive lattices are mediangraphs [9]; median graphs also arise from the solution sets of 2-satisfiabilityproblems [24] and the reconstruction of phylogenetic trees [6,11]. Based onearlier work by Hagauer et al. [27], Imrich et al. [30] showed that the timesfor median graph recognition and for triangle-free graph recognition arewithin polylogarithmic factors of each other. Applying the best knownalgorithm for triangle detection, based on fast matrix multiplication [1]yields a time bound of O(n1.41) for median graph recognition.

• Bresar et al. [10] discuss several other classes of partial cubes that areclosely related to the median graphs and may be recognized in O(m log n)time.

Other Distance Labeling Schemes. The assignment of bitvectors to ver-tices in a partial cube is a form of a distance labeling scheme, an assignmentof labels to vertices in arbitrary graphs that allows distances to be computedfrom the labels [25]. Although bitvectors provide a convenient representationof distances in partial cubes, they are not the only possible scheme for distancelabeling, and other schemes may be more concise. The isometric dimensionof a partial cube is the number of bits needed in each bitvector label, and asdiscussed above it may be as high as n− 1.

Every partial cube may be embedded in a distance-preserving way into aninteger lattice Zd of some dimension d. One such labeling simply uses each bitof a bitvector labeling as a coordinate in Zd; however, some graphs may beembeddable into integer lattices of much lower dimension than their isometricdimension. For instance, a path graph can be embedded into Z, and given one-dimensional coordinates that accurately describe the graph distances, despitehaving an isometric dimension of n− 1. The lattice dimension of a partial cubeis the minimum number d for which the graph admits a distance-preserving em-bedding into Zd. The lattice dimension, and an embedding of that dimension,may be found in polynomial time using an algorithm based on graph match-ing [15], but this algorithm depends on having as input a bitvector labeling andis slower than the algorithm we describe here, so it does not form the basis ofan efficient partial cube recognition algorithm.

It may also be possible to express a partial cube as a distance-preservingsubgraph of a Cartesian product of trees, using many fewer trees than the lat-tice dimension of the graph. For instance, the star K1,n−1 has lattice dimensiondn−12 e despite being a single tree [34]. Any individual tree admits a distance

labeling scheme with O(log2 n)-bit labels [25]; even more concisely, it is possi-

Page 6: Recognizing Partial Cubes in Quadratic Time › pdf › 0705.1025.pdfRecognizing Partial Cubes in Quadratic Time David Eppstein1 1Computer Science Department, University of California,

274 Eppstein Recognizing Partial Cubes in Quadratic Time

ble to assign O(log n)-bit identifiers to the nodes of a tree in such a way thatpairwise distances can be looked up in constant time per query, based on lowestcommon ancestor data structures [8, 28]. Therefore, finding small tree productrepresentations would be of interest as a method of efficient distance representa-tion in these graphs. However, although it is possible to find a representation asa subgraph of a product of two trees in linear time, when such a representationexists [5], it is NP-hard to find optimal representations using larger numbers oftrees or even to find accurate approximations of the optimal number of treesneeded in such a representation, due to a reduction from graph coloring [7].

1.3 Organization

The remainder of this paper is organized as follows. In Section 2 we review acharacterization of partial cubes by Winkler [38]. Winkler characterizes partialcubes in terms of an equivalence relationship defined on the edges of the graphby an inequality between sums of pairs of distances; this characterization iscentral to past partial cube recognition algorithms as well as our own. In thissection we also review other standard results on partial cubes needed in ourwork. In Section 3 we describe how to find a single bit within each vertex labelof a partial cube by using Winkler’s characterization as part of an algorithmbased on breadth-first search, and in Section 4 we show how to find multiple bitsof each label by a single pass of breadth-first search. In Section 5 we show howthis method leads to an efficient algorithm for finding the complete bitvectorlabels of each vertex. In Section 6 we review our previous algorithm for all-pairsshortest paths in partial cubes and examine its behavior on graphs that mightnot be partial cubes, and in Section 7 we show how to use this algorithm totest whether the labeling we have constructed is valid. Section 8 reports on aproof-of-concept implementation of our algorithms. We conclude in Section 9.

2 Preliminaries

The characterizations of partial cubes by Djokovic [12] and Winkler [38] bothdepend on defining certain relations on the edges of the graph that, in thecase of partial cubes, can be shown to be equivalence relations. Moreover,although Djokovic’s and Winkler’s relations may differ from each other on ar-bitrary graphs, they are identical on partial cubes. It will be more convenientfor our purposes to start with the formulation of Winkler. Therefore, followingWinkler, define a relation ∼G on the edges of an undirected graph G, by settingpq ∼G rs if and only if d(p, r) + d(q, s) 6= d(p, s) + d(q, r), where d denotes thenumber of edges in the shortest path between two vertices.

This relation is automatically reflexive in any graph without self-loops: forevery edge pq, pq ∼G pq. It is also symmetric: if pq ∼G rs then rs ∼G pq, andvice versa. It also does not depend on the ordering of the two endpoints of theedges it relates. These are two of the three defining properties of an equivalencerelation, the third being transitivity.

Page 7: Recognizing Partial Cubes in Quadratic Time › pdf › 0705.1025.pdfRecognizing Partial Cubes in Quadratic Time David Eppstein1 1Computer Science Department, University of California,

JGAA, 15(2) 269–293 (2011) 275

b

e

c d

a ce

ab

de ac

be

ad

Figure 2: An example of Winkler’s relationship, for the graph G = K2,3 (left).In this graph, each edge is related to the two other edges that it does not sharean endpoint with; the right side of the figure shows pairs of edges that arerelated to each other. In this graph, ∼G is not an equivalence relationship; forinstance, ab ∼G ce ∼G ad, but ab 6∼G ad. Thus, by Winkler’s characterization,K2,3 is not a partial cube.

For example, if pqrs form a path, with no additional edges connecting thesefour vertices, then pq 6∼G rs because d(p, r) + d(q, s) = 2 + 2 = 3 + 1 = d(p, s) +d(q, r). On the other hand, if pqrs form a 4-cycle, again with no additionaledges, then pq ∼G rs because d(p, r) +d(q, s) = 2 + 2 6= 1 + 1 = d(p, s) +d(q, r).Figure 2 shows a more complicated example of a graph K2,3 with six edges, andthe Winkler relation among these edges.

Lemma 1 (Winkler) Graph G is a partial cube if and only if G is bipartiteand ∼G is an equivalence relation.

Referring again to the example in Figure 2, the transitive property does nothold: for instance, ab ∼G ce, and ce ∼G ad, but ab 6∼G ad. Therefore, for thisexample, ∼G is not an equivalence relation and Winkler’s lemma tells us thatthe graph K2,3 shown in the figure is not a partial cube.

We will use [e] to denote the set of edges related to an edge e by ∼G (thatis, in the case that G is a partial cube, the equivalence class of e).

If G is a partial cube, and e = pq is any edge of G, then let Spq denote theset of vertices nearer to p than to q, and Sqp denote the set of vertices nearerto q than to p. (There can be no ties in a bipartite graph.) The sets Spq andSqp were called semicubes in our algorithm for lattice embeddings of partialcubes [15], where they play a key role, and they are also central to Djokovic’sand Winkler’s characterizations of partial cubes. Equivalently, Spq must consistof the vertices whose labels match that of p in the coordinate at which the labelsof p and q differ, and Sqp must consist of the vertices whose labels match thatof q in the same coordinate. The edges separating these two subsets are exactlythe edges in [e], and both of these two subsets must be connected (since everypair of vertices in one of these two subsets can be connected by a path that doesnot change the label at the coordinate that they share with p or q).

Page 8: Recognizing Partial Cubes in Quadratic Time › pdf › 0705.1025.pdfRecognizing Partial Cubes in Quadratic Time David Eppstein1 1Computer Science Department, University of California,

276 Eppstein Recognizing Partial Cubes in Quadratic Time

Thus, as shown by Winkler, in a partial cube, each equivalence class [e]forms an edge cut partitioning the graph into two connected components, andthe partial cube labeling for G has a coordinate i such that the ith bit in alllabels for vertices in one of the two components is 0, and the same bit in alllabels for vertices in the other component is 1. The dimension of the partial cubelabeling (the isometric dimension of the graph) equals the number of equivalenceclasses of ∼G, and the labeling itself is essentially unique up to symmetries ofthe hypercube.

It will be important for our algorithms to observe that any partial cubewith n vertices has at most n log n edges. This appears to be folklore (see e.g.Lemma 3 of Matousek [32]) but we repeat for completeness a proof, copied (indifferent terminology) from Lemma 4 of [20].

Lemma 2 In any n-vertex partial cube, the number of edges is at most n log2 n.

Proof: We apply induction on the isometric dimension. As a base case, if thereis only one vertex there can be no edges. Otherwise, let e = uv be any edgein the graph, partition the graph into two components Gu and Gv, and assumewithout loss of generality that |Gu| ≤ |Gv|. Then both Gu and Gv inducepartial cubes, which have a number of edges that can be bounded by inductionto the same formula of their numbers of vertices. In addition, the number ofedges in [e] is at most |Gu|, because each edge has an endpoint in Gu and eachvertex in Gu can be the endpoint for at most one edge. (If it were the endpointof two edges in [e], the other endpoints of those edges would have equal labels,contradicting their nonzero distance from each other.)

So, if M(n) denotes the maximum number of edges in any n-vertex partialcube, we have a recurrence

M(n) ≤ maxM(a) +M(b) + min(a, b) | a+ b = n

which can be used in an induction proof to derive the desired bound.

3 Finding a single edge class

Given a graph G and an edge pq of G, it is straightforward to construct theset [pq] of edges related to pq by ∼G: perform two breadth first searches, onestarting from p and another starting from q, using the resulting breadth firstsearch trees to calculate all distances from p or q to other vertices of the graph,and then apply the definition of Winkler’s relation ∼G to test whether eachother edge of the graph belongs to [pq] in constant time per edge. We begin thedescription of our algorithm by showing how to simplify this construction: wemay find [pq] by an algorithm that performs only a single breadth first searchrather than two searches. Moreover, we need not calculate any distances aspart of this computation. This simplification will be an important step of ouroverall result, as it will eventually allow us to construct multiple equivalenceclasses of edges simultaneously, in less time than it would take to perform eachconstruction separately.

Page 9: Recognizing Partial Cubes in Quadratic Time › pdf › 0705.1025.pdfRecognizing Partial Cubes in Quadratic Time David Eppstein1 1Computer Science Department, University of California,

JGAA, 15(2) 269–293 (2011) 277

Our technique is based on the following observation:

Lemma 3 Let pq be an edge in a bipartite graph G. Then pq ∼G rs if and onlyif exactly one of r and s has a shortest path to p that passes through q.

Proof: If neither r nor s has such a path, then d(q, r) = d(p, r) + 1 andd(q, s) = d(p, s) + 1, so d(p, r) + d(q, s) = d(p, r) + 1 + d(p, s) = d(q, r) + d(p, s)by associativity of addition, and pq 6∼G rs. Similarly, if both r and s have suchpaths, then d(q, r) = d(p, r) − 1 and d(q, s) = d(p, s) − 1, so d(p, r) + d(q, s) =d(p, r) − 1 + d(p, s) = d(q, r) + d(p, s). Thus in neither of these cases can pqand rs be related. If, on the other hand, exactly one of r and s has such apath, we may assume (by swapping r and s if necessarily that it is r that hasthe path through q. Then d(q, r) = d(p, r) − 1 while d(q, s) = d(p, s) + 1, sod(p, r) + d(q, s) = d(p, r) + d(p, s) + 1 6= d(p, r)− 1 + d(p, s) = d(q, r) + d(p, s),so in this case pq ∼G rs.

Thus, to find the edge class [pq] in a bipartite graph G, we may performa breadth first search rooted at p, maintaining an extra bit of information foreach vertex v traversed by the search: whether v has a shortest path to p thatpasses through q. This bit is set to false initially for all vertices except for q, forwhich it is true. Then, when the breadth first search traverses an edge from avertex v to a vertex w, such that w has not yet been visited by the search (andis therefore farther from p than v), we set the bit for w to be the disjunction ofits old value with the bit for v. Note that we perform this update for all edgesof the graph, regardless of whether the edges belong to any particular breadthfirst search tree.

Recall that Spq denotes the set of vertices nearer to p than to q. It will beimportant to the correctness of our algorithm to make the following additionalobservation.

Lemma 4 If G is bipartite, then for any edge pq the semicubes Spq and Sqppartition G into two subsets, and the edge class [pq] forms the cut between thesetwo semicubes.

Proof: This follows immediately from the previous lemma, since Sqp consistsexactly of the vertices that have a shortest path to p passing through q.

We remark that this description of edge classes [pq] in terms of semicubesis very close to Djokovic’s original definition of an equivalence relation on theedges of a partial cube. Thus, for bipartite graphs, Winkler’s definition (whichwe are following here) and Djokovic’s definition can be shown to coincide.

4 Finding several edge classes

As we now show, we can apply the technique described in the previous sectionto find several edge classes at once. Specifically, we will find classes [pq] foreach neighbor q of a single vertex p, by performing a single breadth first searchrooted at p.

Page 10: Recognizing Partial Cubes in Quadratic Time › pdf › 0705.1025.pdfRecognizing Partial Cubes in Quadratic Time David Eppstein1 1Computer Science Department, University of California,

278 Eppstein Recognizing Partial Cubes in Quadratic Time

p0000

q01000

q10100

q20010

q30001

1010

1100

0110 0110

1110

0011 0111 0111

0111

0110

Figure 3: The vertex-labeling stage of the algorithm of Lemma 7. The breadthfirst search tree edges are shown darker than the other edges; the left-to-rightplacement of the vertices is determined by their distance from the starting vertexp. Except for the neighbors qi of the starting vertex, the bitvector shown foreach vertex is the disjunction of the bitvectors of its neighbors to the left.

Lemma 5 Let pq and pr be edges in a bipartite graph G. Then pq 6∼G pr.

Proof: By bipartiteness, d(q, r) = 2, so d(p, p) + d(q, r) = 2 = 1 + 1 = d(p, r) +d(q, p).

Our algorithm will need efficient data structures for storing and manipulatingbit vectors, which we now describe. As described in the introduction, we assumethroughout that arithmetic and bitwise Boolean operations on integers of at leastlog n bits, as well as array indexing operations, are possible in constant time.

Lemma 6 Let k be a given number, and let K = 1+k/ log n. Then it is possibleto store bitvectors with k bits each in space O(K) per bitvector, and performdisjunction operations and symmetric difference operations in time O(K) peroperation. In addition, in time O(K) we can determine whether a bitvectorcontains any nonzero bits. If it does, in time O(K) we can determine whetherit has exactly one nonzero bit, and if so find the index of that bit, using a singleprecomputed external table of size n.

Proof: We store a bitvector in dKe words, by packing log n bits per machineword. Disjunction and symmetric difference can be performed independently oneach of these words. To test whether a bitvector is nonzero, we use a comparisonoperation to test whether each of its words is nonzero. To test whether abitvector has exactly one nonzero bit, and if so find out which bit it is, we againuse comparisons to test whether there is exactly one word in its representationthat is nonzero, and then look up that word in a table that stores either theindex of the nonzero bit (if there is only one) or a flag value denoting that thereis more than one nonzero bit.

Page 11: Recognizing Partial Cubes in Quadratic Time › pdf › 0705.1025.pdfRecognizing Partial Cubes in Quadratic Time David Eppstein1 1Computer Science Department, University of California,

JGAA, 15(2) 269–293 (2011) 279

p0000

q01000

q10100

q20010

q30001

1010

1100

0110 0110

1110

0011 0111 0111

0111

0110

[pq0][pq0]

[pq1]

[pq1]

[pq1]

[pq1]

[pq1]

[pq2]

[pq2]

[pq2] [pq2]

[pq0] [pq0][pq2]

[pq3]

[pq3] [pq3]

[pq3]

Figure 4: The edge-labeling stage of the algorithm of Lemma 7. If the bitvectorsof the endpoints of an edge differ only in their ith bits, the edge is included inclass [pqi]. If the bitvectors of the endpoints are the same, the edge is notincluded in any class. If there were an edge that had bitvectors differing inmore than one bit, the graph would not be a partial cube.

We are ready to specify the main algorithm of this section, for finding acollection of edge classes of our supposed partial cube.

Lemma 7 Let G be any graph with n vertices and m edges. Then there is analgorithm which either determines that G is not a partial cube (taking time atmost O(n2) to do so) or finds a collection E of disjoint sets of edges [ei], with|E| ≥ 2m/n, taking time O(|E| · n) to do so where |E| is the number of sets inthe collection. In the latter case, the algorithm can also label each vertex of Gby the set of semicubes it belongs to among the semicubes corresponding to theedges ei, in the same total time.

Proof: We first check that G is bipartite; if not, it cannot be a partial cube.We also check that its number of edges is at most n log2 n, and if not we againreport that it is not a partial cube. We then let p be a vertex of maximumdegree in G. We denote by d the degree of p, which must be at least 2m/n. Wedenote the d neighbors of p in G by qi, for an index i satisfying 0 ≤ i < d.

We create, for each vertex of G, a data structure Dv with d bits Dv[i]. BitDv[i] will eventually be 1 if v has a shortest path to p that passes throughqi (that is, if v ∈ Sqip); initially, we set all of these bits to 0 except that weset Dqi [i] = 1. Next, we perform a breadth first traversal of G, starting at p.When this traversal finds an edge from a vertex v to a vertex w that has not yetbeen traversed (so w is farther from p than v), it sets all bits Dw[i] to be thedisjunction of their previous values with the corresponding bits Dv[i], as shownin Figure 3.

Finally, once the breadth first search is complete and all data structuresDv have reached their final values, we examine each edge vw in the graph. IfDv = Dw, we ignore edge vw, as it will not be part of our output collection.

Page 12: Recognizing Partial Cubes in Quadratic Time › pdf › 0705.1025.pdfRecognizing Partial Cubes in Quadratic Time David Eppstein1 1Computer Science Department, University of California,

280 Eppstein Recognizing Partial Cubes in Quadratic Time

Otherwise, we compute a bitvector B as the symmetric difference of Dv andDw. If B contains two or more nonzero bits B[i] and B[j], then vw belongsto both [pqi] and [pqj ], and G cannot be a partial cube; if we ever encounterthis condition we terminate the algorithm and report that the graph is not apartial cube. Otherwise, we assign vw to the class [pqi] for which B[i] is nonzero.Figure 4 shows this assignment of edges to classes for the example graph shownin Figure 3.

The result of this algorithm is a collection E of disjoint sets of edges [pqi],as the lemma requires; the number of sets in the collection is d. All stages ofthe algorithm perform O(m) steps, each one of which involves at most O(1) ofthe bitvector operations described by Lemma 6, so the total time is O(m(1 +d/ log n)) = O(d(m/d+m/ log n)) = O(dn). Since d ≤ n, this bound is O(n2),as the lemma states for the time taken when the input is determined not tobe a partial cube, and since d = |E| the time is O(|E|n) when the algorithmsuccessfully constructs a set of edge classes.

The semicube labeling output described by the statement of the lemma isrepresented by the data structures Dv computed as part of the algorithm.

5 Finding all edge classes

In order to recognize a partial cube, we need to partition its edges into equiv-alence classes of the relation ∼G, and then verify that the resulting labeling iscorrect. The algorithm of the previous section allows us to find some of theseequivalence classes efficiently, but as it depends for its efficiency on starting froma high degree vertex we will not necessarily be able to use it multiple times onthe same graph. In order to reapply the algorithm and find all equivalenceclasses efficiently, as we now describe, we will need to remove from the graphthe parts we have already recognized.

Lemma 8 Let G be a partial cube, let pq be an edge in G, and let G′ be thegraph formed from G by contracting all edges in [pq]. For any edges e and f inG, neither of which belong to [pq], let e′ and f ′ denote the corresponding edgesin G′. Then e ∼G f if and only if e′ ∼G′ f ′.

Proof: If e and f are not in [pq], by Lemma 4, either both edges connectvertices in one of the two semicubes Spq and Sqp, or one edge is entirely in onesemicube and the other edge is in the other semicube. If both are in the samesemicube, then no shortest path from any vertex of e to any vertex of f can usean edge of [pq] (for if it did, that crossing would increase rather than decreasethe Hamming distance of the path vertex’s labels), so the distances d(x, y) usedin the definition of ∼G′ remain unchanged from those used to define ∼G. If, onthe other hand, e and f are in opposite semicubes, then by similar reasoningevery shortest path from an endpoint of e to a vertex of f must use exactly oneedge of [pq], and each distance d(x, y) used in the definition of ∼G′ is exactlyone smaller than the corresponding distance in the definition of ∼G. Since we

Page 13: Recognizing Partial Cubes in Quadratic Time › pdf › 0705.1025.pdfRecognizing Partial Cubes in Quadratic Time David Eppstein1 1Computer Science Department, University of California,

JGAA, 15(2) 269–293 (2011) 281

p0000

q01000

q10100

q20010

q30001

1010

1100

0110 0110

1110

0011 0111 0111

0111

0110

[pq0][pq0]

[pq1]

[pq1]

[pq1]

[pq1]

[pq1]

[pq2]

[pq2]

[pq2] [pq2]

[pq0] [pq0][pq2]

[pq3]

[pq3] [pq3]

[pq3]

Figure 5: A four-vertex path graph formed by contracting the labeled edgesfrom Figure 4.

are subtracting two units of distance total from each side of the inequality bywhich ∼G′ is defined, it remains unchanged from ∼G.

Lemma 9 Let G be a partial cube, let pq be an edge in G, and let G′ be thegraph formed from G by contracting all edges in [pq]. Then G′ is a partial cube,the equivalence classes of edges in G′ correspond with those in G except for [pq],and the vertex labeling of G′ is formed by omitting the coordinate correspondingto [pq] from the vertex labeling of G.

Proof: By Lemma 8, ∼G′ coincides with ∼G on the remaining edges; thus, it isan equivalence relation, G′ is a partial cube, and its equivalence classes corre-spond with those of G. Since the vertex labeling is formed from the semicubesof G′, which are derived from the cuts formed by equivalence classes of edges,they also correspond in the same way.

Lemma 10 Any partial cube with n vertices has at most n−1 edge equivalenceclasses.

Proof: Choose arbitrarily a vertex v. For any edge equivalence class [pq], withp closer to v than q is, any shortest path from v to q must pass through an edgein [pq] by Lemma 4. In particular, if T is a breadth-first spanning tree of thegraph, rooted at v, T must include an edge in [pq]. But T has only n− 1 edges,and each equivalence class is represented by at least one edge in T , so there canbe at most n− 1 equivalence classes.

Our algorithm for partitioning the edges of a graph G into classes (that, ifG is a partial cube, will be the equivalence classes of ∼G) and simultaneouslylabeling the vertices of G with bitvectors (that, if G is a partial cube, will be acorrect partial cube labeling for G) performs the following steps. As part of thealgorithm, we set a limit L on the number of equivalence classes it can output;

Page 14: Recognizing Partial Cubes in Quadratic Time › pdf › 0705.1025.pdfRecognizing Partial Cubes in Quadratic Time David Eppstein1 1Computer Science Department, University of California,

282 Eppstein Recognizing Partial Cubes in Quadratic Time

for our initial call to the algorithm, we set L = n− 1, but it will be smaller inthe recursive calls the algorithm makes to itself.

• If G has one vertex and no edge, we report that it is a partial cube, labelits vertex with a bitvector of length zero, and return an empty set of edgeequivalence classes.

• We find the maximum degree d of a vertex in G and test whether d exceedsthe remaining limit on the number of allowed equivalence classes. If itdoes, we terminate the algorithm and report that G is not a partial cube.

• We apply the algorithm of Lemma 7 to find a set E of d edge classes of G.If this algorithm terminates and reports that G is not a partial cube, wedo likewise.

• We contract all edges that belong to classes in E , and remove any self-loopsor multiple adjacencies in the resulting contracted graph. As we do so,we maintain a correspondence of edges in G with the edges representingthem in the contracted graph G′, and between vertices in G and the cor-responding vertices in G′. If a set of edges in G corresponds to a multipleadjacency in G′, we represent them all by the same single edge in G′. Ifan edge in G corresponds to a self-loop in G′, and does not belong to oneof the classes in E , we terminate the algorithm and report that G is nota partial cube. Figure 5 shows the smaller contracted graph G′ resultingfrom this step of the algorithm.

• We apply the same algorithm recursively, to partition the edges and labelthe vertices of G′. In this recursive call we limit the algorithm to outputat most L−d equivalence classes. If this algorithm terminates and reportsthat G′ is not a partial cube, we terminate and report that G is also nota partial cube.

• We propagate the labels and partition of G′ back to the vertices and edgesof G, using the correspondence created when we contracted G to form G′.

• To form the list of equivalence classes of edges for G, we concatenate thelist of equivalence classes for G′ (with the edges replaced by the edges theycorrespond to in G) with the separate list of classes E .

• To form the vertex label for each vertex v of G, we concatenate the bitvec-tor for the vertex corresponding to v in G′ with the bitvector Dv foundby the algorithm of Lemma 7.

As an example, if we apply our algorithm to the graph of Figures 3 and 4(perhaps the graph contains an additional edge, not shown, that would cause thevertex p to have maximum degree), it would construct the four edge classes andfour-bit labels shown in Figure 4 in its outermost call. It would then contractthe labeled edges, resulting in a much smaller graph, a path of three edges shownin Figure 5: there are four unlabeled edges in Figure 4 but two of them form

Page 15: Recognizing Partial Cubes in Quadratic Time › pdf › 0705.1025.pdfRecognizing Partial Cubes in Quadratic Time David Eppstein1 1Computer Science Department, University of California,

JGAA, 15(2) 269–293 (2011) 283

a multiple adjacency when contracted. We pass this path to the second levelof recursion, which will label and contract two of the edges and leave unlabeledthe third since a path has no nontrivial edge relations. In the third level ofrecursion, the remaining edge is labeled and contracted, leaving a single vertexin the fourth level of recursion, which terminates immediately. Thus, for thisgraph (which is a partial cube), the algorithm eventually terminates with sevenedge classes: the four shown in Figure 4, one for the two unlabeled edges thatare part of a four-cycle in that figure, and one each for the two remaining edges.

Lemma 11 The algorithm above terminates in time O(n2), and either producesa partition of the edges into classes and a bitvector labeling of the vertices orterminates with the claim that G is not a partial cube. If G is a partial cube, thealgorithm produces a correct partition and a correct labeling of G. If G is not apartial cube, but the algorithm nevertheless returns a partition and a bitvectorlabeling, then each edge set in the partition forms a cut in the graph separatingthe vertices for which the bit corresponding to that edge set is 0 from the verticesfor which the bit is 1.

Proof: As is standard in graph algorithms, removing self-loops and multipleadjacencies from the contracted graph G′ may be performed in time O(m) byassigning index numbers to the vertices and then applying two rounds of bucketsorting to the list of edges, one for each endpoint of each edge. The othersteps of the algorithm, except for applying Lemma 7 and concatenating vertexlabels, take time O(m). By Lemma 7, the time to find E is O(dn), where dis the number of equivalence classes found. And, the time spent in the finalstep of the algorithm concatenating vertex labels is also O(dn). Thus, in eachrecursive call of the algorithm, the time taken at that level of the recursion isO(dn+m) = O(dn). Since we limit the algorithm to produce a total of at mostn− 1 classes, the total time summed over all recursive calls is at most O(n2).

If the input is a partial cube, we prove by induction on the number ofrecursive calls that the output is correct. As a base case, this is clearly truefor the single-vertex graph. Otherwise, each call to the algorithm of Lemma 7finds a valid set of classes [pq], which by Lemma 1 are equivalence classes of∼G, and a valid vertex labeling for the semicubes derived from those classes.The induction hypothesis tells us that the algorithm finds a correct labeling andpartitioning for the contracted graph G′, and by Lemma 9 it is also correct whentranslated to the corresponding objects of G. The algorithm simply combinesthese two components of a correct labeling and therefore all equivalence classesit outputs are correct. By the induction hypothesis again, every edge of G′ ispart of one of the output equivalence classes, from which it follows that theseclasses when translated to G include all edges not already part of a class in E ;therefore our output list of equivalence classes is not only correct but complete,and forms a partition of the edges of G.

If the input is not a partial cube, the desired edge cut property neverthelessfollows for the edge classes in E by Lemma 4, and can be shown to hold for alledge classes straightforwardly by induction on the number of recursive calls.

Page 16: Recognizing Partial Cubes in Quadratic Time › pdf › 0705.1025.pdfRecognizing Partial Cubes in Quadratic Time David Eppstein1 1Computer Science Department, University of California,

284 Eppstein Recognizing Partial Cubes in Quadratic Time

6 All pairs shortest paths

In order to verify that the given graph is a partial cube, we check that thelabeling constructed by Lemma 11 is a correct partial cube labeling of the graph.To do this, we need distance information about the graph, which (if it is acorrectly labeled partial cube) can be gathered by the all-pairs shortest pathsalgorithm for partial cubes from our previous paper [20]. However, as part ofour verification algorithm, we will need to apply this algorithm to graphs thatmay or may not be partial cubes. So, both for the purpose of providing a self-contained explanation and in order to examine what the algorithm does whengiven an input that may not be a partial cube, we explain it again in some detailhere.

It will be convenient to use some of the language of media theory [21, 23], aframework for describing systems of states and actions on those states (calledmedia) as finite state machines satisfying certain axioms. The states and ad-jacent pairs of states in a medium form the vertices and edges of a partialcube, and conversely any partial cube can be used to form a medium. We donot describe here the axioms of media theory, but only borrow sufficient of itsterminology to make sense of the all-pairs shortest path algorithm.

Thus, we define a token to be an ordered pair of complementary semicubes(Spq, Sqp). If G is a graph, with vertices labeled by bitvectors, we may specifya token as a pair (i, b) where i is the index of one of the coordinates of thebitvectors, Spq is the semicube of vertices with ith coordinate equal to b, andSqp is the semicube of vertices with ith coordinate unequal to b. A token actson a vertex v if v belongs to Spq and has a neighbor w in Sqp; in that case,the result of the action is w. Our all-pairs shortest path algorithm begins bybuilding a table indexed by (vertex,token) pairs, where each table cell lists theresult of the action of a token τ on a vertex v (or v itself if τ does not act on v).Note that, if we are given any labeled graph that may or may not be a correctlylabeled partial cube, we may still build such a table straightforwardly in timeO(n2); if as part of this construction we find that a vertex v has two or moreneighbors in Sqp we may immediately abort the algorithm as in this case theinput cannot be a correctly labeled partial cube.

Define an oriented tree rooted at r to be a subgraph of the input graph G,with an orientation on each edge, such that each vertex of G except for r hasa single outgoing edge vw, and such that w is formed by the action on v of atoken (Spq, Sqp) for which r is a member of Sqp.

Lemma 12 Suppose we are given a graph G, a labeling of the vertices of G bybitvectors, and a partition of the edges into classes, such that each class is theset of edges spanning the cut defined by one of the coordinates of the bitvectors.Then the graph distance between any two vertices v and w in G is greater thanor equal to the Hamming distance of the labels of v and w.

Proof: For each bit in which the labels of v and w differ, the path from v to wmust cross the corresponding cut in G at least once. No two cuts can share the

Page 17: Recognizing Partial Cubes in Quadratic Time › pdf › 0705.1025.pdfRecognizing Partial Cubes in Quadratic Time David Eppstein1 1Computer Science Department, University of California,

JGAA, 15(2) 269–293 (2011) 285

same path edge, as the cuts partition the edges. Therefore, any path from v tow must have at least as many edges as there are bit differences.

Lemma 13 Suppose we are given a graph G, a labeling of the vertices of G bybitvectors, and a partition of the edges into classes, such that each class is theset of edges spanning the cut defined by one of the coordinates of the bitvectors,and suppose that T is an oriented tree rooted at r. Then T is a shortest pathtree for paths to r in G, and each path from any vertex s to r in this tree haslength equal to the Hamming distance between the labels of s and r.

Proof: T has no directed cycles, for traversing a cycle would cross the samecut in G multiple times in alternating directions across the cut, while in T anydirected path can only cross a cut in the direction towards r. Thus, T is a tree.The length of a path in T from s to r at most equals the Hamming distancebetween the labels of s and r, because by the same reasoning as above the pathcan only cross once the cuts separating s and r (for which the correspondingbits differ) and cannot cross any cut for which the corresponding bits of thelabels of s and r agree. By Lemma 12 any path must have length at least equalto the Hamming distance, so the paths in T are shortest paths and have lengthequal to the Hamming distance.

Our all-pairs shortest path algorithm traverses an Euler tour of a spanningtree of the input graph, making at most 2n− 1 steps before it visits all verticesof the graph, where each step replaces the currently visited node in the traversalby a neighboring node. As it does so, it maintains the following data structures:

• The current node visited by the traversal, r.

• A doubly-linked ordered list L of the tokens (Spq, Sqp) for which r belongsto Sqp.

• A pointer pv from each vertex v 6= r to the first token in L that acts on v.

• A list Aτ for each token τ in L of the vertices pointing to τ .

Lemma 14 If the data structures described above are maintained correctly, wecan construct an oriented tree rooted at r.

Proof: We set the directed edge out of each v to be the result of the action oftoken pv on v.

To update the data structure when traversing from r to r′, we perform thefollowing steps:

• Append the token τ = (Srr′ , Sr′r) to the end of L, set pr = τ , and add rto Aτ .

• Let τ ′ be the token (Sr′r, Srr′); remove r′ from Aτ ′ .

Page 18: Recognizing Partial Cubes in Quadratic Time › pdf › 0705.1025.pdfRecognizing Partial Cubes in Quadratic Time David Eppstein1 1Computer Science Department, University of California,

286 Eppstein Recognizing Partial Cubes in Quadratic Time

• For each vertex v 6= r in Aτ ′ , search L sequentially forward from τ ′ forthe next token that acts on v. Replace pv with a pointer to that tokenand update the lists Ai appropriately.

• Remove (Sr′r, Srr′) from L.

We modify the algorithm in one small regard to handle the possibility thatthe input might not be a partial cube: if the search for the replacement for pvruns through all of list L without finding any token that acts on v, we abort thealgorithm and declare that the input is not a partial cube.

Lemma 15 If the input graph G is a correctly labeled partial cube, the algorithmdescribed above will correctly update the data structures at each step and finda shortest path tree rooted at each node. If the input graph is not a correctlylabeled partial cube, but is a bitvector-labeled graph together with a partition ofthe edges into classes such that each class is the set of edges spanning the cutdefined by one of the coordinates of the bitvectors, then the algorithm will abortand declare that the input is not a partial cube. In either case, the total runningtime is at most O(n2).

Proof: If the input is a partial cube, then, at any step of the algorithm, eachvertex v has a token in L that acts on it, namely the token corresponding tothe first edge in a shortest path from v to r. Thus, the sequential search for areplacement for pv, starting from a point in L that is known to be earlier thanall tokens acting on v, is guaranteed to find such a token. Thus, by Lemma 14we have an oriented tree rooted at r for each r, and by Lemma 13 this is ashortest path tree.

Conversely, if the algorithm terminates with an oriented tree rooted at rfor each r, this gives us by Lemma 13 a shortest path tree in which each pathlength equals the Hamming distance of labels; since all graph distances equalthe corresponding Hamming distances, the input is a partial cube. Thus, if theinput were not a correctly-labeled partial cube, but satisfied the other conditionsallowing us to apply Lemma 13, the algorithm must at some point abort.

L starts with at most n− 1 items on it, and has at most 2n− 1 items addedto it over the course of the algorithm. Thus, for each v, over the course of thealgorithm, the number of steps performed by searching for a new value for pvis at most 3n− 2. Thus, the total amount of time spent searching for updatedvalues of pv is O(n(3n − 2)) = O(n2). The other steps of the algorithm aredominated by this time bound.

7 Testing correctness of the labeling

We now put together the pieces of our partial cube recognition algorithm.

Lemma 16 If we are given a graph G, a labeling of the vertices of G by bitvec-tors, and a partition of the edges into classes, such that each class is the set ofedges spanning the cut defined by one of the coordinates of the bitvectors, then

Page 19: Recognizing Partial Cubes in Quadratic Time › pdf › 0705.1025.pdfRecognizing Partial Cubes in Quadratic Time David Eppstein1 1Computer Science Department, University of California,

JGAA, 15(2) 269–293 (2011) 287

we can determine whether the given labeling is a valid partial cube labeling intime O(n2).

Proof: We apply the algorithm of Lemma 15. By that Lemma, that algorithmeither successfully finds a collection of shortest path trees in G, which can onlyhappen when the input is a partial cube, or it aborts and declares that the inputis not a partial cube. We use the presence or absence of this declaration as thebasis for our determination of whether the given labeling is valid.

Theorem 1 Let G be an undirected graph with n vertices. Then we may checkwhether G is a partial cube, and if so construct a valid partial cube labeling forG, in time O(n2).

Proof: We use Lemma 11 to construct a partial cube labeling, and Lemma 16to test its validity.

8 Implementation

As a proof of concept, we implemented the algorithms described in this paper aspart of our open-source Python algorithm implementation library PADS, avail-able online at http://www.ics.uci.edu/~eppstein/PADS/, replacing a previ-ous implementation of an O(mn)-time algorithm.

8.1 Implementation details

The labeling phase of the new algorithm is in one Python module, PartialCube,and consists of approximately 66 lines of code within that module. The distance-checking phase of the algorithm is in a separate module, Medium, and consistsof approximately 48 lines of code within that module. Additionally, a moduleperforming breadth-first searches (written at the same time) and a previously-written module for testing bipartiteness of a graph (using depth-first search)were used as subroutines by the implementation.

The labeling algorithm described in this paper is recursive—it finds somelabels, contracts the labeled edges, recursively labels the remaining graph, andthen uncontracts it and in the process of uncontraction it extends the labelsfrom the contracted graph to the original graph. However, some versions ofPython are unsuited for algorithms involving deep recursion. Instead, we per-formed an iterative version of the algorithm that finds some edge equivalenceclasses, contracts the graph, and continues without recursing. Our implementa-tion represents the partition of the edges into equivalence classes by a union-finddata structure [37] (also previously implemented) in which each set element rep-resents an edge of the input graph and each of the disjoint sets represented bythe union-find data structure represents a set of edges that are all known tohave the same label. Whenever our algorithm finds the equivalence classes ofall of the edges incident to a single vertex using the algorithm of Section 4, ituses union operations to group those edges into a single set, and whenever it

Page 20: Recognizing Partial Cubes in Quadratic Time › pdf › 0705.1025.pdfRecognizing Partial Cubes in Quadratic Time David Eppstein1 1Computer Science Department, University of California,

288 Eppstein Recognizing Partial Cubes in Quadratic Time

contracts those labeled edges and the contraction generates multiple adjacenciesbetween a single pair of vertices, those multiple adjacencies are again groupedtogether by union operations and replaced in the contracted graph by a sin-gle representative edge. At the end of the algorithm, when the input graph hasbeen contracted down to a single vertex, the sets of edges sharing the same labeldo not need to be constructed by uncontracting the graph, as they are exactlythe sets represented by the union-find structure. The total time spent perform-ing union-find operations, O(n2α(n2,m)) = O(n2), is not asymptotically largerthan that for the rest of the algorithm.

Altogether, including comments, unit tests, and routines for other relatedtasks, but not including the other modules they refer to, both modules total631 lines.

8.2 Experimental tests

In order to test how well our theoretical bounds match the actual performanceof the implementation, we ran tests on a family of partial cubes generated fromsets of random permutations.

Let P = P1, P2, . . . , Pk be a set of permutations of the same t items, andfor each k-tuple of integers X = (x1, x2, . . . xk), 0 ≤ xi ≤ t, let S(X) be the setof items that appear in a position earlier than xi in at least one permutation Pi.Then the sets S(X) generated in this way form an antimatroid, and the graphthat has one vertex for each such set and one edge for each two sets that differ ina single element is an example of a partial cube. These graphs do not include allpossible partial cubes; we chose them as test cases for two reasons: first becausechoosing k permutations uniformly at random (with replacement) provides aconvenient probability distribution with which to perform random testing, andsecond because efficient algorithms and a proof of concept implementation wereavailable to generate these graphs from their defining permutations [17].

Our experimental data is presented in Table 1. Each row of the table shows,averaged over ten randomly chosen graphs, the number of vertices in the graph,the number of edges in the graph, the number of iterations performed in thefirst phase of the algorithm (in which the partial cube labeling is constructed),and the number of steps per vertex performed by the second phase of the algo-rithm (in which the labeling is tested). We also measured the average numberof breadth-first-search passes needed within the first phase of the algorithm,limiting each pass to use bitvector operations with at most 32 bits per word,but for the size parameters we chose this number was not significantly differentthan the total number of iterations. The total time of the algorithm may beestimated by adding the numbers in the phase I and phase II columns and thenmultiplying the sum by the number of vertices.

Because the worst case for our algorithm is a path graph, a special case ofa tree, we also performed experiments for randomly generated trees (Table 2).The trees in our experiments are rooted and ordered (that is, the ordering ofthe children of each node is significant for determining whether two trees arethe same). The number of trees with a given number of nodes is counted by the

Page 21: Recognizing Partial Cubes in Quadratic Time › pdf › 0705.1025.pdfRecognizing Partial Cubes in Quadratic Time David Eppstein1 1Computer Science Department, University of California,

JGAA, 15(2) 269–293 (2011) 289

k t |V (G)| |E(G)| phase I iterations phase II steps / vertex2 15 76.0 135.0 3.7 115.02 20 125.8 229.6 4.6 193.82 25 183.4 339.8 5.3 292.82 30 238.4 444.8 6.6 376.42 35 332.4 627.8 7.1 542.32 40 465.6 889.2 7.9 759.63 15 194.3 470.6 2.5 340.13 20 473.6 1232.4 3.0 876.53 25 773.4 2039.8 3.6 1445.53 30 1264.9 3401.5 4.0 2393.84 15 466.0 1410.7 2.1 879.14 20 1192.4 3843.0 2.0 2306.15 15 1029.5 3719.7 1.9 2008.7

Table 1: Experimental test results from our implementation, on antimatroidsgenerated from k random permutations of a t-item set.

Catalan numbers, and we generated trees uniformly at random, with probabilityinversely proportional to the Catalan numbers. Again, each row of the data isan average of ten trials. For these experiments, on sufficiently large trees, thephase I iteration counts diverged from the count of the number of passes overthe graph that would be required using bitvectors that are limited to 32 bits.However both counts were still close to each other and both were significantlysmaller than the number of vertices in the graph.

n phase I iterations phase I 32-bit passes phase II steps / vertex50 9.6 9.6 24.5100 14.0 14.0 49.5200 21.5 21.5 99.5400 27.9 28.3 199.5800 45.5 49.4 399.5

Table 2: Test results on random n-node trees.

Our experiments showed that, for both types of graphs that we tested, thenumber of phase I iterations appears to be significantly less than its worst casebound of Θ(n) (a bound achieved, for instance, by path graphs); therefore, thisphase of the algorithm is significantly faster than its worst case bound. Thenumber of steps per vertex in phase II of the algorithm was smaller for treesthan it was for antimatroids, but in both test sets this number appeared tobe growing linearly in the number of vertices. Therefore, for these graphs, theaverage performance of phase II was no better than its worst case.

Page 22: Recognizing Partial Cubes in Quadratic Time › pdf › 0705.1025.pdfRecognizing Partial Cubes in Quadratic Time David Eppstein1 1Computer Science Department, University of California,

290 Eppstein Recognizing Partial Cubes in Quadratic Time

9 Conclusions

We have shown that recognition of partial cubes may be performed in quadratictime, improving previous algorithms for the same problem. If an explicit partialcube labeling of the vertices is required as output, this is close to the O(n2)bit complexity of the output. Although not simple, our algorithms are imple-mentable.

A worst case input, requiring Ω(n2) bits of output and forcing the labelingphase of our algorithm to take Ω(n2) time, takes the form of a path graph.However, the existence of algorithms for recognizing median graphs [30] thatdo not output a labeling explicitly and are significantly faster than quadraticsuggests that it may similarly be possible to recognize partial cubes more quickly.Our experiments suggest that the labeling phase of our algorithm is often muchfaster than our worst case bounds; however, to speed up the overall algorithm,we must also speed up the verification phase of the algorithm, which in ourexperimental tests was no better on average than it is in the worst case.

Acknowledgments

This research was supported in part by the National Science Foundation undergrant 0830403, and by the Office of Naval Research under MURI grant N00014-08-1-1015. A preliminary version of this paper was presented in the 19th ACM-SIAM Symposium on Discrete Algorithms, 2008; some of the material from thepreliminary version was also incorporated into the book Media Theory: AppliedInterdisciplinary Mathematics [21].

Page 23: Recognizing Partial Cubes in Quadratic Time › pdf › 0705.1025.pdfRecognizing Partial Cubes in Quadratic Time David Eppstein1 1Computer Science Department, University of California,

JGAA, 15(2) 269–293 (2011) 291

References

[1] N. Alon, R. Yuster, and U. Zwick. Color-coding. J. ACM 42(4):844–856,1995, doi:10.1145/210332.210337.

[2] F. Aurenhammer and J. Hagauer. Recognizing binary Hamming graphsin O(n2 log n) time. Mathematical Systems Theory 28:387–395, 1995.

[3] S. P. Avann. Metric ternary distributive semi-lattices. Proc. Amer. Math.Soc. 12(3):407–414, 1961, doi:10.2307/2034206.

[4] H.-J. Bandelt, V. Chepoi, and D. Eppstein. Combinatorics and geometryof finite and infinite squaregraphs. SIAM J. Discrete Math.24(4):1399–1440, 2010, doi:10.1137/090760301, arXiv:0905.4537.

[5] H.-J. Bandelt, V. Chepoi, and D. Eppstein. Ramified rectilinear polygons:coordinatization by dendrons. ACM Computing Research Repository,2010, arXiv:1005.1721.

[6] H.-J. Bandelt, V. Macaulay, and M. B. Richards. Median networks:speedy construction and greedy reduction, one simulation, and two casestudies from human mtDNA. Molecular Phylogenetics and Evolution16(1):8–28, 2000, doi:10.1006/mpev.2000.0792.

[7] H.-J. Bandelt and M. van de Vel. Embedding topological median algebrasin product of dendrons. Proc. London Math. Soc. 58(3):439–453, 1989.

[8] M. A. Bender and M. Farach-Colton. The LCA problem revisited. Proc.4th Latin American Symp. on Theoretical Informatics, pp. 88–94.Springer-Verlag, Lecture Notes in Computer Science 1776, 2000,doi:10.1007/10719839 9.

[9] G. Birkhoff and S. A. Kiss. A ternary operation in distributive lattices.Bull. Amer. Math. Soc. 52(1):749–752, 1947,doi:10.1090/S0002-9904-1947-08864-9.

[10] B. Bresar, W. Imrich, and S. Klavzar. Fast recognition algorithms forclasses of partial cubes. Discrete Applied Mathematics 131(1):51–61, 2003,doi:10.1016/S0166-218X(02)00416-X.

[11] P. Buneman. The recovery of trees from measures of dissimilarity.Mathematics in the Archaeological and Historical Sciences, pp. 387–395.Edinburgh University Press, 1971.

[12] D. Z. Djokovic. Distance preserving subgraphs of hypercubes. J.Combinatorial Theory, Ser. B 14:263–267, 1973.

[13] J.-P. Doignon and J.-C. Falmagne. Knowledge Spaces. Springer-Verlag,1999.

Page 24: Recognizing Partial Cubes in Quadratic Time › pdf › 0705.1025.pdfRecognizing Partial Cubes in Quadratic Time David Eppstein1 1Computer Science Department, University of California,

292 Eppstein Recognizing Partial Cubes in Quadratic Time

[14] D. Eppstein. Algorithms for drawing media. Proc. 12th Int. Symp. GraphDrawing (GD 2004), pp. 173–183. Springer-Verlag, Lecture Notes inComputer Science 3383, 2004, arXiv:cs.DS/0406020.

[15] D. Eppstein. The lattice dimension of a graph. Eur. J. Combinatorics26(5):585–592, 2005, doi:10.1016/j.ejc.2004.05.001, arXiv:cs.DS/0402028.

[16] D. Eppstein. Isometric diamond subgraphs. Proc. 16th Int. Symp. GraphDrawing, pp. 384–389. Springer-Verlag, Lecture Notes in ComputerScience 5417, 2008, doi:10.1007/978-3-642-00219-9 37, arXiv:0807.2218.

[17] D. Eppstein. Learning sequences. ACM Computing Research Repository,2008, arXiv:0803.4030.

[18] D. Eppstein. Upright-quad drawing of st-planar learning spaces. J. GraphAlgorithms & Applications 12(1):51–72, 2008, arXiv:cs.CG/0607094,http://www.jgaa.info/accepted/2008/Eppstein2008.12.1.pdf.

[19] D. Eppstein. Happy endings for flip graphs. Journal of ComputationalGeometry 1(1):3–28, 2010, arXiv:cs.CG/0610092.

[20] D. Eppstein and J.-C. Falmagne. Algorithms for media. ACM ComputingResearch Repository, 2002, arXiv:cs.DS/0206033.

[21] D. Eppstein, J.-C. Falmagne, and S. Ovchinnikov. Media Theory: AppliedInterdisciplinary Mathematics. Springer-Verlag, 2008.

[22] D. Eppstein and K. A. Wortman. Optimal angular resolution forface-symmetric drawings. ACM Computing Research Repository, 2009,arXiv:0907.5474.

[23] J.-C. Falmagne and S. Ovchinnikov. Media theory. Discrete AppliedMathematics 121(1–3):103–118, 2002.

[24] T. Feder. Stable Networks and Product Graphs. Memoirs of the AmericanMathematical Society 555. 1995.

[25] C. Gavoille, D. Peleg, S. Perennes, and R. Raz. Distance labeling ingraphs. J. Algorithms 53(1):85–112, 2004,doi:10.1016/j.jalgor.2004.05.002.

[26] R. L. Graham and H. Pollak. On addressing problem for loop switching.Bell System Technical Journal 50:2495–2519, 1971.

[27] J. Hagauer, W. Imrich, and S. Klavzar. Recognizing median graphs insubquadratic time. Theoret. Comput. Sci. 215:123–136, 1999.

[28] D. Harel and R. E. Tarjan. Fast algorithms for finding nearest commonancestors. SIAM J. Comput. 13(2):338–355, 1984, doi:10.1137/0213024.

Page 25: Recognizing Partial Cubes in Quadratic Time › pdf › 0705.1025.pdfRecognizing Partial Cubes in Quadratic Time David Eppstein1 1Computer Science Department, University of California,

JGAA, 15(2) 269–293 (2011) 293

[29] W. Imrich and S. Klavzar. A simple O(mn) algorithm for recognizingHamming graphs. Bull. Inst. Combin. Appl. 9:45–56, 1993.

[30] W. Imrich, S. Klavzar, and H. M. Mulder. Median graphs andtriangle-free graphs. SIAM J. Discrete Math. 12:111–118, 1999.

[31] S. Klavzar, I. Gutman, and B. Mohar. Labeling of benzenoid systemswhich reflects the vertex-distance relations. Journal of ChemicalInformation and Computer Sciences 35(3):590–593, 1995.

[32] J. Matousek. The number of unique-sink orientations of the hypercube.Combinatorica 26(1):91–99, 2006, doi:10.1007/s00493-006-0007-0.

[33] L. Nebesk’y. Median graphs. Comment. Math. Univ. Carolinae12:317–325, 1971.

[34] S. Ovchinnikov. The lattice dimension of a tree. Electronic preprintarxiv:math.CO/0402246, 2004.

[35] S. Ovchinnikov. Hyperplane arrangements in preference modeling. J.Mathematical Psychology 49:481–488, 2005.

[36] O. E. Polansky and D. H. Rouvray. Graph-theoretical treatment ofaromatic hydrocarbons. II. The analysis of all-benzenoid systems.Informal Communications in Mathematical Chemistry, Match No. 2,pp. 111–115. Inst. fur Strahlenchemie, Max-Planck-Institut furKohlenforschung, Mulhein a.d. Ruhr, 1976.

[37] R. E. Tarjan. Efficiency of a good but not linear set union algorithm. J.ACM 22(2):215–225, 1975, doi:10.1145/321879.321884.

[38] P. Winkler. Isometric embeddings in products of complete graphs.Discrete Applied Mathematics 7:221–225, 1984.


Recommended