In this entry, I’ll attempt to record the important ideas Knuth presented in his 14^{th} Annual Christmas Tree Lecture, part of his regular Computer Musings.

While I wasn’t fortunate enough to attend the lecture in person, I did go through the recording that, thankfully, was quite well done. Like all other “musings”, this one included some fascinating anecdotal bits (no pun intended) and Knuth’s good sense of humor was sprinkled throughout; but most noticeable was his infectious enthusiasm for the topics he spoke about. The preface to Volume 4 (printed in Fascicle 0) begins with:

The title of Volume 4 is

Combinatorial Algorithms, and when I proposed it, I was strongly inclined to add a subtitle:The Kind of Programming I Like Best.

His excitement for these topics clearly showed in the lecture. It is a must-watch for anyone interested in The Art of Computer Programming (especially Volume 4 on Combinatorial Algorithms), or interested in learning a very useful data structure for combinatorial applications — ZDDs.

ZDDs (Zero-suppressed Binary Decision Diagrams) were introduced by Shin-Ichi Minato in 1993, about 7 years after Randal Bryant introduced his key ideas of the BDD data structure; one of which was that keeping the BDD both *reduced* and *ordered* is important in practice. In his “Fun with BDDs” musing (this June), Knuth mentioned that this 1986 paper of Bryant’s took computer science almost by storm; remaining the most-cited paper in the field for many years (see here).

Minato’s idea (of zero-suppression in BDDs) was motivated by his observation that when working with combinatorial objects, very often tests in BDDs have to be made just to ensure a potentially large number of variables are FALSE. So, in ZDDs, we just “skip over” any node whose `HI` pointer leads to (the FALSE sink) — if having TRUE results in the subfunction rooted at to become FALSE, then we skip testing altogether. That’s the basic difference between BDDs and ZDDs. And while ZDD theory is much like the BDD theory, it differs sufficiently in the sense of requiring a different “mind-set”.

The *pre-fascicle 1B* that discusses BDDs and ZDDs is available from Knuth’s News page (direct link). Interested readers can download it (but Knuth cautions against printing it out just yet, since he’ll be putting up an updated version in a couple of weeks). That pre-fascicle is a 140-odd page booklet with more than 260 exciting exercises! It will be published as part of fascicle 1 (Knuth said it’ll go to printing around the end of January 2009); fascicle 1 would in turn form part of Volume 4A (along with the already published fascicles 0, 2, 3 and 4).

*Update (May 2009): Volume 4, Fascicle 1, is here!*The Fascicle 1 of Volume 4 of The Art of Computer Programming is now in print. In addition to the material on BDD and ZDD techniques (described in this post), it includes a subsection on bitwise tricks and techniques, including broadword computing (formerly known as “Platologic computing”). In Knuth’s own words, it is *“cram-packed with lots of new stuff among nearly 500 exercises and answers to exercises.”*

There’s also a bundle of Volume 4, Fascicles 0-4 available at a discounted price.

*Get your copies today!*

#### Families of subsets and their ZDD representation

BDD is a data structure for (representing, manipulating, working with) boolean functions. ZDD, on the other hand, is a data-structure for what Knuth calls *Families of Sets* — a large number of combinatorial problems are based on studying families of sets (which are just Hypergraphs). Families of sets are another way of looking at boolean functions since there’s a natural one-to-one correspondence between the two — the *solutions of a boolean function* (the set of n-tuples of boolean values such that is TRUE) correspond to family of subsets, with each solution corresponding to a subset where belong to the subset iff [see TAOCP Exercise 7.1.4 -- 203(b)]. But even though the families of sets can be encoded as boolean functions, it is sometimes better to think of them directly as families of sets, rather than in boolean sense. This is the thinking cap we’ll put on when working with ZDDs.

Families of subsets are taken from a well-ordered universe . We use the following rules to represent those using ZDDs [again, see TAOCP Exercise 7.1.4 -- 203]:

*The Empty Family*, , is represented by (the FALSE sink node).*The Unit Family*(the set comprising just the empty set), , is represented by (the TRUE sink node).- If is any other family (besides empty and unit), we know that it is nonempty, and that at least one of its members is in turn nonempty. In that case, let be the
*least*element of universe that*supports*— that appears in at least one set in . [see answer to TAOCP Exercise 7.1.4 -- 197 for the definition of supports]. Then define sub-families and of with and . These sub-families correspond to those sets that don’t and do contain . Then, inside the computer, the family corresponds to a*zead*(ZDD-node) labeled with`LO`and`HI`branches going to the ZDD representations of and respectively. Notice the recursive definition (see Knuth’s diagram below).

