A poset-based approach to embedding graphs in hypercubes and lattices

Christine T. Cheng∗ Department of Computer Science University of Wisconsin–Milwaukee Email: [email protected]

July 7, 2010

Abstract A median graph G is a graph where, for any three vertices u, v and w, there is a unique node that lies on a shortest path from u to v, from u to w, and from v to w. While not obvious from the definition, median graphs are partial cubes; that is, they can be isometrically embedded in hypercubes and, consequently, in integer lattices. The isometric and dimensions of G, de- noted as dimI (G) and dimZ (G), are the smallest integers k and r so that G can be isometrically embedded in the k-dimensional hypercube and the r-dimensional lattice respectively. Motivated by recent results on the cover graphs of distributive lattices, we study these parameters through median semilattices, a class of ordered structures related to median graphs. We show that not only does this approach provide new combinatorial characterizations for dimI (G) and dimZ (G), they also have nice algorithmic consequences. Assume G has n vertices and m edges. We prove that dimI (G) can be computed in O(n + m) time and an isometric embedding of G on a hypercube with dimension dimI (G) can be constructed in O(n × dimI (G)) time. The algorithms are extremely simple and the running times are optimal. We also show that dimZ (G) can be computed and an isometric embedding 2.5 of G on a lattice with dimension dimZ (G) can be constructed in O(n × dimI (G) + dimI (G) ) time by using an existing algorithm of Eppstein’s that performs the same tasks for partial cubes. We are able to speed up his algorithm by using our framework to provide a new “interpretation” to the algorithm. In particular, we note that its main part is essentially a generalization of Fulkerson’s method for finding a smallest-sized chain decomposition of a poset.

∗Supported by NSF Award No. CCF-0830678.

1 1 Introduction

A median graph is a graph where, for any three vertices u, v and w, there is a unique node that lies on a shortest path from u to v, from u to w, and from v to w. For example, trees and hypercubes are median graphs. Median graphs have been studied extensively ([15, 2, 17]) and arise quite naturally in practice. It is known, for instance, that the solutions of a (solvable) 2-satisfiability instance with no equivalent variables as well as the stable matchings of a (solvable) stable roommates problem form median graphs [11, 5]. We say that a graph G is an isometric subgraph of another graph H (or G can be isometrically embedded in H) if there is a function α : V (G) → V (H) that is -preserving; that is, for any two vertices u and v, dG(u, v) = dH (α(u), α(v)). Graphs which can be isometrically embedded in a hypercube are called partial cubes. While not obvious from the definition, median graphs are in fact partial cubes, and it is this property that is of interest to us. When G is a , the isometric dimension of G, dimI (G), is the smallest dimension k so that G is an isometric subgraph of Qk, the k-dimensional hypercube. Since partial cubes can also be isometrically embedded in integer lattices, the lattice dimension of G, dimL(G), is defined as the smallest dimension r so that G is an isometric subgraph of Zr, the r-dimensional lattice. For example, when T is a with n vertices and l leaves, dimI (T ) = n−1 while dimZ (T ) = dl/2e [19]. We shall say that an isometric hypercube or lattice embedding of G is optimal if it is embedded in a hypercube or a lattice whose dimension is equal to dimI (G) or dimZ (G) respectively. Recently, together with a colleague I. Suzuki [6], we proved that when L is a , H(L) its , and G(L) its cover graph, dimI (G(L)) is equal to the diameter of G(L) while dimZ (G(L)) is equal to the maximum outdegree of H(L). Cover graphs of distributive lattices form a small subclass of median graphs. Yet, as we shall show, the techniques we used for them generalizes in a straightforward fashion to median graphs. Consequently, we obtain new characterizations for the isometric and lattice dimensions of a median graph. These results in turn lead to simpler if not faster algorithms that compute these dimensions and embed the graph in a hypercube and in a lattice with the said dimensions. A fundamental result in distributive lattices is Birkhoff’s representation theorem [4] which implies that every distributive lattice L can be encoded by its subposet of join-irreducible elements PL; in particular, every element of L can be represented by a subset of join-irreducible elements of the lattice. Our main insight is that every chain decomposition of PL of size r gives rise to an isometric lattice embedding of G(L) in Zr. In [1], Avann noted that median graphs are intimately related to a class of of posets called median semilattices, which are generalizations of distributive lattices. In particular, if we start with a median graph and one of its node u, transform the graph into a by directing all its edges away from u (i.e., if vw is an edge, direct the edge from v to w when u is closer to v than to w), then the directed graph is the Hasse diagram of a median semilattice. Results similar to Birkhoff’s theorem exist for median semilattices [3, 5]; we use the one found in [5]: for every ∗ median semilattice M, there is also a poset PM constructed from the join-irreducible elements ∗ of M that encodes M. What makes PM different from PL is that each join-irreducible element ∗ of M is represented by two elements in PM. To generalize the embedding result for distributive ∗ lattices, we shall use a special type of chain decomposition of PM, which we refer to as a twin chain decomposition, and show that such a decomposition gives rise to an isometric lattice embedding of the cover graph of M in Zr, where r is again the size of the twin chain decomposition. Current approaches to obtaining an optimal isometric hypercube or lattice embedding for me- dian graphs and partial cubes rely on a very different tool: the Djokovi´c-Winklerrelation Θ on the the edge set of a graph [9, 21], where two edges uv and u0v0 are related if and only if

