Journal of Graph Algorithms and Applications Confluent Hasse Diagrams

We show that a transitively reduced digraph has a confluent upward drawing if and only if its reachability relation has order dimension at most two. In this case, we construct a confluent upward drawing with O(n 2) features, in an O(n) × O(n) grid in O(n 2) time. For the digraphs representing series-parallel partial orders we show how to construct a drawing with O(n) features in an O(n) × O(n) grid in O(n) time from a series-parallel decomposition of the partial order. Our drawings are optimal in the number of confluent junctions they use.


Introduction
One of the most important aspects of a graph drawing is that it should be readable: it should convey the structure of the graph in a clear and concise way. Ease of understanding is difficult to quantify, so various proxies for it have been proposed, including the number of crossings and the total amount of ink required by the drawing [1,18]. Thus given two different ways to present information, we should choose the more succinct and crossing-free presentation. Confluent drawing [7-9, 15, 16] is a style of graph drawing in which multiple edges are combined into shared tracks, and two vertices are considered to be adjacent if a smooth path connects them in these tracks ( Figure 1). This style was introduced to reduce crossings, and in many cases it will also improve the ink requirement by representing dense subgraphs concisely. However, it has had a limited impact to date, as there are only a few specialized graph classes for which we can either guarantee the existence of a confluent drawing or test for confluence efficiently. A closely related graph drawing technique, edge bundling [10], differs from confluence in emphasizing the visualization of high level graph structure, but does not necessarily seek to reduce the number of edge crossings.
Hasse diagrams are a type of upward drawing of transitively reduced directed acyclic graphs (DAGs) that have been used since the late 19th century to visualize partially ordered sets. To maximize the readability of Hasse diagrams, as with other types of graph drawing, we would like to draw them without crossings. Thus upward planar graphs (DAGs that can be drawn so that all edges go upwards and no edges cross) have been an important thread of research in graph drawing. A DAG is upward planar if and only if it is a subgraph of a Fig. 2. A simple DAG P (left) that is not upward planar, although its underlying graph is planar. Its Dedekind-MacNeille completion (middle) is upward planar, with an added element (shaded). Replacing that element with a junction creates an upward confluent drawing of P (right).
planar st-graph, i.e. a planar DAG with one source and one sink, both on the outer face [6]. Testing upward planarity is NP-complete [12] but for DAGs with a single source or a single sink it may be tested efficiently [4,17]. However, many DAGs (even planar DAGs such as the one in Figure 2) are not upward planar.
In this paper, we bring these threads together by finding efficient algorithms for upward confluent drawing of transitively reduced DAGs. We show that a graph has an upward confluent drawing if and only if it represents a partial order P with order dimension at most two, and that these drawings correspond to twodimensional lattices containing P . We construct the smallest lattice containing P (its Dedekind-MacNeille completion) in worst-case-optimal time, and draw it confluently in area O(n 2 ), using as few confluent junctions as possible. For seriesparallel partial orders, the time and number of junctions can be reduced to linear.

Posets and Lattices
A partially ordered set (partial order, or poset) P = (V, ≤) is a set V with a reflexive, antisymmetric, and transitive binary relation ≤. We adopt the convention that n = |V | unless otherwise stated. We also use a < b to denote that a ≤ b and a = b. We say that a covers b in P if b < a and x ∈ P such that b < x < a. Elements a, b ∈ P are comparable if a ≤ b or b ≤ a; otherwise, we write a||b to indicate that they are incomparable. A total order or linear order is a partial order in which every pair of elements in P is comparable. If R is a set of linear orders R i , we can define a poset P as the intersection of R: that is, a ≤ b in P if and only if a ≤ b in every linear order R i . If P can be defined from R in this way, then R is called a realizer of P . Every partial order P has a realizer; the dimension dim(P ) is the smallest number of linear orders in a realizer of P .
If X ⊆ P is any subset of P , then an element a ∈ P is called a lower bound of X if it is less than or equal to every element of X. Similarly, an element b is called an upper bound of X if it is greater than or equal to every element of X. If X has a lower bound a that belongs to X itself, then a is the (unique) least element in X, and similarly if X has an upper bound b that belongs to X then b is the (unique) greatest element in X. If the set A of lower bounds of X has a greatest element a, then a is the greatest lower bound or infimum of X, and similarly if the set B of upper bounds of X has a lowest element b then b is the least upper bound or supremum of X. If P itself has an infimum or a supremum, these elements are typically denoted by 0 and 1 respectively. If P contains both an infimum and a supremum, it is said to be bounded.
A poset L is a lattice if for every pair of elements x and y in L the set {x, y} has both an infimum and a supremum. In this context, the supremum of {x, y} is called the meet of x and y and denoted x ∧ y, and similarly the infimum is called the join and denoted x ∨ y. A lattice L is complete if every subset of L has an infimum and supremum in L. Every finite lattice is complete and bounded.