Throughout the talk, Knuth took up various problems, and worked them out in real-time for everyone to follow, rather than putting up a bunch of slides with everything already worked out. This is one of the beauties of his musing lectures — it’s interactive. While Knuth works his way through the problems, the audience can work with him or with their own pencil and paper. And it’s even more convenient when watching the recorded session over net, where one can pause, work the problem out, and then play to see if they got it right, and go back etc.). Knuth also joked about how he sometimes had to sit through presentations where the speaker doing PowerPoint would whiz through the slides a bit too fast. So, anyway, the most instructive (and most enjoyable) way to understand these examples, I think, would be to just watch the video and work them out yourself (possibly after going through the relevant material in the pre-fascicle).

##### Example 1. ZDD of a function

Here, Knuth took the example of family = the family of 2-element subsets of , that is, and explained how to construct its ZDD (see diagram above). The family is , so its least support is . The and families turn out to be and respectively. So we have `LO` and `HI` branches (dotted and solid lines in the diagram above) going from to the ZDD of and that we recursively construct. Notice that no node with the same (`V`, `LO`, `HI`) triple appears twice in our data structure (that is, the diagram is *reduced*). So although Knuth is giving a Christmas “Tree” lecture, a *ZDD is not a true tree* because overlapping subtrees are combined into one — rather, a ZDD is a DAG (directed acyclec graph).

##### Example 2. ZDD-base and its computer representation