2 d(u, u0) + d(v, v0) 6= d(u, v0) + d(u0, v). It is known that when G is a partial cube, Θ is an equiv- alence relation on E(G), and dimI (G) = |E(Θ)|, where E(Θ) contains all the equivalence classes induced by Θ. Once E(Θ) has been computed, it is easy to construct an optimal isometric hyper- cube embedding of G (that is, assign each of G a point of QdimI (G)) in O(n × dimI (G)) time (see step 4 of Algorithm 2.2. in [15] for example).1 For median graphs, the fastest algorithms for computing E(Θ) is due to Hagauer, et.al. [14] and Imrich and Klavzar [15]. The first one is a recur- sive algorithm while the second one involves the listing of all the squares (i.e., 4-cycles) of a median graph. Both algorithms take O(m log n) time. Thus, the current fastest algorithm for constructing an optimal isometric hypercube embedding for a median graph takes O(m log n + n × dimI (G)) time. Eppstein [10] was the first to show that the lattice dimension of a partial cube and an optimal isometric lattice embedding of a the partial cube can be computed in polynomial time. Given G and an optimal isometric hypercube embedding of G, the algorithm first constructs what Eppstein calls the semicube graph of G, Sc(G). It then finds a maximum matching Fmax of Sc(G) and transforms this matching into a set of paths. These paths are then used to construct an optimal isometric lattice embedding of G. Eppstein proved that dimZ (G) = dimI (G) − |Fmax|, which is also the 2 number of paths generated from Fmax. Eppstein’s algorithm runs in O(n × dimI (G) ) time. While faster algorithms for trees exist [16], the best one for median graphs is still Eppstein’s algorithm. We are ready to describe our main results. Let us now assume that G is a median graph with n vertices and m edges. Let M(G) be the median semilattice derived from G and one of its nodes u. Let kˆ denote the number of join-irreducible elements of M(G) andr ˆ the size of a smallest twin ∗ chain decomposition of PM(G). It is known that m ≤ n log2 n and log2 n ≤ dimI (G) ≤ n [15].

ˆ • First, we prove that dimI (G) = k. Additionally, we show that the join-irreducible elements of M(G) can be identified and, therefore, counted in O(n + m) time and that G can be ˆ isometrically embedded in Qkˆ in O(nk) time. The running times are not only optimal but the algorithms themselves are extremely simple – the first one consists of a single run of BFS (the breadth-first search algorithm) together with some post-processing while the second one requires an additional step of topological sorting.

In light of the fact that dimI (G) = |E(Θ)|, this result suggests that there is a one-to-one correspondence between the join-irreducible elements of M(G) and the Θ-equivalence classes of E(G). This is indeed the case. Each join-irreducible element ai of M(G) covers exactly one element, say bi. It is not difficult to show that mapping ai to the Θ-equivalence class that contains the edge aibi for i = 1,..., kˆ is a bijection. Of course, the advantage of the join- irreducible elements of M(G) is that they are much easier to identify than the equivalence classes induced by Θ. We also note that the correspondence between these two fundamental objects can be derived from Barthelemy and Constantin’s work [3] but they never used it to address embedding problems.

• Second, we show that dimZ (G) =r ˆ. Moreover, we prove that a minimum-sized chain- ∗ decomposition of PM(G) can be determined and, hence, counted, and G can be isometrically ˆ ˆ2.5 embedded in Zrˆ in O(nk+k ) time using Eppstein’s algorithm. We achieve this not by doing anything fancy but by using our framework to provide a new interpretation to the algorithm.

1We emphasize that we have distinguished the step of computing E(Θ) from the step of assigning each vertex of

G a point of QdimI (G). In particular, we view the latter step as part of embedding G in a hypercube. For some researchers, however, the former is sufficient (see [15, 14]). Hence, the phrase “embedding a graph in a hypercube” is sometimes used to refer to computing E(Θ) only. We note though that if the embedding is optimal, then it has to take Ω(n × dimI (G)) time since every vertex of G has to be assigned a bit vector of length dimI (G).)

3 In particular, we note that when the input to Eppstein’s algorithm is a median graph, its main part is essentially a generalization of Fulkerson’s method for finding the a smallest-sized chain decomposition of a poset [12].

Our results demonstrate once again that poset theory can be a very useful tool for graph embedding problems (see, for example, [20], etc.). They highlight the fact that representation the- orems of ordered structures like Birkhoff’s theorem are not only helpful for studying the structures themselves but also for understanding the cover graphs of these structures. The rest of the paper is organized as follows. In Section 2, we define terms and describe important results with regards to distributive lattices and their cover graphs. In Section 3, we briefly describes the representation theorem for median semilattices. In Section 4, we prove the new characterizations for the isometric and lattice dimensions of median graphs and present the algorithmic results in Section 5.

2 Preliminaries

Let P = (P, ≤) be a finite partially ordered set (poset). When x ≤ y, x is a predecessor of y while y is a successor of x. If x ≤ y and there is no other element z such that x < z < y, then y is said to cover x. A subset of P is a down-set (also called order ideal) if, for every element in the subset, all its predecessors are also in the subset. A sequence of elements x1, x2, . . . , xr form a chain in P if x1 < x2 < ··· < xr. The Hasse diagram of P, H(P), is the directed graph on P where (x, y) is an edge if and only if x is covered by y. The cover graph of P, G(P), is the undirected version of its Hasse diagram. An element z is an upper bound of x and y whenever x ≤ z and y ≤ z. It is the least upper bound or the join of x and y, denoted as x ∨ y, if z ≤ z0 for every upper bound z0 of x and y. Similarly, w is a lower bound of x and y whenever w ≤ x and w ≤ y. It is the greatest lower bound or the meet of x and y, denoted as x ∧ y, if w0 ≤ w for every lower bound w0 of x and y. The upper and lower bounds, the least upper bound and the greatest lower bound of sets are defined similarly. The poset P is a join or a meet semilattice if, for every pair of elements x and y, x ∨ y or x ∧ y exists respectively. As a result, every join semilattice has a top element 1ˆ = ∨P while every meet semilattice has a bottom element 0ˆ = ∧P . A poset is a lattice if it is both a join and a meet semilattice. It is a distributive lattice if, additionally, the join and meet operations distribute over each other. As an example, let D(P) consist of all the down-sets of P, and let D(P) be the poset that results from ordering the down-sets according to the subset relation; i.e., D(P) = (D(P), ⊆). It is not difficult to show that when P1,P2 ∈ D(P), the sets P1 ∪ P2 and P1 ∩ P2 are also in D(P). Furthermore, P1 ∨ P2 = P1 ∪ P2 while P1 ∧ P2 = P1 ∩ P2. And since set union and intersection distribute over each other, it follows that D(P) is a distributive lattice. Interestingly, as we shall see below, it turns out the every distributive lattice can be viewed in this manner. In a lattice L, an element a is join-irreducible if a 6= 0,ˆ and, whenever x ∨ y = a, either x = a or y = a. Equivalently, a is join-irreducible if a covers exactly one element of the lattice; that is, the indegree of a in H(L) is 1. Let J(L) consist of all the join-irreducible elements of L, and let PL be the subposet induced by the elements of J(L). From the previous paragraph, we know that D(PL) is a distributive lattice. For each element x of L, define J(x) = {a ∈ J(L): a ≤ x}. Notice that J(x) is a down-set of PL and is therefore an element of D(PL). Here is Birkhoff’s representation theorem for distributive lattices.