Hasse Diagrams and Upward Planarity
Every poset P = (V, ≤) can be represented by a directed acyclic graph G which has a vertex for each element in P and an edge uv for each pair (u, v) with u ≤ v in P . However, when we draw a poset it is more common to draw a different DAG, the transitive reduction G of G, in which there is an edge from u to v in G if and only if v covers u in P . A Hasse diagram of P is an upward drawing of G , meaning that the y coordinate of the head of each edge is greater than the y coordinate of the tail of each edge, so that the drawing "flows" upward from smaller elements to larger elements. In a Hasse diagram, we do not need to explicitly draw the edges as directed edges: the direction of an edge is represented implicitly by the relative position of its endpoints. There is an upward path from a to b in a Hasse diagram of P if and only if a ≤ b. A poset is planar if it has a Hasse diagram that is upward planar, i.e. its transitive reduction has an upward drawing in which none of the edges intersect except at a shared vertex.
A finite lattice is planar if and only if its transitive reduction is a planar stgraph, a DAG which contains exactly one source s and one sink t both of which belong to the outer face of an upward planar drawing [28]. More generally, any DAG is upward planar if and only if it is a subgraph of a planar st-graph [6]. In the other direction, every planar finite bounded poset must be a lattice [3,5,19]. This implies that a two-dimensional bounded poset that is not a lattice (such as the one on the left of Figure 2) cannot have an upward planar drawing, and that planarity (a crossing-free drawing) and two-dimensionality (realization by a pair of linear orders) are distinct for non-lattice posets.

Lattice Completion of a Poset
The Dedekind-MacNeille completion of a poset P is the smallest complete lattice containing P [22]. For any subset X of P , let X − and X + denote the set of lower bounds and upper bounds of X respectively. A cut of P is a pair A, B ⊆ P such that A + = B and A = B − ; the completion of P has these cuts as its elements. The completion is partially ordered by set containment: if (A, B) and (C, D) are cuts, then (A, B) ≤ (C, D) if and only if A ⊆ C and B ⊇ D. The element of the completion corresponding to an element x of P is the cut ({x} − , {x} + ), and the new elements added to P to make it into a lattice come from cuts (A, B) for which A ∩ B = ∅. The completion automatically has the same dimension as the partial order from which it was constructed [27].
Ganter and Kuznetsov [11] give a stepwise algorithm for constructing the completion of P . Given a poset P and its completion L they show how to complete a one-element extension of P in time O(|L|·|P |·ω(P )), where ω(P ) denotes the width of P . To compute the completion of a large poset, they begin with a single-element poset (whose completion is trivial) and use this subroutine to add elements one at a time; therefore, the total time is O(|L| · |P | 2 · ω(P )). Nourine and Raynaud [26] give an algorithm with running time O((|P | + |B|) · |B| · |L|) where B is a basis of P (a set of subsets of P which generate L). As part of our drawing algorithm, we improve these results in the case of two-dimensional posets: we show for such sets how to construct the completion in time O(|P | 2 ), optimal in the worst case since (as we also show) there exist two-dimensional posets whose completion has a quadratic number of elements.

