In 1976, Robert Cass Keller proposed a puzzle in Word Ways that was inspired by Benjamin Franklin’s quote “We must all hang together, or assuredly we shall all hang separately”. The puzzle asks you to fit the first 33 distinct surnames of American Presidents into a rectangle of minimum area using the following rules:

  • Names can only be placed left-to-right or top-to-bottom
  • Letters in adjacent cells must belong to a common name
  • Every name must be connected by some path of intersecting names to every other name (“We must all hang together…”)

Keller mentions a similar contest in a British magazine involving the 50 U.S. states. The winner apparently fit them all into a 23-by-29 rectangle.

In the next issue, Sam Harlan presented the first solution to Keller’s puzzle, a 17-by-31 rectangle with total area 527:

W A S H I N G T O N M
I O A C
L O R J A C K S O N P O L K
S V F I L L M O R E E I I
O E I I F N I X O N E N
N R E N T A F T N R L
L C E E J C E
F O R D O M R D A M O N R O E Y
H L A S T Y L E R H O
V A N B U R E N D O T N O C
R U I N H A Y E S S O
D C S U O E O
I H A R R I S O N T A Y L O R N V L
N A N D E I
G R A N T A C L E V E L A N D
A T R U M A N T G
E I S E N H O W E R S E

1976 was an election year, which meant the set of presidential surnames was about to increase by one. Harlan mentioned that Jimmy Carter (the eventual winner) could easily be packed into the existing solution using the first “e” in Pierce.

In 1993, shortly after Bill Clinton’s election, Darryl Francis produced a new rectangle that was smaller than Harlan’s solution by two columns (17-by-29, for a total area of 493) and included the entire current set of 37 presidential surnames including Carter, Reagan, Bush, and Clinton:

W A S H I N G T O N B U S H P
I A C O N I X O N
L R C O J A C K S O N E
S F I L L M O R E E V R
O I E L F N E M C K I N L E Y
N T E V I F N R E I
A L E D E E J S
F O R D L G R D A M O N R O E
H T A E S T Y L E R T H O N
V A N B U R E N O T A N O H
R U D L I N C O L N H A Y E S S O
D C T P U L O E W
I H A R R I S O N C A R T E R O N V E
N A U L D R E R
G R A N T M K R E A G A N L
A A M T
C L I N T O N M A D I S O N

In next issue of Word Ways, Lee Sallows presented a 19-by-22 grid containing the same set of 37 surnames. The resulting area (418) was a massive reduction from previous attempts, but his grid contained an isolated Ford-Polk-Jackson component. Sallows noted that some “purists” might therefore disqualify his rectangle.

Such a purist showed up in the very next issue, when Leonard Gordon presented a completely connected 19-by-22 grid as well as the following new record-setting 17-by-24 grid with area 408:

H A R D I N G F I L L M O R E E
A I T O O O H J I
G R A N T X Y R J N O A D A M S
R C O O L I D G E R S Y C E
L I N C O L N E F O E E K N
S E R B F E V S S H
P O L K V A N B U R E N E O O
N E S R C L I N T O N W
G W I L S O N H S T E
A T A B O A R T H U R
R E A G A N T R U M A N K
F F D C E C P H
I T W A S H I N G T O N M A D I S O N
E A N R E O
L M C K I N L E Y E T R V
D A D E C E
J O H N S O N T A Y L O R E R

Gordon published an update a year later, presenting a tighter 18-by-22 solution with an area of 396:

H A R D I N G F I L L M O R E R
A I T O O O
G R A N T X Y R J N O
R C O O L I D G E R A S
L I N C O L N E F O P I E R C E
S E R F E T V
P O L K V A N B U R E N B U S H E
N E R U L
G W I L S O N E I S E N H O W E R T
A J A B O
R E A G A N T R U M A N A D A M S C
F C D C K C L
I K H O O V E R C K I
E S H A N M A D I S O N
L O W A S H I N G T O N R N T
D N Y A A E T L O
E N F D E E N
J O H N S O N T A Y L O R Y

Gordon’s rectangle above from 1994 is the last communication I can find on the Presidential Rectangle for a while, even though thanks to the election of George W. Bush in 2000, the set of presidential surnames didn’t change again until 2008.

The Presidential Rectangle shows up as an exercise in Don Knuth’s Art of Computer Programming Vol. 4, Fascicle 5, although the exercise asks for a square rather than a rectangle, and the exercise asks for the first 38 surnames up to and including Obama.

The 20-by-20 solution (area: 400) is credited to Gary McDonald from 2017:

W I L S O N T A F T P
I A O C J O H N S O N
X Y R L E L
T H C O O L I D G E F A K
R E A G A N O V F R
U R R R O O S E V E L T
M R T B B L R H A Y E S
A I E U A D A M S U I
N S R C M N O R S
O H H A R D I N G E
N A O G R A N T
F M O N R O E J H
I A V W A S H I N G T O N
L M C K I N L E Y C A W
L L R K P I E R C E
M A D I S O N B U S H F R
O N O I
R T Y L E R V A N B U R E N
E O L
L I N C O L N K E N N E D Y

McDonald’s solution contains some elements from Gordon’s earlier patterns, including an updated 4-way intersection including Coolidge and Nixon. But I think the dense Roosevelt-Adams-Harding-Obama-Cleveland-Jefferson block is really the centerpiece of the packing.

Knuth mentions that a 19-by-19 solution is surely impossible, and after many attempts over the past few weeks, I am also doubtful that a 19-by-19 packing exists. I was, however, able to pack all of the first 38 surnames up to Obama in a 19-by-21 grid, which beats McDonald’s 20-by-20 solution by exactly 1 when scoring by total area.

Here’s my 19-by-21:

B H A R R I S O N T A F T F T
U B I M O N R O E
C A R T E R A P O L K R U
H M K L A D A M S
A B U S H H A Y E S M A
N A N J O H N S O N
A R T H U R N R N
N A D C J E F F E R S O N I
Y I L D X
W I L S O N E T Y L E R V O
A O G V I R E A G A N
S R C E M S O N C
H O L C E O M L
I O J A C K S O N S B A I
N G L N I H E U D N
G A R F I E L D N H O O V E R I T
T A D L W E E S O
O N G P I E R C E L I N C O L N
N T E Y R T N

I was also able to construct a similar 21-by-21 packing of all 40 presidential surnames as of 2021, including Trump and Biden:

F O R D B H T
B T R U M A N A Y F
B C A R T E R S R L I
U T M U H M O N R O E L
C A D A M S M I R L
H F P O L K S M
A T H E J O H N S O N
N A B I D E N N I R
A R T H U R N X E
N A D C J E F F E R S O N
Y I L D N
W I L S O N E H A Y E S V
A O G V I R E A G A N
S R C E M S O N C
H O L C E O M L
I O J A C K S O N S B A I
N G L N I H E U D N
G A R F I E L D N H O O V E R I T
T A D L W E E S O
O N G P I E R C E L I N C O L N
N T E Y R T N

Word Ways has suspended publication as of a year ago, otherwise I’d continue the tradition by posting these constructions there. In the rest of this post, I’ll explain how I found these new rectangles and share some tools that you can use to construct similar word rectangles.

SAT Encoding

You can frame the Presidential Rectangle problem as a set cover problem and use solvers based on the Dancing Links technique to identify solutions but the connectedness property is difficult to specify in that setting. Knuth describes a way to modify a solver to only discover connected solutions instead of filtering out disconnected solutions as a post-processing step. But SAT solvers are usually much more efficient at finding a solution with multiple constraints like this, and I like playing around with complicated SAT encodings, so I wanted to give that approach a go.

My encoding creates a boolean variable C(ch,r,c) for every possible character ch and row r, column c pair in the rectangle. These variables represent “the cell at (r, c) in the rectangle contains character ch”. I also add clauses that restrict at most one C(ch,r,c) to be true for any (r, c) pair.

Now I can go through all surnames that I want to pack in the rectangle and create variables PH(x,r,c) representing “surname x is placed horizontally starting at (r,c)” and PV(x,r,c) respresenting “x is placed vertically starting at (r,c)”. I create clauses connecting the PH(x,r,c) and PV(x,r,c) to the C(ch,r,c) in the natural way, spelling out the exact placements of characters in surname x into cells in the rectangle if PH(x,r,c) or PV(x,r,c) is true. Finally, I add clauses that ensure that exactly one PH(x,r,c) or PV(x,r,c) is true for any surname x.

At this point, any satisfying assignment of the variables must pack all of the surnames into the rectangle and ensure that each cell in the rectangle is assigned at most one distinct character. But recall the rules we started with:

  1. Names can only be placed left-to-right or top-to-bottom
  2. Letters in adjacent cells must belong to a common name
  3. Every name must be connected by some path of intersecting names to every other name

Without additional contraints, we could still violate rule 2 above by placing surnames too close together in the rectangle.

To encode the spacing constraints of rule 2, I introduced three more families of variables:

  • S(r,c): “spacer” variables.
  • H(r,c): “horizontal cell” marker variables.
  • V(r,c): “vertical cell” marker variables.

When a PH(x,r,c) or PV(x,r,c) is set, we set spacer variables in the two cells before and after the placement of the surname. There may be no cell that comes before or after a surname if it’s placed on the border of the rectangle, and that’s fine, we just omit spacers when that happens. We also set H(r,c) on all cells covered by the characters in a surname when PH(x,r,c) is set and V(r,c) on all cells covered by the characters in a surname when PV(x,r,c) is set.