Theorem 1 [4] Let L = (L, ≤) be a distributive lattice. Let f : L → D(PL) such that f(x) = J(x). Then f is an order-isomorphism from L to D(PL).

4 2.1 Embedding the cover graphs of distributive lattices When L is a distributive lattice, its cover graph G(L) is a median graph. Together with a col- league [6], we used Birkhoff’s theorem to isometrically embed G(L) in Qk, where k = |J(L)| ; that is, each element x of L is mapped to the characteristic vector of J(x), which is the |J(L)|-bit vector whose ith bit is set to 1 if the ith join-irreducible element is in J(x) and is 0 otherwise. Since the diameter of G(L) is |J(L)|, the next theorem is true.

Theorem 2 [6] When L is a distributive lattice, the isometric dimension of G(L), the number of join-irreducible elements of L, and the diameter of G(L) are all equal.

Given a poset P, a chain decomposition of P is a set of chains C = {C1,C2,...,Cr} so that each Ci is a chain of P, and each element of P is part of exactly one chain. We then showed that every chain decomposition of PL of size r generates an isometric embedding of G(L) into Zr. Using Dilworth’s theorem [8], we proved the next theorem.

Theorem 3 [6] When L is a distributive lattice, the lattice dimension of G(L), the size of the smallest chain decomposition of PL, and the maximum outdegree of H(L) are all equal.

In [6], we did not consider the algorithmic aspects of our results. We note though that there is a well-known procedure for finding a smallest chain decomposition of a poset P due to Fulkerson [12]. He used it to argue that Dilworth’s theorem can be derived from K¨onig’stheorem for bipartite graphs. The procedure involves constructing a Y (P) based on P, finding a maximum matching of Y (P), and then transforming the maximum matching into a minimum-sized chain decomposition of P. In the graph Y (P), each element pi of P is represented by two vertices pi,0 and pi,1. Two nodes pi,1 and pj,0 form an edge if and only if pi < pj. Let Fmax be a maximum matching of Y (P). Add edges {pi,1, pi,0}, i = 1, . . . , n, to Fmax. The result is a set of n − |Fmax| disjoint paths, where each path is of the form pi1,0, pi1,1, pi2,0, pi2,1, . . . , pis,0, pis,1. A chain decomposition of P of the same size is obtained by transforming each path to a chain, where the chain corresponding to the path we just described is pi1 , pi2 , . . . , pis . This procedure is also used to find a minimum path covering for directed acyclic graphs (see Exercise 26.2 in [7] for example).

3 Median semilattices

A median semilattice M = (M, ≤) is a meet semilattice such that (i) for any element x, the subposet induced by the down-set {y ∈ M : y ≤ x} is a distributive lattice, and (ii) any three elements have an upper bound whenever any two of them have an upper bound. Our interest on median semilattices has to do with its connection to median graphs. Let G be a connected graph. For each vertex u of G, the canonical order ≤u on V (G) is defined as follows: for any two vertices v, w of G, v ≤u w whenever d(u, v) + d(v, w) = d(u, w).

∗ Theorem 4 [1] Suppose G is a median graph and u is one of its nodes. The poset MG,u∗ = (V (G), ≤u∗ ) is a median semilattice. Its cover graph is G; i.e., G(MG,u∗ ) = G.

For example, a tree T is a median graph. Root T at one of its nodes u∗, and then direct all edges of T away from u∗. The resulting directed graph is the Hasse diagram of the median semilattice MT,u∗ . It is in fact an interesting median semilattice – whenever two elements are incomparable, they have no upper bound. On the other hand, a distributive lattice is a median semilattice too. In this case, however, every pair of incomparable elements have an upper bound.

5 u* 00000 a5 a4

10000 a a a 1 2 3 a1 a2 a3 01000 00100

a4 a5 a1 a2 a3 10010 10001

a4 a5

Figure 1: The leftmost figure is the median semilattice M = MT,u∗ obtained from a tree T using ∗ the ordering ≤u∗ . Notice that except for u , all other nodes of T are join-irreducible elements of ∗ ∗ M. The figure in the middle is the poset PM. The figure on the right is B(PM), the poset formed ∗ from the complete down-sets of PM. Each set is represented by a 5-bit vector where the ith bit is 0 ifa ¯i is in the set and 1 if ai is in the set. For example, 00000 corresponds to the complete down-set ¯ ∗ J(M), etc. It is obvious that M is order-isomorphic to B(PM). Our discussion below also shows that the 5-bit vector labeling is an isometric embedding of T in Q5.