When working on real problems, there’re usually many functions we’re working with simultaneously. So instead of a ZDD representing a single function, we really have a *ZDD-base* where common zeads are shared across functions. Knuth constructed the ZDD-base of the two functions and . Inside a computer, a ZDD is represented as a linked structure, where each node of the ZDD, zead, is represented as a record with 3 fields: `V` (Variable ID#), `LO` (Low Pointer), `HI` (High Pointer). Sink nodes have their `V` field set to , with `LO` and `HI` fields pointing back to them (see diagrams above).

ZDD-bases satisfy certain laws:

- No two nodes have the same triple (
`V`,`LO`,`HI`) — that is, the structure is*reduced*. - No node has
`HI`= 0, except sink — this is the reason Minato called it*zero-suppressed*— the idea is to “skip over” such nodes - For non-sinks f,
`V`(`LO`(f)) >`V`(f) and`V`(`HI`(f)) >`V`(f) when f > 1 — that is, the nodes are*ordered*with`V`-fields are always increasing as we walk down the structure

ZDD-bases, which though conceptually easy to understand, are non-trivial to implement efficiently because behind the scenes, lots of things have to be taken care of:

- New nodes are born and old ones die — nodes have to be efficiently
*allocated*,*ref-counted*and*garbage-collected.* - The UNIQUE-table (hash-table for (
`V`,`LO`,`HI`) triple) has to be efficiently maintained and kept fresh with any changes made to the ZDD. A*memo-cache*has to be similarly maintained for caching recently computed results (like`AND`/`OR`of function).

Knuth’s BDD15 (literate) program, available from his Downloadable Programs page should provide an instructive study of the issues and techniques involved. I gained a lot of clarity on tricky BDD topics like ref-counting nodes, reordering of variables, efficient function composition etc, by studying Knuth’s BDD14 program. I used it as a reference whenever I got stuck anywhere with my own BDD implementation. BDD15 should be equally useful, and I plan to find time to study it soon.

##### Example 3. Nested parentheses

Nested parentheses are a natural way to describe tree structures (LISP being a good example). In this example, Knuth showed how to represent the 5 ways to correctly nest 3 sets of parentheses using a ZDD:

Parenthesis Nesting | ZDD subset |
---|---|

( ( ( ) ) ) | |

( ( ) ( ) ) | |

( ( ) ) ( ) | |

( ) ( ( ) ) | |

( ) ( ) ( ) |

In this representation, subscripts correspond to positions in the string with / denoting the kind of parenthesis. The ZDD for this represents a family of 5 sets (each containing 6 elements). In the ZDD, the variable ordering used is (see the diagram above).

For parentheses pairs, the ZDD contains 14 nodes and don’t appear to save a whole lot. However, if we consider parentheses pairs, the ZDD of 602 nodes represents 1.3 trillion solutions! This compact ZDD structure encodes all those solutions without having to store them explicitly; and yet we can ask questions about the solutions. Like, say, analyze solutions that contain . The figure 1.3 trillion comes because the number of ways to nest pairs of parentheses in a balanced way is the Catalan number, . See TAOCP Exercises 2.2.1 — 2, where a proof of this fact is given using a “reflection principle”; and also Eq. 2.3.4.4 — (14), which deals with enumeration of binary tree.

So, using only a small space, ZDDs can represent large families of combinatorial sets of interest, allowing efficient operations on these families and helping solve problems related to them. For example, if a family of sets is represented as a ZDD, one can quickly (in terms of the size of the ZDD) find a random set of the family.

##### Operations on families and Family Algebra

*[This is actually Ex. 7.1.4 -- 203, 204.]* People from different branches of combinatorics have different operations they want to perform on families of sets. The usual ones are:

- Union:
- Intersect:
- Difference:
- Symmetric Difference:

All four operations can be efficiently implemented given the ZDDs of and using an easy recursive process based on the smallest support for the two functions. The implementation can be sped up using a *memo-cache*.

In addition to the ones above, more operations are possible: (meet), (join), (delta), (quotient), (remainder). Knuth said he had recently discovered another binary operation that he thinks might prove useful. He likes to call it *disjoin* since it is similar to the join operation, but works on disjoint sets:

The is not what Knuth uses as the symbol for disjoin (for the correct symbol, see his hand-written version in the snapshot above), but I haven’t yet figured out the name for that symbol.

Even more operations on families are possible (see Exercise 7.1.4 — 236): (closure), (maximal elements), (minimal elements), (nonsubsets), (nonsupersets), (cross elements). Seeing all these funny symbols, somebody chimed in an obligatory APL joke; somebody else mentioned “family values” in connection with family algebra. People were certainly having fun!

Towards the end, Knuth mentioned various applications of the ideas he had spoken of earlier.

##### Application 1. Exact Cover Problems

Exact Cover Problems essentially involves selecting rows from a given boolean matrix in such a way that each column among the selected rows has exactly one 1. A popular example of this kind of problem is Sudoku. Knuth, in an earlier musing, mentioned an efficient dancing links algorithm for solving such problems.

In the present musing, Knuth showed the examples of covering an 8×8-chessboard with 32 dominoes [7.1.4 -- displays (127) to (129)]; covering with monominoes/dominoes/trominoes [display 7.1.4 -- 130]; or covering with dominoes of three colors (red, white, blue) with no adjacent dominoes of the same color [Exercise 7.1.4 -- 216].

##### Application 2. Dictionaries

Knuth showed how, with 5*26 = 130 variables, it is possible to store all five-letter words of the SGB (Stanford GraphBase) in a ZDD, and perform operations such as:

- Querying for words that match the pattern
`t?u?h`. This involves using family algebra on ZDDs to compute where is the “pattern” . - Finding all words such that when the letter
`b`in it is changed to the letter`o`, we still get a valid five-letter word. This involves computing , which gives words like`busts`(),`herbs`() etc.

##### Application 3. Graph problems

Knuth illustrated application of ZDDs to graph problems by showing how it can be used to find various constrained arrangement of queens on a chessboard (queens-graph): kernels, maximal cliques, dominating sets etc. [See Exercise 7.1.4 -- 241]. All these operations can be implemented using ZDDs and family algebra operations (see Knuth’s sheet). Letting to be the family of edges of a graph/hypergraph (such that members of are subsets of the sets of vertices ):

*Independent Sets*are (Knuth uses Weierstrass P to denote the power set )*Maximal Independent Sets*(or*Kernels*) are the maximal elements of the independent sets*Dominating Sets*are where*Minimal Dominating Set*is*Maximal Induced Bipartite Subgraphs*is

Knuth had so much interesting stuff to talk about. But he had spoken for 90 minutes already — so he decided to stop.

I’ve been studying `fasc1b` for about three months now and have been enjoying myself thoroughly — learning new data-structures & proof techniques, developing my own BDD-base, working out the programming exercises, playing with Conway’s game of Life, coloring graphs and more. I’m still only two-thirds through with the exercises (that total to a respectable 263) — just about to start working on the ZDD exercises. I hope to be able to work my way through the exercises by the end of this year.

Pingback: Ashutosh Mehra’s Blog » Solving Project Euler Problems 161 (Trominoes Tiling) and 185 (Number Mind) with ZDDs

Pingback: Data Structures: What are some great non-standard (not widely used) data structures? - Quora