Discrete

A walk that changes one thing at a time

Counting from nothing to fifteen in binary changes four digits at once somewhere in the middle. There is another order through the same sixteen words in which every step changes exactly one — and it is a closed walk on a four-dimensional cube.

Worth reading first: Seven bridges, and the invention of throwing things away · A formula is a corner of a cube.

Count from nothing to fifteen in binary. Somewhere in the middle, 0111 becomes 1000 and every one of the four digits changes at once. That is a nuisance for anything that reads the digits by physical means — a disc with contacts on it, a set of switches, a row of sensors — because the four will not change at exactly the same instant, and for a moment the reading is a word that was never intended.

The repair is to count in a different order.

A closed walk on the 3-cube changing one place at a timeThe corners of a 3-dimensional cube with a path through every one of them exactly once, each step moving along an edge, and the last corner one step from the first.000000110112010311041115101610078 corners, 8 steps, and every step changes one placethe walk closes: 100 is one change from 000
Fig. 1 The eight words of three places, drawn as the corners of a cube, with a closed walk through all eight. Each step moves along an edge, which means it changes exactly one place, and the eighth step returns to the start.

The order in that figure visits every three-place word once and changes one place at every step, including the step from the last back to the first. The picture is why: the words are the corners of a cube, two corners are joined when their words differ in one place, and the walk is a closed tour along edges.

The cube, and what its edges mean

The words of n places are the corners of an n-dimensional cube. That is not a metaphor: a word is a list of n coordinates each equal to 0 or 1, which is exactly what a corner of a cube is.

Two words differing in one place are joined by an edge, because an edge of a cube is what changes one coordinate and leaves the rest alone. The number of places two words differ in is the Hamming distance between them, and it is the length of the shortest route along edges from one corner to the other.

So “change one place per step” and “move along an edge” are the same instruction, and the problem becomes: find a closed walk visiting every corner exactly once. That is a Hamiltonian cycle, and asking for one is a question about a graph rather than about numbers.

A code on the 3-cube, and the balls around its wordsThe corners of a hypercube with the chosen codewords marked and the words within one error of each shaded.0000010100111001011101112 codewords in the 3-cube, minimum distance 3 — 1 error corrected, 2 detectedthe 2 balls of radius 1 hold 4 words each and cover all 8 exactly once: the code is perfect
Fig. 2 The same cube, asked a different question. Error-correcting codes want words far apart on this graph, so that a single change cannot reach one from another; a Gray code wants an order in which every consecutive pair is as close as possible.

The site has met this cube before, in a field that wants the opposite thing from it. Error-correcting codes choose corners far apart so that a single flip cannot reach one from another; a Gray code chooses an order in which every consecutive pair is one flip apart. Same graph, opposite objectives.

Building one by reflecting

Counting to 15 two waysA table of the 16 words of 4 places in ordinary counting order beside the reflected code, with the places that change at each step marked.counting upthe code000000000100010001200100011300110010401000110501010111601100101701110100810001100910011101101010111111101111101211001010131101101114111010011511111000counting up changes 26 places over the 15 steps; the code changes 15, one per stepthe worst ordinary step changes all 4 at once, which is where a mechanical reading of it goeswrong
Fig. 3 Sixteen words in ordinary counting order and in the reflected code, with the places that change at each step marked. Counting up changes twenty-six places over fifteen steps; the code changes fifteen, one per step.

The code has a construction that takes one line to state. To get the order for n places: write the order for n − 1 places, then write it again reversed, put a 0 in front of every word in the first half and a 1 in front of every word in the second.

Every step inside a half changes one place, because the shorter order did. The step between the halves changes one place, because the two words either side of the join are the same word with different leading digits. And the step from the very last word back to the very first changes one place, because the last word of the reversed half is the first word of the original half with a 1 in front.

That is a recursive construction with three cases to check and it is checked: the figure walks the order it produces and asserts, step by step, that exactly one place changes, including the closing step.

The comparison in the same figure is the reason to bother. Ordinary counting to fifteen changes twenty-six places over its fifteen steps, and its worst single step changes all four.

Why any such order has to exist

The construction proves existence by producing one, which is the best kind of proof and also the kind that explains least. A separate argument says why the cube was always going to cooperate.

Colour each corner by the parity of its word — how many 1s it contains, even or odd. Every edge of the cube joins an even corner to an odd one, because moving along an edge changes exactly one place and so flips the parity. A graph with that property is called bipartite, and any closed walk in one alternates between the two sides.

So a closed walk visiting every corner needs equally many of each parity, and the cube has exactly that: half its corners have an even number of 1s and half have an odd number. The condition is satisfied, which does not by itself produce a walk, but it does mean the obvious obstruction is absent.