Since median semilattices are generalizations of distributive lattices, a natural question to ask is whether Birkhoff’s theorem has a counterpart for median semilattices. Barth´elemy and Con- stantin [3] were the first ones to show that the elements of a median semilattice can indeed be represented by its join-irreducible elements.2 Here, we consider another representation theorem proven in [5] that is also equivalent to that of Barth´elemy and Constantin’s; we do so mainly because it is more suitable for the goals of our paper. Like Birkhoff’s theorem, we start with a poset that is based J(M). For each a ∈ J(M), create another elementa ¯. We shall say that a anda ¯ are duals of each other, and a¯ = a. Let J¯(M) = {a¯ : a ∈ J(M)}. Define the relation ≤0 on J(M)∪J¯(M) as follows: for any pair of elements a, b ∈ J(M), (i) if a ≤ b then a ≤0 b and ¯b ≤0 a¯, and (ii) if a and b do not have an upper bound in 0 ¯ 0 ∗ ¯ 0 M, thena ¯ ≤ b and b ≤ a. We shall denote the resulting poset as PM = (J(M) ∪ J(M), ≤ ). ∗ Once again, we are interested in the down-sets of PM. However, we want this down-set to be complete – that is, every dual pair {a, a¯} of the poset must have exactly one element in the set. ∗ ∗ ∗ + Let B(PM) denote the complete down-sets of PM. For every set R ∈ B(PM), let R = R ∩ J(M) − ¯ ∗ and R = R ∩ J(M). Let B(PM) be the poset that results by making R1 precede R2 whenever + + R1 ⊆ R2 . For each element x of M, define J¯(x) = {a¯ : a ∈ J(M) and a 6∈ J(x)} and K(x) = J(x) ∪ J¯(x). ∗ Clearly, K(x) is a complete subset of PM. With a little bit of work, one can show that it is also a ∗ down-set of B(PM). We are now ready for the representation theorem for median semilattices.

∗ Theorem 5 [5] Let M = (M, ≤) be a median semilattice. Let g : M → B(PM) such that g(x) = ∗ K(x). Then g is an order-isomorphism from M to B(PM).

See Figure 1 for an example that demonstrates Theorem 5. Notice that the mappings f and g in Theorems 1 and 5 are not that different since K(x) and J(x) can be derived from each other. ∗ It is, however, important to use the complete down-sets of PM as the co-domain of the function g because not all down-sets of PM may have a pre-image since M is a median semilattice, and not necessarily a distributive lattice.

2Note that join-irreducible elements have traditionally been defined for lattices only (perhaps because the join of two elements of an arbitrary poset does not always exist). Nonetheless, the definition still holds when the poset is a meet semilattice.

6 For the latter sections, we shall represent each element x of M as K(x). The next lemma describes the distance between x and y in the cover graph of M in terms of K(x) and K(y).

Lemma 1 [5] Let M = (M, ≤) be a median semilattice. For any two elements x and y, the distance between x and y in G(M) is |K(x) − K(y)| = |K(y) − K(x)|.

4 Embedding median graphs

Let G[l1, l2, . . . , lr] denote the Cartesian product of r paths where the ith path has length li. Hence, a vertex x is represented as (x1, x2, . . . , xr) where 0 ≤ xi ≤ li for i = 1, . . . , r, and two vertices x and y are adjacent if and only if, for some i, |xi − yi| = 1 and xj = yj for j 6= i. Notice that the hypercube Qk is simply G[1, 1,..., 1] whose number of factors is k. Furthermore, since all graphs we are considering are finite, whenever a graph is isometrically embedded in Zr, there exists non-negative integers l1, l2, . . . , lr so that the graph is effectively embedded in G[l1, l2, . . . , lr]. An isometric embedding α of G in G[l1, l2, . . . , lr] is said to be irredundant or full dimensional if for i = 1, . . . , r and for j = 0, . . . , li, there is a node v of G such that the ith coordinate of α(v) is j. One alternate definition for the isometric dimension of a partial cube is that it is the unique integer k for which there is an irredundant isometric embedding of the partial cube in Qk [13].

4.1 The isometric dimension of a median graph ∗ Suppose G is a median graph, and u is one of its nodes. Recall that MG,u∗ = (V (G), ≤u∗ ) is a median semilattice, and the complete down-sets of the poset P∗ represent the vertices of G. MG,u∗ ∗ ∗ For ease of notation, we shall simply refer to M ∗ as M and P = P . G,u MG,u∗ M ∗ A twin chain decomposition of PM is a set C = {C1,C−1,C2,C−2,...,Cr,C−r} such that each C±i is a chain of the poset, each element of the poset is in exactly one of the chains, and Ci = y1 < y2 < . . . < yk if and only if C−i =y ¯k < . . . < y¯2 < y¯1, wherey ¯j denotes the dual of yj. (Note that some of the yi’s can belong to J¯(M); equivalently, some of they ¯i’s can belong to J(M).) We shall refer to Ci and C−i as twin chains, and define the size of C as r because the set can be fully specified by just r chains. As an example, suppose that J(M) = {a1, . . . , ak}. The set of trivial + + ∗ chains {{a1 }, {a¯1},..., {ak }, {a¯k}} is a twin chain decomposition of PM whose size is k.

∗ Lemma 2 Let C = {C1,C−1,C2,C−2,...,Cr,C−r} be a twin chain decomposition of PM. Let li = |Ci|, the number of elements in the chain Ci. Define β : V (G) → V (G[l1, l2, . . . , lr]) such that β(v) = (β1(v), β2(v), . . . , βr(v)) where βi(v) = |K(v) ∩ Ci|, i = 1, . . . , r. The mapping β is an isometric embedding of G into G[l1, l2, . . . , lr].