With that setup, we add some more clauses to guarantee that:

  • A spacer and a horizontal cell never co-occur
  • A spacer and a vertical cell never co-occur
  • Two horizontal cells can’t be vertically adjacent unless both are also vertical cells
  • Two vertical cells can’t be horizontally adjacent unless both are also horizontal cells.

At this point, there are already a lot of variables involved, so here’s an example that might help make sense of the encoding so far: If POLK is placed horizontally in the cells (1,1), (1,2), (1,3), (1,4), then PH(POLK,1,1) is set and no other PH(POLK,r,c) or PV(POLK,r,c) can be set. This also means that C(P,1,1), C(O,1,2), C(L,1,3), and C(K,1,4) get set and no other C(ch,1,1), C(ch,1,2), C(ch,1,3), or C(ch,1,4) can be set. Also, S(1,0) and S(1,5) get set, which keeps any other surnames from using those cells. And H(1,1), H(1,2), H(1,3), and H(1,4) get set, which keeps horizontal surnames from occurring adjacent to POLK in row 0 or row 2 unless the cells are also vertical cells (to see why we make this exception, look at, for example, the Reagan-Carter-Nixon-Coolidge intersection in Gary McDonald’s solution above).

So we’ve encoded rules 1 and 2, and we’re left with rule 3: “Every name must be connected by some path of intersecting names to every other name”. This is a little tedious but we can do it. Define variables R(x,y,i) for every pair of surnames x and y and each i from 0 to N-2, where N is the total number of surnames. R(x,y,i) is true exactly when x is reachable from y through a path consisting of exactly i intermediate surnames. You can define it inductively as follows:

  • R(x,y,0) is true if any of the placements of x and y that intersect are selected, or false if there are no such placements.
  • R(x,y,i) for i > 0 is true if there’s some z such that R(x,z,0) and R(z,y,i-1) are both true.

Finally, we generate a set of variables R(x,y) for each pair of surnames x, y and define each as the disjunction of R(x,y,i) for i from 0 to N-2, so that R(x,y) is true exactly when there’s a path of any length from x to y. Then we add unit clauses for each R(x,y) to the encoding to ensure everything is connected to everything else.

Now we can just run a SAT solver on the formula. If it returns a satisfying assignment, we can extract the assignments of the PH(x,r,c) and PV(x,r,c) for each surname x to reconstruct the solution.

Assisted solutions

Unfortunately, the Presidential Rectangle is a bit too difficult even for modern SAT solvers. You can, for example, find a packing of a dozen or so surnames in, say, a 12-by-12 square or prove that no such packing exists for an 11-by-11 square in a few minutes. But packing even the first 38 presidential surnames into a 20-by-20 rectangle or proving a 19-by-19 packing seems beyond the reach of today’s technology: I ran multiple state-of-the-art solvers for a week each on problems like these with no results.

Since I care more about finding packings than proving they don’t exist, I started to look for ways to make the problem easier by targeting packings with additional constraints instead of solving the general problem.

Along these lines, I played around with:

  • Forces: Some of the advances in the Presidential Rectangle came from re-using partial packings that were successful in earlier attempts (Lee Sallows re-used Darryl Francis’s Van Buren-Harding-Grant-Buchanan loop and Leonard Gordon’s solutions share many common constructs, for example). It makes sense that you might want to force absolute positions for some of the surnames and let the solver figure out the rest.

  • Relative Forces: Similar to forces, these allow you to specify relative positions instead of absolute posititions in the form of constraints that say things like “I’d like Harding and Harrison to intersect in their first ‘H’”.

  • Partial Packings: Allow the solver to pack a subset of a particular size into a sub-rectangle. Partial packings are easy for the solver to figure out, and they could be used incrementally with forces to figure out a full packing.

  • Minimizing Blank Cells: A constraint that guarantees at most N blank cells in a packing helps create denser partial packings. Without adding something like this, if you ask a solver for a partial packing of any 10 surnames into a 10-by-10 grid you’re likely to just get the 10 shortest names.

I had some promising partial successes with each of these techniques. But the only thing that really worked for me (and what gave me the 19-by-21 and 21-by-21 rectangles above) was to hand-pack some of the trickier surnames and use those as absolute forces, letting the solver figure out the rest. Both of my solutions above share the same lower half which I hand-crafted and then fed into the solver with forces. That lower half helped the solver find both solutions in just a few hours each.

Tools

I’ve put the tools I used to generate the rectangles above up at github.com/aaw/presidential-rectangle. They support all of the advanced features for assisted solutions described in the previous section. Have fun creating your own rectangles!