Confluent Drawing
Confluent drawing is a technique for drawing non-planar diagrams without crossings [7][8][9]15,16] by merging together groups of edges and drawing them as tracks that, like train tracks, meet smoothly at junction points but do not cross. A confluent drawing consists of a set of labeled points (vertices and junctions) and curves (track segments) in the Euclidean plane, such that the two endpoints of each track segment are vertices or junctions, such that no two track segments intersect except at a shared endpoint, and such that all track segments that meet at a junction share a common tangent line at that point. The graph represented by a confluent drawing has as its vertices the vertices of the drawing; two vertices u and v are adjacent if and only if there is a smooth curve in the plane from u to v that is a union of track segments and that does not pass through any other vertex. (Some papers on confluence require that this curve also be nonself-intersecting but that requirement is irrelevant for upward drawings since monotone curves cannot self-intersect.) An undirected graph G is confluent if and only if there exists a confluent drawing that represents it.
We define a confluent diagram of a poset to be a drawing of its transitive reduction in a way that is both confluent and upwards. In other words, if G is a directed acyclic graph representing a poset P , then we define a confluent diagram of P to be an upward confluent drawing of the transitive reduction of G in which all tracks are oriented upwards (monotonic in the y direction), and therefore all smooth curves passing through the tracks are similarly oriented. For each pair of elements a, b ∈ P , the drawing should have a smooth track from a upwards to b if and only if a is covered by b. For technical reasons we also require that for each source there exists an unbounded y-monotone curve downwards that does not cross the diagram -that is, that each source can be seen from below -and symmetrically that each sink can be seen from above. In the application to visualization of partial orders, this is a natural restriction as it makes the minimal and maximal elements easy to find in the drawing.

The Algorithm
Let G be a poset with dimension at most two. We now describe an O(n 2 ) algorithm to embed a confluent diagram of P in an O(n) × O(n) grid. That is, we will generate an upward confluent drawing of the transitive reduction of a DAG representing P such that each vertex in the drawing has integer coordinates.
Our algorithm has three phases. In the first phase, we embed the elements of P in a (2n + 1) × (2n + 1) grid. Recall that since P has dimension two, it is realized by two linear orders, which correspond to two different total orderings of the same n elements in P . Thus, the first steps of our algorithm are: 1. (a) Find two linear orders L 1 and L 2 that realize P . This can be done in O(n 2 ) time from any graph whose transitive closure is P by Algorithm 1 of [21]. (b) For each element p of P , having position p 1 in L 1 and p 2 in L 2 with 1 ≤ p i , p j ≤ n, place a vertex representing p in the grid with coordinates (2i, 2j).
After this step, the even rows and columns in the grid each contain exactly one element of P , and the dominance relationship of these points corresponds to the order of the elements in P . Recall that for two elements p and q in the plane, p dominates q if and only if p i ≥ q i for each coordinate i and p = q.
In the second phase, we insert additional points representing elements of the completion of P ; these completion nodes correspond to confluent junctions in the confluent diagram of P . We defer to a later section the proof that the dominance order on the points generated in the first two phases gives the completion of P . -The point with x-coordinate i + 1 has y-coordinate greater than j + 1.
-The point with y-coordinate j − 1 has x-coordinate less than i − 1.
-The point with y-coordinate j + 1 has x-coordinate greater than i + 1. In addition if P does not already have a least or a greatest element, then insert invisible points at (1, 1) and (2n + 1, 2n + 1) respectively.
In the third phase, we generate the segments of the confluent diagram. These segments correspond to direct dominance pairs of points from the first two phases. It is possible to find all dominance pairs in a set of N points in time O(N log N + k) [13] where k is the number of dominance pairs, but in our case this would only lead to an O(n 2 log n) time bound. Instead, we leverage the fact that the vertices are embedded in an O(n) × O(n) grid, and use the following O(n 2 + k) time method to generate dominance pairs using a stack-based algorithm related to Graham scan within each row. We prove later that the diagram is planar and therefore that the number of dominance pairs k = O(n 2 ).
3. Initialize for each column c a value t c , the topmost element seen so far in column c. Then, for each row r from 1 to 2n + 1: (a) Initialize an empty stack S. (b) For each column c from 1 to 2n + 1: i. If there is a vertex or junction p at (r, c), add an edge from every element of S to p, add an edge from t c to p (if t c is non-empty), and set t c to p. ii. If t c is non-empty, pop all items from S whose row number is less than or equal to the row number of t c , and push t c onto S.
Thus we have computed the coordinates of all elements, confluent junctions, and edges in the confluent diagram. When we render the drawing, we rotate it 45 • counterclockwise to make it upward confluent (Figure 3).
Examples of non-confluent and confluent drawings of the same 100-element set are shown in Figure 4. Our Python implementation renders the confluent track segments as cubic Bézier curves with control points at a small fixed distance directly above and below each confluent junction. Two such curves cannot cross each other: for pairs of edges that do not share an endpoint, this follows from the fact that the convex hulls of the control points are disjoint and that the curves lie within the convex hulls, while for pairs of curves that share an endpoint it follows from the fact that the two curves are images of each other under an affine transformation of the plane and that (for pairs of edges sharing an endpoint) the direction that any point on the curve is translated by this affine transformation is transverse to the tangent direction of the curve at that point.
If the input is provided as a realizer rather than as a graph, and its completion has few elements, then it is possible to construct the diagram more efficiently. To do so, construct for each odd-indexed row or column of the integer grid an axis-parallel line segment that passes through a grid point if and only if that point meets two of the four conditions for adding a junction in phase two of our algorithm. The junctions can be recovered as the intersections of these line segments, and we may compute the edges of the diagram using an output-sensitive algorithm for dominance pairs. By using integer searching data structures the total time for this algorithm may be reduced to O((n + k) log log n), where k is the number of confluent junctions; we omit the details.