Proof First, we note that β(v) is a node of G[l1, l2, . . . , lr]. Furthermore, when βi(v) = j, this means that K(v) contains the first j elements of Ci and the first li − j elements of C−i because ∗ ∗ K(v) is a complete down-set of PM, and Ci and C−i are twin chains of PM. Hence, the elements of K(v) can be fully recovered from β(v), implying that β could not have mapped the two different nodes of G to the same nodes of G[l1, l2, . . . , lr]. ∗ Next, let u be another node of G. Since the chains in C partition the elements of PM, it is enough for us to compare K(v)∩Ci with K(u)∩Ci and K(v)∩C−i with K(u)∩C−i, for i = 1, . . . , r, 0 to compute |K(v) − K(u)|, the distance between u and v in G. Let βi(u) = j and assume for now 0 that j ≤ j. Our discussion in the previous paragraph implies that K(u) ∩ Ci ⊆ K(v) ∩ Ci but K(v)∩C−i ⊆ K(u)∩C−i. Hence, what gets counted in |K(v)−K(u)| is |K(v)∩C−i|−|K(u)∩C−i| which is equal to j − j0. On the other hand, when j < j0, what gets counted in |K(v) − K(u)| is

7 0 Pr |K(v) ∩ C−i| − |K(u) ∩ C−i| which is equal to j − j. It follows that |K(v) − K(u)| = i=1 |βi(v) − βi(u)|, which is the distance between v and u in G[l1, l2, . . . , lr]. Thus, β is an isometric embedding of G into G[l1, l2, . . . , lr]. 2

+ + ∗ So consider C = {{a1 }, {a¯1},..., {ak }, {a¯k}}, as the twin chain decomposition of PM. Let βC denote the isometric embedding of G into Qk constructed from C in Lemma 2. Notice that βC(v) ∗ is in fact the characteristic vector of J(v) for each node v of G. Thus, βC(u ) = (0, 0,..., 0) while the ith coordinate of βC(ai) is 1 for i = 1, . . . , k. That is, βC is also an irredundant embedding of G into Qk. We have just shown that the next theorem is true.

Theorem 6 Let G be a median graph and let u∗ be one of its nodes. Then the isometric dimension of G is equal to the number of join-irreducible elements of M = MG,u∗ .

4.2 The lattice dimension of a median graph ∗ Let Cmin be a minimum-sized twin chain decomposition of PM. An immediate consequence of Lemma 2 is that dimZ (G) ≤ |Cmin|. Our goal is to show that dimZ (G) ≥ |Cmin|. We can do this directly by proving that every isometric lattice embedding of G gives rise to a twin chain decomposition of PM. We can then use a procedure similar to Fulkerson’s [12] to find a minimum- ∗ sized chain decomposition of PM. Instead of doing them from scratch, we shall make use of Eppstein’s work [10]. To determine dimZ (G), Eppstein first constructed an intermediate structure called the semicube graph of G, Sc(G), which is defined as follows. Let α be an irredundant isometric embedding of G in Qk. For each i ∈ {1, . . . , k} and each l ∈ {0, 1}, the semicube Si,l consists of all nodes v of G such that αi(v) = l. In Sc(G), there are 2k vertices si,l, where si,l represents the semicube Si,l. Two vertices si,l and si0,l0 is an edge in Sc(G) if and only if the union of their corresponding semicubes is V (G) and their intersection is non-empty. Interestingly, when G is a median graph, we can describe its semicube graph in terms of the join-irreducible elements of M. The proof of the next lemma is in the appendix.

Lemma 3 Suppose the join-irreducible elements of M are a1, a2, . . . , ak. Let Y (M) be the graph on the set {si,0, si,1, i = 1, . . . , k} where {si,1, sj,0} is an edge if and only if ai ≤u∗ aj, and {si,0, sj,0} is an edge if and only if ai and aj have no upper bounds in M. Then Y (M) = Sc(G).

Notice the similarity between Y (M) and the graph Y (P) used in Fulkerson’s procedure. In particular, if M is a distributive lattice, Y (M) is the graph used in the procedure to find a minimum-sized chain decomposition of PM, the subposet of the join-irreducible elements of M. Eppstein then showed that every isometric lattice embedding of G in Zr gives rise to a matching of Sc(G) whose size is k − r. Conversely, every matching F of Sc(G) corresponds to an isometric lattice embedding of G in a lattice whose dimension is k − |F |. Consequently, Eppstein was able to prove that dimZ (G) = k − |Fmax|, where Fmax is a maximum matching of Sc(G). To obtain an isometric embedding βF of G in Zk−|F | from the matching F of Sc(G), Eppstein’s algorithm proceeded in a manner very similar to that of Fulkerson’s. The edges {si,1, si,0}, i = 1, . . . , k are added to F and the result is a set of k − |F | disjoint paths which are then used to derive βF (v) for each node v of G.

Theorem 7 [10] Let G be a partial cube with n vertices and dimI (G) = k. Let Fmax be a maximum matching of Sc(G). Then dimZ (G) = k − |Fmax|. Moreover, dimZ (G) and an optimal isometric lattice embedding of G can be computed in O(n2 + nk2) time.

8 We shall now show that the set of paths formed by F = F ∪{{si,1, si,0}, i = 1, . . . , k} correspond ∗ to a twin chain decomposition of PM of the same size. Let us call an edge in F horizontal, diagonal, and vertical if it is of the form {si,1, si,0}, {si,1, sj,0}, and {si,0, sj,0} respectively. Notice that every path in F starts and ends with a horizontal edge and alternates between a horizontal edge and a diagonal or a vertical edge. It is also not difficult to verify that such a path contains at most one vertical edge. So let us prove that each type of path correspond to twin chains.

Case 1: The path has no vertical edge. Then it is of the form si1,0, si1,1, si2,0, si2,1, . . . , sis,0, sis,1. ∗ ∗ ∗ By Lemma 3, this means that ai1 ≤u ai2 ≤u · · · ≤u ais so ai1 , ai2 , . . . , ais anda ¯is ,..., a¯i2 , a¯i1 are ∗ chains in PM.