The same test is what kills the question elsewhere. A cube with one corner removed has seven corners, four of one parity and three of the other, and no closed walk through all seven can exist — the alternation would have to fail somewhere. That is a complete proof of impossibility in one sentence, and it is the sort of argument parity is for: not a search, but a quantity that the desired object would have to preserve and cannot.

How many such walks there are

All 6 closed walks on the 3-cubeEvery way of visiting each corner of the 3-cube once and returning to the start, one step along an edge at a time, drawn side by side.walk 1walk 2walk 3walk 4walk 5walk 66 walks in all, found by trying every order and keeping the ones that only ever step along an edgeeach is drawn once rather than twice: a walk and the same walk backwards are the same set of steps
Fig. 4 Every closed walk on the 3-cube that visits each corner once, found by trying all orders and keeping the ones that only ever step along an edge. There are six, drawn as sets of edges rather than as directed tours.

The reflected construction gives one such walk. It is natural to ask how many there are, and the honest answer is a search: try every order of the corners, keep the ones in which consecutive corners are joined and the last is joined to the first.

For the three-place cube the answer is six. The search finds twelve — each walk once in each direction — and the figure halves it, because a walk and the same walk backwards use the same set of edges and are the same tour.

6 of the 1,344 closed walks on the 4-cube6 of the 1,344 ways of visiting each corner of the 4-cube once and returning to the start, one step along an edge at a time, drawn side by side.walk 1walk 2walk 3walk 4walk 5walk 61344 walks in all, found by trying every order and keeping the ones that only ever step along anedgeeach is drawn once rather than twice: a walk and the same walk backwards are the same set ofsteps
Fig. 5 The same search on the 4-cube finds 1,344, of which six are drawn. The drawing is a sample; the count is exhaustive.

For four places there are 1,344, and the same search finds them in a few milliseconds. For five the answer is 906,545,760, and the search that found the first two numbers would still finish, though not on a page. For six nobody knows the exact count.

That escalation is worth pausing on. The construction gives one walk for every n, effortlessly. Counting all of them stops being possible almost immediately. Producing an object and counting the objects are unrelated difficulties, and this is one of the cleanest examples: the recursive construction has no idea how many siblings its output has.

Reading the code, and unreading it

There is a small piece of arithmetic that makes the code usable rather than merely existent, and it is worth having because it is two lines and because it shows the reflected construction was not the only way in.

The kth word of the code is k with each place replaced by the exclusive-or of itself and the place above it. Going the other way — from a code word back to its position — each place is the exclusive-or of all the code’s places from the top down to it.

Both are immediate consequences of the reflection. Doubling the position range prefixes a 0 to the first half and a 1 to the reversed second half, and reversing a range in binary is complementing it, and complementing every place below the leading one is what an exclusive-or with the shifted value does.

The point is not the formula. It is that a construction defined by a recursion has a closed form, so the kth word can be got in a couple of operations without generating the first k − 1. Whether such a form exists is not predictable from the recursion, and here it does; the Catalan numbers are a case where a recursion also collapses into a formula, and plenty of others are cases where nothing collapses at all.

Every edge, or every corner

The neighbouring question — a closed walk using every edge exactly once — has a completely different character, and the difference is one of the oldest results in the subject.

The bridges of Königsberg settled the edge version: such a walk exists exactly when every vertex has an even number of edges, and the test takes one pass over the graph. On the cube each corner has n edges, so an every-edge walk exists precisely when n is even.

Arriving and leaving come in pairsA stop in the middle of a walk pairs each arrival with a departure, so it needs an even number of edges; an odd count can only be a start or a finish.inoutinout4even degreeevery arrival has a departureinoutinoutin5odd degreeone edge left unpaired
Fig. 6 Why the edge version is easy: a stop in the middle of a walk pairs each arrival with a departure, so it needs an even number of edges. Counting degrees settles the question for any graph whatever, in one pass.

The every-corner version has no such test. Deciding whether an arbitrary graph has a Hamiltonian cycle is one of the standard hard problems, with no known method essentially faster than trying orders, and the contrast with the edge version is the reason it is famous. Two questions about a graph, phrased almost identically, one settled by counting degrees and the other apparently requiring search.

For the cube specifically the question is settled — the reflected construction answers it for every n — but that is a fact about cubes and not a method for graphs.

What the search actually did

The counts above deserve a word about method, because “try every order” is not what the search does and the difference is the reason it finishes.

Trying every order of sixteen corners means twenty thousand billion orders, which is not a few milliseconds. What the search does instead is grow a path one corner at a time, and at each step consider only corners joined by an edge to the current end. Most partial orders die within a few steps, and the ones that survive are already nearly walks.

That is depth-first search with the constraint applied as early as possible, and the saving is enormous: a factor of roughly a thousand million on the 4-cube. It is still exponential, and it is still the reason five is reachable and six is not.