Algorithm Correctness and Minimality
In this section we prove that the algorithm of Section 3 is correct and has optimal running time. Our analysis also shows that a poset P has a confluent diagram if and only if it has dimension at most two. [3]). Let P be a bounded finite planar poset. Then P is a lattice and has dimension at most 2.

Lemma 1 (Baker, Fishburn and Roberts
Lemma 2. Let P be a finite poset with a confluent Hasse diagram D. Then dim(P ) ≤ 2, and there exists a two-dimensional lattice C containing P such that the elements of C \ P (other than the top and bottom element, if they do not belong to P ) correspond one-for-one with the confluent junctions of D.
Proof: Replace the confluent junctions of D with vertices, and re-interpret the confluent segments as edges between these vertices. If there is more than one minimal vertex of P , add a vertex below all minimal vertices, connected to the minimal vertices by upward edges, and similarly if there is more than one maximal vertex of P , add a vertex above all maximal vertices connected to them by edges. The modified drawing is st-planar and hence by Lemma 1 represents a lattice, which clearly contains P . Lemma 3. Let P be a finite poset with order dimension at most two, let C be the completion of P , and let S be the set of elements of C \ P (other than the top and bottom element, if P itself is not bounded). Then the elements of S coincide with the junction points added in phase 2 of our algorithm, and the dominance ordering on these points coincides with the lattice ordering in C.
Proof: In one direction, let p be a junction point added in phase 2 of our algorithm, and p − and p + be the sets of points from phase 1 that are dominated by p and that dominate p respectively. Then it follows from the four conditions according to which phase 2 adds a point that (p − , p + ) forms a cut in P . The equivalence of the dominance and lattice orderings on pairs consisting of a junction point and a point from P follows immediately, and the same equivalence for pairs of junction points is also easy to verify.
In the other direction, we must show that we add a junction point for every element of S, that is, every cut (L, U ) where L has more than one maximal element and U has more than one minimal element. Let i be one less than the minimum x-coordinate of a point in U , and let j be one less than the minimum y-coordinate; then (because the coordinates of points in P are their positions in the two orderings of a realizer) the set L of points dominated by every point in U equals the set of points below and to the left of (i, j). Two of the four conditions of phase 2 are automatically met at (i, j): the points with x-coordinate i + 1 and with y-coordinate j + 1 are both in U and are distinct because U has more than one minimal point. The other two conditions must also be met, for if they were not then the point violating the condition would dominate L, contradicting the fact that all points that dominate L belong to U . Theorem 1. A given partial order P has a confluent diagram if and only if dim(P ) ≤ 2. If P has a confluent diagram, the algorithm of Section 3 computes a valid confluent diagram of P , and embeds that diagram in a O(n) × O(n) grid in worst case optimal O(n 2 ) time. The number of confluent junctions in the drawing is the minimum possible for any confluent diagram of P .
Proof: If a poset P has dimension three or more, then so does any lattice containing it, and by Lemma 1 and Lemma 2 there can be no confluent diagram of P . Otherwise, we may assume that P has dimension at most two.
By Lemma 3, the dominance ordering on the points computed by our algorithm coincides (except possibly for the removal of the top and bottom elements) with the completion of P . In this set of points, there can be no crossing pairs of dominance relations, for if the edges (L 1 , U 1 )-(L 2 , U 2 ) and (L 3 , U 3 )-(L 4 , U 4 ) crossed (where (L i , U i ) is a cut either added in the completion or corresponding to an original point of P ) then (L 1 ∪L 3 , U 2 ∪U 4 ) would also be a cut whose point would lie between the other four points, contradicting the assumption that these edges represent minimal dominance pairs. Therefore, the diagram constructed by our algorithm is planar, and by Lemma 1 it must represent a lattice superset of P . The added elements belong to the completion, so the diagram must represent a subset of the completion, and since the completion has no proper Fig. 5. A poset P with O(n) elements and dimension 2 whose completion has size Ω(n 2 ). On the left is the normal Hasse diagram, and on the right is the confluent version as drawn by our algorithm. The two permutations L1 and L2 generating P are the identity and the permutation (3n, 3n − 2, . . . , n; 4n + 1, n − 1, 4n, n − 2, . . . , 3n + 2, 0; 3n + 1, 3n − 1, . . . , n + 1).
lattice subsets it must represent the completion itself. The completion gives the minimum number of added elements (and therefore, by Lemma 2, the minimum number of junctions) of any diagram for P .
Our algorithm spends O(n 2 ) time in its first two phases as it iterates over O(n 2 ) grid cells spending constant time per cell. In the third phase, it uses constant time per edge and by planarity there are O(n 2 ) edges, so the time is again O(n 2 ). This time bound is optimal since (as shown in Figure 5) there exist two-dimensional posets whose completion has Ω(n 2 ) elements.
Although our method produces drawings in a grid of linear dimensions, it may be possible in some cases to compact our drawings into a smaller grid. An algorithm of de la Higuera and Nourine [14] may be used to find the smallest grid into which a drawing produced by our algorithm can be compacted. A series-parallel partial order is a poset that can be built up from single elements by two simple composition operations:

Confluent Drawings of Series-Parallel Posets
-The series composition P ; Q of posets P and Q is the order on the set P ∪ Q in which p ≤ q for every p ∈ P and q ∈ Q. -The parallel composition P ||Q is the order on P ∪ Q in which every pair of an element from P and an element from Q are incomparable. Pairs of elements that are both from P or both from Q retain their ordering in the larger set.
Series-parallel partial orders are attractive because many important computational problems can be solved more easily in them than in more general posets, and because they have applications to a wide variety of problems including scheduling [25], concurrency [20], data mining [23], networking [2], and more (see [24]).
Series-parallel partial orders can be represented naturally by a binary tree, known as a decomposition tree of the order. The leaves of the tree correspond to single element sets and the internal nodes of the tree correspond to series or parallel composition operations. As the following theorem shows, given a decomposition tree T for a series-parallel partial order P , we can draw the confluent diagram of P in linear time by traversing T , performing the corresponding composition operations, and inserting confluent junctions when necessary.
Theorem 2. Let P be a series-parallel partial order, given as its decomposition tree. Then a confluent diagram of P with a linear number of junctions can be drawn in an O(n) × O(n) grid in linear time.  Proof: We traverse the decomposition tree in postorder, recursively finding embeddings for each subtree. For each tree node, we do the following: 1. If the node is a leaf, then we embed the corresponding element in a single grid cell 2. Otherwise, if the node is a series or parallel node, then we translate the grid embeddings of its two children so that their bounding boxes meet corner to corner (Figure 7). 3. For a series composition A; B we also insert a confluent junction at the shared corner of A and B if and only if A has more than one maximal element and B has more than one minimal element ( Figure 8).
By using a linked list of the maximal and minimal nodes for the current subtrees, we can perform these operations in time proportional to the number of leaves in the decomposition tree. Therefore the total time is linear. The size of the grid will be proportional to the size of the decomposition tree, i.e., O(n) × O(n)

Conclusions
We have designed, analyzed, and implemented an algorithm for drawing confluent Hasse diagrams using a minimum number of confluent junctions. It would be of interest to test experimentally how many crossings it eliminates, and how much ink it saves. Also, upward planarity may be tested even for non-st-planar graphs that have only one source or one sink; can similar conditions be extended to the case of upward confluent drawings? Can we efficiently find upward planar drawings of graphs that are not transitively reduced? If a partially ordered set must be drawn with crossings, can we use confluence in a principled way to keep the number of crossings small? We leave these questions to future research.