Case 2: The path has one vertical edge. Then it has to be of the form si1,1, si1,0, . . . , sij ,1, sij ,0, ∗ ∗ ∗ sij+1,0, sij+1,1, . . . , sis,0, sis,1. Again, by Lemma 3, this implies that aij ≤u aij−1 ≤u · · · ≤u ai1 , ∗ ∗ ∗ aij and aij+1 do not have an upper bound in M, and aij+1 ≤u aij+2 ≤u · · · ≤u ais . That is, ∗ a¯i1 , a¯i2 ,..., a¯ij , aij+1 , aij+2 , . . . , ais anda ¯is ,..., a¯ij+2 , a¯ij+1 , aij , . . . , ai2 , ai1 are chains in PM.

Now, for each ai, the horizontal edge {si,1, si,0} is always part of some path in F. Furthermore, the paths in the F are disjoint. Consequently, the set of paths in F correspond to a twin chain ∗ decomposition of PM of the same size. Theorem 8 Let G be a median graph and u∗ be one of its nodes. The lattice dimension of G is equal to the size of a smallest twin chain decomposition of P∗ = P∗ . M MG,u∗ ∗ Proof Let r be the size of a smallest twin chain decomposition of PM. When F is a matching of ∗ Sc(G), we showed that the set of paths in F generated a twin chain decomposition of PM whose size is k − |F |, where k = dimI (G). Since this is true when F is a maximum matching of Sc(G), it follows from Theorem 7 that r ≤ dimZ (G). But according to Lemma 2, dimZ (G) ≤ r. Hence, dimZ (G) = r. 2

In Figure 1, C1 =a ¯5, a¯1, a2, C−1 =a ¯2, a1, a5, C2 =a ¯3, a4 and C−2 =a ¯4, a3 form a smallest-sized ∗ twin chain decomposition of PM. Thus, dimZ (T ) = 2.

5 The algorithms

Let us now translate the results and proof techniques from the previous section into algorithms. In our analysis, we make use of the fact that a median graph G is a bipartite graph. Moreover, if G has n vertices and m edges, then m ≤ n log2 n and log2 n ≤ dimI (G) ≤ n [15]. The basis of the next algorithm is Theorem 6 and the proof we used to argue its correctness. EmbedHypercube(G) ∗ 1. Pick some node u and transform G into the Hasse diagram of MG,u∗ = (V (G), ≤u∗ ) by directing all edges of G away from u∗. Keep track of each node’s in-neighbors and out- neighbors. Denote the resulting graph as Gu∗ .

2. Identify the join-irreducible elements of MG,u∗ , which are the nodes with indegree 1 in Gu∗ . Name them as a1, a2, . . . , ak. ∗ 3. Topologically sort Gu∗ and let its vertices be ordered as v1, v2, . . . , vn. Clearly, v1 = u .

4. Set β(v1) = (0, 0,..., 0), the k-tuple of 0’s. For i = 2 to n do the following: if vi has only one in-neighbor u, then vi is a join-irreducible element of MG,u∗ , say aj. Set β(vi) = β(u) and set βj(vi) = 1. Otherwise, vi must have another in-neighbor w. Set βj(vi) = max{βj(u), βj(w)} for j = 1, . . . , k.

9 Theorem 9 Let G be a median graph on n vertices and m edges. EmbedHypercube computes dimI (G) = k in O(n+m) time and an irredundant isometric embedding of G in Qk in O(nk) time. Proof The first two steps of the algorithm simply used the definition of the objects involved to identify all the join-irreducible elements of M = MG,u∗ . By Theorem 6, this is enough to determine dimI (G). The first step can be implemented using BFS, while the second step requires a simple scan of each node’s indegree list. Hence, the two steps together take O(n + m) time. To prove that β is an irredundant isometric embedding of G in Qk, let us show that β(v) is the ∗ characteristic vector of J(v) for each node v of G. For v1 = u , this is clearly true. Suppose the claim is true for v1, v2, . . . , vi−1. Consider vi. Note that all the in-neighbors of vi occurred earlier in the topological ordering so that their β-values correctly record their predecessors. Suppose that aj ≤u∗ vi but βj(vi) has been set to 0 and not 1 by the algorithm. The only way this could have happened is if aj is not a predecessor of the two in-neighbors u and w of v chosen by the algorithm. Hence, aj belongs to both K(vi)−K(u) and K(vi)−K(w). But |K(vi)−K(u)| = |K(vi)−K(w)| = 1, ∗ and all the K(∗)-sets are complete down-sets of PM; it follows that K(u) = K(w), a contradiction to Theorem 5. Hence, the algorithm must set βj(vi) to 1. By induction, the claim is true. Step 3 takes O(n + m) time while step 4 takes O(nk) time. Since m ≤ n log n and k ≥ log n, the total time it takes to compute β is O(nk). 2 Let us now describe a procedure for computing the semicube graph Sc(G) efficiently using Lemma 3. Let β be the isometric embedding computed by EmbedHypercube. Notice that ai ≤u∗ aj if and only if βi(aj) = 1. By examining the β-values of all the join-irreducible elements of MG,u∗ , all the edges of the form {si,1, sj,0} in Sc(G) can be determined. We shall find the remaining edges in Sc(G) by identifying all pairs ai and aj that have an upper bound in MG,u∗ . Semicube(G)

1. Run EmbedHypercube(G). Initialize Sc(G) to the graph just consisting of nodes si,0, si,1, i = 1, . . . , k. Create a k × k matrix A and set all its entries to 0.

2. For i = 1, . . . , k and for each aj, if βi(aj) = 1 then add edge {si,1, sj,0} to Sc(G). ∗ 3. Let T be the obtained by running BFS on Gu∗ starting at u . For each edge (u, v) in T do the following: find the unique j so that βj(u) = 0 but βj(v) = 1. For each i 6= j such that αi(v) = 1, set Aij = Aji = 1.

4. For each pair {i, j} such that Aij = 0, add {si,0, sj,0} to Sc(G). ∗ Proposition 1 For each node y of G and for each ai ∈ J(y), the path from u to y in T has one edge (u, v) such that J(v) − J(u) = {ai}.

