Finally (in § 4) we give Algol realizations of the various algorithms. 1. Triangularizing partial orderings. By a partial ordering we mean a reflexive, transitive, ...
B I T 9 (1969), 97--118
ALGORITHMS OVER PARTIALLY ORDERED SETS R. M. B A E R a n d O. O S T E R B Y
Abstract. W e here s t u d y some problems concerned with the computational analysis of finite p a r t i a l l y ordered sets. W e begin (in § I) b y showing t h a t the m a t r i x representation of a b i n a r y relation R m a y always be taken in triangular form if R is a p a r t i a l ordering. We consider (in § 2) the chain structure in p a r t i a l l y ordered sets, answer the combinatorial question of how m a n y m a x i m a l chains might exist in a partially ordered set with n elements, a n d we give a n algorithm for enumerating all maximal chains. We give (in § 3) algorithms which decide whether a p a r t i a l l y ordered set is a (lower or upper) semi-lattice, and whether a lattice has distributive, modular, a n d Boolean properties. Finally (in § 4) we give Algol realizations of the various algorithms.
1. Triangularizing partial orderings. B y a partial ordering w e m e a n a r e f l e x i v e , t r a n s i t i v e , a n t i s y m m e t r i c b i n a r y t r e l a t i o n . A p a r t i a l l y o r d e r e d s e t is a p a i r (X, < ) i n w h i c h X is a s e t of e l e m e n t s , a n d < is a p a r t i a l o r d e r i n g o v e r X . W h e n s e v e r a l partially ordered sets with the same set X are under discussion we sometimes use < and < * to refer to the distinct partial orderings. B y a chain (= totally ordered set) w e m e a n a p a r t i a l l y o r d e r e d s e t X i n w h i c h (for a l l x , y ~ X ) x < y o r y < x . I f n e i t h e r x < y n o r y < x t h e n w e w r i t e x y a n d w e s a y t h a t x a n d y a r e incomparable. W e w r i t e x < y if x < y b u t x 4 y . T h e r e a r e s e v e r a l w a y s t o r e p r e s e n t b i n a r y r e l a t i o n s . L e t X = (x I . . . . . xn} have exactly n elements. Then by the binary representation of (X, < )
x1 x2 x8 x4 x5 x~
x1
x2
x3
x4
x5
x6
1 0 0 0 0 0
1 1 0 0 0 0
1 1 1 1 1 0
1 0 0 1 0 0
1 0 0 0 1 0
1 0 0 0 1 1
Fig. 1. t Standard definitions which we do not include here axe used as in [1]. Received July 20, 1968. Revised Jan. 15, 1969. BIT 9 -- 7
98
R.M. BAER AND O. OSTERBY
we mean the n × n matrix (btj) over {0, 1} such that bti= 1 if and only if x i < x J (otherwise bij=O ). An alternate representation, called the Hasse diagram, consists of the drawing of a graph in which the nodes are the elements of X and arcs indicate the partial ordering in the sense that, x, < xj if and only if it is possible to move upward from x i to xj. Thus the binary representation of an example is given in Fig. 1, and the Hasse diagram of the same partially ordered set is given in Fig. 2. ~
~
x5
~C..)xI x5 Fig. 2.
Suppose that (X, < ) is a partially ordered set and that Y is a totally ordered set. If X and Y have the same cardinal and a is a one-to-one mapping of X onto Y such that, for all, x , x ' e X , x < x' implies a(x) < a(x') then we say that a is a Szpilrajn mapping. The designation is due to a well known result due to Szpilrajn [2], which we m a y state in the following way. T~.O~EM 1.1 (Szpflrajn). For any partially ordered set (X, < ) there exists a totally ordered set Y and a Szpilrajn mapping (I such that ~ maps X onto Y. The original proof of this result is neat b u t nonconstructive. I t proceeds b y considering the class of all partial orderings which contain a given one and then observing that a maximal chain (under the partial ordering given b y set-inclusion) exists in the class of all such partial orderings, and that this maximal chain has a maximal element (which is then necessarily a total ordering, otherwise it could be extended). If the partially ordered set (X, < ) has a complicated structure then this is reflected in the structure of the totally ordered set Y, as shown in [3]. We are here concerned with the case in which X is finite. In this case, a very simple constructive proof of Szpilrajn's theorem m a y be given. We first recall that a minimal element of a subset S _ X is any element s e S such that there exists no element x e S satisfying x < s.
ALGORITHMS OVER P A R T I A L L Y O R D E R E D SETS
99
Let (X, < ) be a partially ordered set and let. IXI = n be the finite cardinal of X. If n = 1 then the theorem is obviously true. Suppose that the theorem is true for all n = 1. . . . . /c; then we show that it must be true for n =/c + 1. Let x e X be a minimal element of X (there must be one, since X is finite). Let Y be totally ordered, I Y l = I c + l and let y be the least element of Y. B y induction there is a mapping a of X \ {x} onto Y \ {y}. Extend a b y setting a ( x ) = y . Then it is clear that the extended mapping a is a Szpilrajn mapping. This gives the theorem in the finite case. THEOREM 1.2. Let (X, < ) be a partially ordered ordered set and a be a Szpilrajn mapping such Y × Y -+ {0, 1} be the mapping defined as follows. B ( y , y ' ) = l if and only if y = a ( x ) , y ' = a ( x ' ) and B ( . , . ) is upper-triangular.
set and let Y be a totally that a: X -~ Y. Let B: For all y,y' e Y, we let x < x'. Then the matrix
PROOF. Suppose that there is some pair y,y' ~ Y such that B(y,y') = 1. L e t x , x ' e X be the elements such that ~(x) =y, a(x') = y ' . B u t B ( y , y ' ) = 1 if and only if a - l ( y ) < a - l ( y ') which implies that a ( a - l ( y ) ) = y < y ' = a(a-l(y')) since a is one-to-one and order-preserving. This gives Theorem 1.2. A binary relation ( X , - ( ) that is not necessarily a partial ordering m a y still be triangularizable (that is, if when considered as a graph it contains no directed circuits x i l ~ , x i - ~ . . . -~xil ). We give an algorithm (suggested b y the preceding proof) for obtaining a triangular representation from an arbitrary representation B of ( X , ~ ) , where X = {xl . . . . .
1) S e t / c : = l a n d s e t i : = k . 2) If there is no j > k (j4-i) such that B ( j , i ) = 1 then do 4); otherwise do 3). 3) If i = n then ( X , ~ ) is not triangularizable; otherwise set i : = i + 1 and then do 2). 4) If i =/~ then do 5); otherwise permute the indices of x k and xt (that is, permute the kth and the ith rows of B and the kth and ith columns of B). Then do 5). 5) S e t / c : = k + 1 and set i:=/c; if k < n then do 2); otherwise stop. The algorithm, in effect, starts with xl, and seeks an element y (y :~ xl) such that y . ~ x r If no such y is found, then the procedure is iterated on the ( n - 1 ) × ( n - 1 ) submatrix B~, obtained from B b y deleting the first row and column. Otherwise we go to x 2 and search for an element y
i00
R.M. B A E R A N D O. O S T E R B Y
among xl, x 8. . . . . xn such that y.,(x~, etc. If we finally find an element x i such that y-~x~ implies y = x i , then we switch the first index and i (by permuting the corresponding rows and corresponding columns), and the procedure is iterated on B1; otherwise the binary relation i s not triangularizable (and therefore not a partial ordering). Thus at each selection an element is chosen which is minimal with respect to the set of those elements not yet selected (or the process terminates because no such element exists). If the entire matrix B can be processed to some matrix B', it is clear that, for all i , j < n ( i < j ) either zl~(xi or x~ and x t are incomparable. This proves that the algorithm produces an upper-triangular representation B'. If it is known that B represents a binary relation with no directed circuits, then the algorithm can be simplified b y deleting 3) and, in 2) replacing 'do 3)' b y 'let i be the smallest such j and do 2)'. This algorithm, instead of searching the elements according to increasing index, will descend along a chain, and therefore in general lead faster to the result.
2. Maximal chains.
I t is sometimes necessary to know the number of maximal chains in a partially ordered set, and occasionally it is necessary to identify these chains. We construct an algorithm which finds all maximal chains in a partially ordered set. In some numerical procedures, concerned with maximal chains, each such chain m a y be generated, put to its use, and then discarded. However, other cases require that the entire collection of maximal chains be simultaneously available during a course of computation. This requires an estimate of the corresponding memory allocation which is necessary; accordingly we derive an upper bound on the possible number of maximal chains and we show that this bound is sharp. Let (X, < ) be a partially ordered set and let x and y be elements of X. Then we say that y is a cover of x if x < y and there exists no element u in X which satisfies x < u < y. Let -~ denote the binary relation over X which corresponds to the notion of cover: x - ~ y just in case y is a cover of x. In enumerating the maximal chains we use an idea, due to David Matula, and first derive the cover relation -~. (The algorithm, for this, is given below as procedure C O V E R . ) The mode of enumerating the maximal chains m a y be understood with the following example. Suppose that a partially ordered set is given b y an upper-triangular
ALGORITHMS OVER PARTIALLY
x7 x3
ORDERED
~
SETS
I01
:~6
( z/~
x5
Xl ( / ~x2 F i g . 3.
representation, as in Fig. 3. Start with the minimal element with lowest index (that is, xl); find the cover of x 1 with lowest index (that is, x3); find the cover, of the last element selected, with least index, and continue until at some stage no further cover is found (that is, the last element which has been selected is maximal). So this gives the maximal chain xl < xa < x ~ < xs. Then, descending along this chain, each element is examined to see if it has a cover other t h a n the cover occurring in the chain. If there is another cover, this cover is used to replace the corresponding cover, and completion of the resulting new chain takes place as above. This process is iterated until no further chains can be generated. (Note t h a t M1 these chains begin with the same minimal element.) Thus the chains x 1 < x a < x~ < x s xI
2. I n a normalizing partition there can occur no Pi = 1 since 1- Pt < 1 ÷ Pi. F u r t h e r m o r e a triple of 2's c a n n o t occur since 2 + 2 + 2 = 3 + 3 b u t 2 . 2 . 2 < 3.3. Since a pair of 2's is equivalent to a single 4, this also excludes pairs 2,4 a n d 4,4 as well as triples 2,2,4. Since Pi > 4 implies ps< 3 . ( p i - 3 ) , in a normalizing partition all Pl m u s t be 3 w i t h the possible exception of (i) a single 2 or (ii) a single 4 (which is equivalent to a pair 2,2). I t is now easy to arrive a t t h e given formulae, a n d this concludes the proof of the Corollary. 3. Analysis of structure. I t is often of interest to know whether a partial ordering has additional properties, for example, t h a t it is a lattice or a Boolean algebra. To arrive a t the relevant definitions let us begin b y defining lower a n d upper bounds for collections of elements. L e t (X, < ) be a partial ordering. If Y is a n o n c m p t y subset of X t h e n a n element x e X is called a lower bound of t h e subset :Y if (Vy e Y ) [ x ~ y ] ; a n element z e X is called a greatest lower bound for Y if (i) z is a lower b o u n d of Y a n d (ii) x is a lower b o u n d of Y implies x < z. (Upper bound a n d least upper bound are defined dually.) I n general the greatest lower b o u n d (glb) of x a n d y is denoted x ^ y ; the least upper b o u n d (lub) of x and y is denoted x v y . We consider finite partially ordered sets X = (xl, x 2. . . . . x~) of indexed elements. If X is in upper triangular f o r m (§ I), t h a t is, x t < x j implies i = 0 end
LUB;
Boolean p r o c e d u r e L A T T I C E (A, rank, GLU) ; c o m m e n t Let A be a partial ordering in upper triangular form. I f A is a lattice then the value t r u e is returned and GLU [1 : rank, 1 : rank]
contains the greatest lower bound of the pair of elements with indices i and j (i