The figure also takes care of a detail that would otherwise double every count. A walk and the same walk reversed use the same edges, so the search keeps a tour only when the corner after the start is smaller than the corner before it — a canonical form, chosen once, that admits exactly one member of each pair. Getting that wrong would have reported twelve walks on the 3-cube, and twelve is a plausible enough number that nothing else would have looked odd.

Where the same order turns up

(p ∧ q) ∨ (¬p ∧ r) on the 2-by-4 mapA grid of the assignments arranged so that neighbouring squares differ in one variable.qrp000111100101100011(p ∧ q) ∨ (¬p ∧ r) on the map — 4 of 8 squares truesquares that touch, edges included, differ in exactly onevariable
Fig. 7 A Karnaugh map: the eight assignments of three variables laid out so that squares which touch differ in exactly one variable. The row and column labels are a Gray code, and that is the whole reason the map works.

The order appears wherever adjacency in one place has to mean adjacency on the page. The map that puts neighbours side by side is a rectangle of truth values whose rows and columns are labelled 00, 01, 11, 10 rather than 00, 01, 10, 11 — a Gray code in two places — and the labelling is not a stylistic choice. It is what makes touching squares differ in exactly one variable, which is what makes a block of touching true squares correspond to a term with variables left out.

The same order is what puts the cube of assignments on a page without tearing it, and it is why a truth table of a formula in four variables has a two-dimensional layout at all.

Why the ordinary order is as bad as it is

Twenty-six changes over fifteen steps sounds like a modest overhead against the code’s fifteen. The average is not the problem; the worst step is.

In ordinary counting, the step from a word of all 1s in its lower places to the next changes every one of those places at once. Over the whole range from nothing to 2ⁿ − 1 there is exactly one step that changes all n, two that change n − 1, four that change n − 2, and so on — which is where the total of 2ⁿ⁺¹ − 2 changes comes from, and why the average per step approaches two however large n gets.

An average of two and a worst case of n is precisely the profile that makes something safe on paper and unsafe in a device. Anything reading the digits at an arbitrary moment sees a word that is a mixture of the old and the new, and for a step that changes one digit the mixture is one of the two intended words. For a step that changes four, the mixture can be any of sixteen, most of which are nowhere near either.

The code removes the worst case entirely by accepting no improvement at all in the average — fifteen changes against twenty-six is a saving, but the code would be worth using if the total were higher. What is being optimised is the maximum, and that is a different objective from the one a total measures.

What the picture cannot show

The drawings are of a cube in three dimensions and a cube in four drawn as two cubes side by side. That second drawing is a graph rather than a picture: the pairs of corners joined across the gap really are adjacent, and no arrangement on a page can make them look it. Past four places even that stops working, and the figures stop.

Only six of the 1,344 walks are drawn, and the caption says so. The count is exhaustive and the picture is a sample; conflating the two would be exactly the kind of quiet overstatement this site’s figures exist to avoid.

The 4-cube figure is a drawing of a graph and not of a shape. Nothing in it is a projection of anything: two cubes are drawn side by side with corresponding corners joined, and the joining lines cross the gap because that is where they fit. A reader who takes the picture as a perspective view of a four-dimensional object will draw wrong conclusions about which corners are near which, and the only reliable reading is the combinatorial one — two corners are adjacent when their words differ in one place, whatever the drawing does.

And the practical problem that motivated all this is not shown at all. Nothing here draws a disc with contacts, a shaft encoder or a switch, because those are physical objects and this site’s figures are generated from stated rules. What can be shown is the combinatorial fact, and the physical reading of it is a sentence rather than a picture.

Where the ladder goes next

Three directions lead out.

The counting question is one of them, and it is open: the number of Hamiltonian cycles on the n-cube is known for n up to five and not for six, which is an unusually low ceiling for a quantity this easy to define.

The existence question in general is the other, and it is the hard one. There is no known test for a Hamiltonian cycle that avoids search, and there are good reasons to believe none exists — a belief with an enormous amount riding on it, which belongs next door with the cost of computation rather than here.

And there is the question of what other orders exist. A Gray code is any order in which consecutive words differ in one place, and the reflected one is merely the most convenient; some applications want a code that is also balanced, so that each place changes about equally often, and others want one that is monotone. Each requirement carves a subset out of the 1,344, and for larger cubes the existence of a code with a given extra property is often unknown. The cube is a very small object with a great many questions still attached to it.

Shares its objects with

Essays that name at least two of the same things, and that neither author linked.

Named objects

A dashed tag is an object no other essay names yet.

BinaryComplete graphCounting argumentGraphGray codeHamming distanceHypercubeKarnaugh mapParityRecursion