Theorem 10 Let G be a median graph with n vertices and dimI (G) = k. Semicube computes Sc(G) in O(nk) time.

Proof Based on our earlier discussion, it is sufficient to show that step 3 sets Aij and Aji to 1 if and only if ai and aj have an upper bound in MG,u∗ . First, if Aij and Aji are set to 1 when some edge (u, v) is considered, then it is because ai ∈ J(v) and aj ∈ J(v); that is v is an upper bound of ai and aj. On the other hand, suppose ai and aj have an upper bound, say y. From Proposition 1, ∗ the path from u to y in T has one edge (u, v) such that J(v) − J(u) = {ai} and another edge 0 0 0 0 0 (u , v ) such that J(v ) − J(u ) = {aj}. Without loss of generality, assume that v ≤u∗ u so that 0 0 0 ai ∈ J(u ). Then, when edge (u , v ) is considered, Aij and Aji are set to 1. As for the running time of Semicube, the first step takes O(nk + k2) time, the second step O(k2) time, the third step O(n + m + nk) time, and the fourth step O(k2) time. Hence, the total time to implement the algorithm is O(nk) time. 2

10 EmbedLattice(G)

1. Run EmbedHypercube(G) and Semicube(G).

2. Compute a maximum matching Fmax of Sc(G) and set F = Fmax ∪ {{si,1, si,0}, i = 1, . . . , k}.

3. Find the paths formed in F and name them as R1,R2,...,Rr.

4. For each Rj, traverse it from one end to another. For each edge {si,1, si,0} encountered, if the edge was traversed from si,1 to si,0, set chain[ai] = j; otherwise set chain[ai] = −j. 5. For each node v of G, do the following: initially set γ(v) = (0, 0,..., 0), the r-vector of 0’s. For i = 1 to k, if (βi(v) = 1 and chain[ai] > 0) or (βi(v) = 0 and chain[ai] < 0) , set j = chain[ai] and increase γj(v) by 1. 6. Return (r, γ).

Theorem 11 Let G be a median graph with n vertices and dimI (G) = k. EmbedLattice com- 2.5 putes dimZ (G) = r and an isometric embedding of G in Zr in O(nk + k ) time.

Proof EmbedLattice follows Eppstein’s algorithm except for steps 4 and 5. In step 4, we derive the twin chain decomposition from the paths in F by setting chain[ai] = ±j if ai belongs to C±j. We note that there is no need to store chain[a ¯i] since this is just −chain[ai]. In step 5, we use Lemma 2 to derive an isometric embedding of G in Zr. The correctness of the algorithm follows. Step 1 takes O(nk) time. If we use Micali and Vazirani’s algorithm for finding maximum matchings [18] in Sc(G) which has O(k) nodes and O(k2) edges, step 2 takes O(k2.5) time. Since F has O(k) nodes and edges, steps 3 and 4 take O(k) time. Finally, step 5 takes O(nk) time. Thus, EmbedLattice can be implemented in O(nk + k2.5) time. 2

6 Final Remarks

It is interesting that EmbedHypercube is strikingly similar to the “labeling” algorithm for Ham- ming graphs (see Lemma 3.30 in [15]), which are Cartesian products of complete graphs. (Note that hypercubes are Hamming graphs and are, in fact, the only median graphs that are also Hamming graphs.) Suppose H is isomorphic to Kn1 × Kn2 × ... × Knk but n1, n2, . . . , nk are not known. How might H be isometrically embedded in this Hamming graph? The algorithm begins by choosing an arbitrary vertex v0 and computing the connected components formed by the neighbors of v0. Since H is a Hamming graph, the connected components must be complete graphs whose sizes are ni − 1, i = 1, . . . , k. An isometric embedding α for H in Kn1 × Kn2 × ... × Knk is now formed as follows. First, set α(v0) = (0,..., 0), the k-vector of 0’s. Then for the neighbors of v0 in the ith component, i = 1, . . . , k, set the ith coordinates of their α-labels from 1 to ni − 1 and all other coordinates to 0. Finally, for all other nodes v of H, derive α(v) from the α-labels of two in-neighbors of v in a manner similar to step 4 of EmbedHypercube. What makes these two algorithms similar is that there are “representative” vertices in each graph class which determine the desired isometric embeddings. For the Hamming graph H, they are the neighbors of v0; for median graph G, they are the -one vertices of Gu∗ . In both cases, the vertices are easy to identify. It is an intriguing question whether a similar kind of algorithm works for other graph classes and isometric embeddings. We also note that there is a recognition algorithm for Hamming graphs based on its “represen- tative” vertices (see Algorithm 3.2 in [15]). We strongly suspect that it can be modified for median graphs but its running time will not likely beat the algorithm of Hagauer, et.al. [14].

11 References

[1] S. Avann. Metric ternary distributive semi-lattices. Proceedings of the American Mathematical Society, 12:407–414, 1961. [2] H. Bandelt and V. Chepoi. Metric and geometry: a survey. Contemporary Mathematics, pages 49–86, 2008. [3] J. Barth´elemy and J. Constantin. Median graphs, parallelism and posets. Discrete Mathemat- ics, 111:49–63, 1993. [4] G. Birkhoff. Rings of sets. Duke Mathematical Journal, 3:443–454, 1937. [5] C. Cheng and A. Lin. Stable roommates matchings, mirror posets, median graphs and the local/global median phenomenon in stable matchings. Submitted to SIAM Journal of Discrete Mathematics, 2009. [6] C. Cheng and I. Suzuki. Weak sense of direction labelings and graph embeddings. Submitted to Discrete Applied Mathematics, 2010. [7] T. Cormen, C. Leiserson, R. Rivest, and C. Stein. Introduction to Algorithms. The MIT Press, 2nd edition, 2001. [8] R. Dilworth. A decomposition theorem for partially ordered sets. Annals of Mathematics, 51:161166, 1950. [9] D. Djokovi´c.Distance preserving subgraphs of hypercubes. Journal of Combinatorial Theory Series B, 14:263–267, 1973. [10] D. Eppstein. The lattice dimension of a graph. European Journal of Combinatorics, 26:585– 592, 2005. [11] T. Feder. Stable networks and product graphs, volume 116 of Memoirs of the American Math- ematical Society. AMS Bookstore, 1995. [12] D. Fulkerson. Note on dilworths decomposition theorem for partially ordered sets. Proc. Amer. Math. Soc., 7:701702, 1956. [13] R. Graham and P. Winkler. On isometric embeddings of graphs. Transactions of the American Mathematical Society, pages 527–536, 1985. [14] J. Hagauer, W. Imrich, and S. Klavˇzar. Recognizing median graphs in subquadratic time. Theoretical Computer Science, pages 123–136, 1999. [15] W. Imrich and S. Klav˘zar. Product Graphs: Structure and Recognition. John Wiley and Sons, 2000. [16] W. Imrich and M. Kovˇseb.Lattice embeddings of trees. European Journal of Combinatorics, pages 1142–1148, 2009. [17] D. Knuth. The Art of Computer Programming, volume IV. Addison-Wesley, 2008. √ [18] S. Micali and V. Vazirani. An O( VE) algorithm for finding maximum matchings in general graphs. In Proceedings of the 21st Annual IEEE Symposium on Foundations of Computer Science, pages 17–27, 1980.

12 [19] S. Ovchinnikov. The lattice dimension of a tree. Manuscript, 2004.

[20] W. Schnyder. Planar graphs and poset dimension. Order, pages 323–343, 1989.

[21] P. Winkler. Isometric embedding in products of complete graphs. Discrete Applied Mathemat- ics, pages 221–225, 1984.

Appendix

Lemma 3 Suppose the join-irreducible elements of M are a1, a2, . . . , ak. Let Y (M) be the graph on the set {si,0, si,1, i = 1, . . . , k} where {si,1, sj,0} is an edge if and only if ai ≤u∗ aj, and {si,0, sj,0} is an edge if and only if ai and aj have no upper bounds in M. Then Y (M) = Sc(G).

Proof Recall how the semicube graph Sc(G) is constructed. Without loss of generality, let us assume that it is based on the irredundant isometric embedding of G in Qk that is constructed from the twin chain decomposition where Ci = {ai} for i = 1, . . . , k. Hence, Si,0 = {v|ai 6∈ J(v)} while Si,1 = {v|ai ∈ J(v)}. ∗ ∗ Consider any pair of join-irreducible elements ai and aj. Since u 6∈ Si,1∪Sj,1 because J(u ) = ∅, (si,1, sj,1) is not an edge in Sc(G). If ai and aj have an upper bound z in M, z 6∈ Si,0 ∪ Sj,0 since ai, aj ∈ J(z). Hence, (si,0, sj,0) is not an edge in Sc(G). Similarly, if ai and aj are incomparable in M, ai 6∈ Si,1 ∪ Sj,0 while aj 6∈ Si,0 ∪ Sj,1 so both (si,0, sj,1) and (si,1, sj,0) are not edges in Sc(G). Now there are three possible relationships between ai and aj.

Case 1: ai and aj are comparable. Without loss of generality, let us assume ai ≤u∗ aj. Thus, for each node v, if aj ∈ J(v) then ai ∈ J(v). That is, Sj,1 ⊆ Si,1 and Si,0 ⊆ Sj,0. Since Si,0∪Si,1 = V (G), it follows that Sj,0 ∪ Si,1 = V (G). Furthermore, ai ∈ Sj,0 ∩ Si,1 so (si,1, sj,0) is an edge in Sc(G). From the previous paragraph, we have already established that neither (si,0, sj,0) nor (si,1, sj,1) are edges in Sc(G). Finally, since ai 6∈ Si,0 ∪ Sj,1,(si,0, sj,1) is not an edge in Sc(G). Hence, we have shown that if ai and aj are comparable, only (si,1, sj,0) is an edge in Sc(G).

Case 2: ai and aj are incomparable and have an upper bound. The first paragraph has established that in Sc(G), there are no edges between si,0, si,1, sj,0 and sj,1.

Case 3: ai and aj are incomparable and have no upper bound. Hence, for every node v, either ∗ ai 6∈ J(v) or aj 6∈ J(v). That is, Si,0 ∪ Sj,0 = V (G). Moreover, u ∈ Si,0 ∩ Sj,0 so (si,0, sj,0) is an edge in Sc(G). The first paragraph has established that (si,0, sj,0), (si,0, sj,1) and (si,1, sj,0) are not edges in Sc(G). Hence, we have shown that in the third case, only (si,0, sj,0) is an edge in Sc(G).

By considering all possible relationships between ai and aj, we have shown that the edge set of Sc(G) is {(si,1, sj,0)|ai ≤u∗ aj} ∪ {(si,0, sj,0): ai and aj have no upper bound }. 2

∗ Proposition 1 For each node y of G and for each ai ∈ J(y), the path from u to y in T has one edge (u, v) such that J(v) − J(u) = {ai}.

∗ Proof Recall that T is the tree obtained by running BFS on Gu∗ so that the path from u to y is a shortest u∗ – y path in the graph. According to Lemma 1, its length is |K(y) − K(u∗)|, ∗ ∗ ∗ which is equal to |J(y)| since J(u ) = ∅. Now, if the u – y path is u0 = u , u1, . . . , us = y, where s = |J(y)|, it has to be the case that |J(ui+1) − J(ui)| = 1 because ui is covered by ui+1 in the ∗ ∗ median semilattice MG,u∗ . Hence, the elements of J(u ) are added one at a time along the u – y path. In particular, there must be one edge (uj, uj+1) so that ai 6∈ uj but ai ∈ uj+1. 2

13