Upload
others
View
5
Download
0
Embed Size (px)
Citation preview
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 1
Enumerative CombinatoricAlgorithms
Gray code
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 2
Binary codeStandard binary code:
Example with 3 bit:
000b = 0001b = 1010b = 2011b = 3100b = 4101b = 5110b = 6111b = 7
Problem:In the transition from 3 to 4 every bitchanges.
Thus, if timing is not absolutely perfect (itnever is) then any number from 0 to 7 canbe read.
e.g.: position encoder, electronic circuits, ...
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 3 - 1
Gray code• a.k.a. reflected binary code (Frank Gray, 1947)
• binary Gray code: successive values differ in only one bit
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 3 - 2
Gray code• a.k.a. reflected binary code (Frank Gray, 1947)
• binary Gray code: successive values differ in only one bit
generate a Gray code (G(n)) for n bits from G(n−1):G(n)
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 3 - 3
Gray code• a.k.a. reflected binary code (Frank Gray, 1947)
• binary Gray code: successive values differ in only one bit
generate a Gray code (G(n)) for n bits from G(n−1):
G(n−1)
G(n)
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 3 - 4
Gray code• a.k.a. reflected binary code (Frank Gray, 1947)
• binary Gray code: successive values differ in only one bit
generate a Gray code (G(n)) for n bits from G(n−1):
G(n−1)
G(n−1)
G(n)
“reflected”binary code
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 3 - 5
Gray code• a.k.a. reflected binary code (Frank Gray, 1947)
• binary Gray code: successive values differ in only one bit
generate a Gray code (G(n)) for n bits from G(n−1):
G(n−1)
G(n−1)
0
01
1
...
...
G(n)
“reflected”binary code
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 3 - 6
Gray code• a.k.a. reflected binary code (Frank Gray, 1947)
• binary Gray code: successive values differ in only one bit
generate a Gray code (G(n)) for n bits from G(n−1):
G(n−1)
G(n−1)
0
01
1
...
...
G(n)
“reflected”binary code
n=1: 01
n=2: 010
101
1
0
n=3: 010
101
1
0
010
101
1
0
00
00
11
11
Observe that this way also the last andthe first number differ only in oneposition.
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 4
Uses – applications• initially for analog to digital conversion for compatible
color television signals
• also used in telegraphy and position encoders (linearand rotary)
• error correction in digital communications
• genetic algorithms
• solving puzzles (Towers of Hanoi, Chinese rings puzzle,Spin-out, ...)
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 5
binary ↔ Gray code conversionbn−1 bn−2 . . . b1 b0 ←→ gn−1 gn−2 . . . g1 g0
bn−1 = gn−1
∀0 ≤ i < n− 1
bi = bi+1 XOR gi gi = bi+1 XOR bi
X Y X XOR Y0 0 00
00
1 11
111
Ex: standard binary 1010b −→ ?
Ex: Gray code 01001g −→ ?
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 6 - 1
Puzzle: Tower of HanoiGiven: A stack of n different sized disks, arranged fromlargest on the bottom to smallest on top, placed on a rod,A. Plus two more empty rods, B and C.
Valid move: move one disk from one rod to another onebut not placing a larger disk on top of a smaller disk.
Task: Move the stack (tower) from rod A to rod B (usingrod C) (. . . with minimum number of moves)
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 6 - 2
Puzzle: Tower of HanoiGiven: A stack of n different sized disks, arranged fromlargest on the bottom to smallest on top, placed on a rod,A. Plus two more empty rods, B and C.
Valid move: move one disk from one rod to another onebut not placing a larger disk on top of a smaller disk.
Task: Move the stack (tower) from rod A to rod B (usingrod C) (. . . with minimum number of moves)
Solution (recursive): to move a tower, T (n), of size n fromA to B using C: move T (n−1) from A to C using B, thenthe largest disk from A to B, and then move T (n−1) fromC to B using A.
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 7 - 1
Tower of Hanoi vs. Gray code• label the disks from 0 (smallest) to n− 1 (largest) ≡
gn−1 gn−2 . . . g1 g0 . . . a number in Gray code
• The changes in the Gray code (if bit i changes) definethe disk (disk i) that has to be moved:
◦ disk 0 always cw for n odd and ccw for n even.
◦ disks i, 0 < i < n, unique way to move
A B
C
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 7 - 2
Tower of Hanoi vs. Gray code• label the disks from 0 (smallest) to n− 1 (largest) ≡
gn−1 gn−2 . . . g1 g0 . . . a number in Gray code
• The changes in the Gray code (if bit i changes) definethe disk (disk i) that has to be moved:
◦ disk 0 always cw for n odd and ccw for n even.
◦ disks i, 0 < i < n, unique way to move
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0
n is even → move 0 ccwn is even → move 0 ccw
A B
C
start: all 4 disks on Agoal: all 4 disks on B
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 7 - 3
Tower of Hanoi vs. Gray code
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0
n is even → move 0 ccwn is even → move 0 ccw
A B
C
start: all 4 disks on Agoal: all 4 disks on B
A B C
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 7 - 4
Tower of Hanoi vs. Gray code
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0
n is even → move 0 ccwn is even → move 0 ccw
A B
C
start: all 4 disks on Agoal: all 4 disks on B
A B C
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 7 - 5
Tower of Hanoi vs. Gray code
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0
n is even → move 0 ccwn is even → move 0 ccw
A B
C
start: all 4 disks on Agoal: all 4 disks on B
A B C
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 7 - 6
Tower of Hanoi vs. Gray code
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0
n is even → move 0 ccwn is even → move 0 ccw
A B
C
start: all 4 disks on Agoal: all 4 disks on B
A B C
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 7 - 7
Tower of Hanoi vs. Gray code
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0
n is even → move 0 ccwn is even → move 0 ccw
A B
C
start: all 4 disks on Agoal: all 4 disks on B
A B C
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 7 - 8
Tower of Hanoi vs. Gray code
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0
n is even → move 0 ccwn is even → move 0 ccw
A B
C
start: all 4 disks on Agoal: all 4 disks on B
A B C
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 7 - 9
Tower of Hanoi vs. Gray code
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0
n is even → move 0 ccwn is even → move 0 ccw
A B
C
start: all 4 disks on Agoal: all 4 disks on B
A B C
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 7 - 10
Tower of Hanoi vs. Gray code
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0
n is even → move 0 ccwn is even → move 0 ccw
A B
C
start: all 4 disks on Agoal: all 4 disks on B
A B C
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 7 - 11
Tower of Hanoi vs. Gray code
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0
n is even → move 0 ccwn is even → move 0 ccw
A B
C
start: all 4 disks on Agoal: all 4 disks on B
A B C
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 7 - 12
Tower of Hanoi vs. Gray code
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0
n is even → move 0 ccwn is even → move 0 ccw
A B
C
start: all 4 disks on Agoal: all 4 disks on B
A B C
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 7 - 13
Tower of Hanoi vs. Gray code
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0
n is even → move 0 ccwn is even → move 0 ccw
A B
C
start: all 4 disks on Agoal: all 4 disks on B
A B C
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 7 - 14
Tower of Hanoi vs. Gray code
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0
n is even → move 0 ccwn is even → move 0 ccw
A B
C
start: all 4 disks on Agoal: all 4 disks on B
A B C
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 7 - 15
Tower of Hanoi vs. Gray code
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0
n is even → move 0 ccwn is even → move 0 ccw
A B
C
start: all 4 disks on Agoal: all 4 disks on B
A B C
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 7 - 16
Tower of Hanoi vs. Gray code
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0
n is even → move 0 ccwn is even → move 0 ccw
A B
C
start: all 4 disks on Agoal: all 4 disks on B
A B C
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 7 - 17
Tower of Hanoi vs. Gray code
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0
n is even → move 0 ccwn is even → move 0 ccw
A B
C
start: all 4 disks on Agoal: all 4 disks on B
A B C
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 7 - 18
Tower of Hanoi vs. Gray code
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0
n is even → move 0 ccwn is even → move 0 ccw
A B
C
start: all 4 disks on Agoal: all 4 disks on B
A B C
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 7 - 19
Tower of Hanoi vs. Gray code
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0
n is even → move 0 ccwn is even → move 0 ccw
A B
C
start: all 4 disks on Agoal: all 4 disks on B
M. Gardner proved (1957,59) that the “Tower of Hanoi”problem is isomorphic to finding a Hamiltonian path on ann-hypercube.
n-hypercube has 2n vertices
a path visiting them all has 2n−1 edges
⇒ at least 2n−1 moves
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 8 - 1
Another puzzle: “Spin-out”• very similar to “Chinese Rings puzzle”
• 7 spinners take one of two possible orientations: ↑, ←
• initially all are ↑; the goal is that all are ←
• problem: only the rightmost spinner can rotate and anyspinner which is the left neighbor of the rightmost ↑
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 8 - 2
Another puzzle: “Spin-out”• very similar to “Chinese Rings puzzle”
• 7 spinners take one of two possible orientations: ↑, ←
• initially all are ↑; the goal is that all are ←
• problem: only the rightmost spinner can rotate and anyspinner which is the left neighbor of the rightmost ↑
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 8 - 3
Another puzzle: “Spin-out”• very similar to “Chinese Rings puzzle”
• 7 spinners take one of two possible orientations: ↑, ←
• initially all are ↑; the goal is that all are ←
• problem: only the rightmost spinner can rotate and anyspinner which is the left neighbor of the rightmost ↑
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 8 - 4
Another puzzle: “Spin-out”• very similar to “Chinese Rings puzzle”
• 7 spinners take one of two possible orientations: ↑, ←
• initially all are ↑; the goal is that all are ←
• problem: only the rightmost spinner can rotate and anyspinner which is the left neighbor of the rightmost ↑
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 8 - 5
Another puzzle: “Spin-out”• very similar to “Chinese Rings puzzle”
• 7 spinners take one of two possible orientations: ↑, ←
• initially all are ↑; the goal is that all are ←
• problem: only the rightmost spinner can rotate and anyspinner which is the left neighbor of the rightmost ↑
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 8 - 6
Another puzzle: “Spin-out”• Solution: n spinners from left to right ≡ gn−1 . . . g0
• spinner i is ↑ ≡ gi = 1; spinner i is ← ≡ gi = 0
• traverse the Gray code from 1 . . . 1 to 0 . . . 0 in reversedirection
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0 ↑ ←↑ ↑ ↑ ↑ ↑ ↑ ←↑ ↑ ←
←↑ ←← ←↑← ↑ ↑← ↑↑
↑← ↑← ← ↑←← ← ↑← ↑
←← ↑← ←←←← done
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 8 - 7
Another puzzle: “Spin-out”• Solution: n spinners from left to right ≡ gn−1 . . . g0
• spinner i is ↑ ≡ gi = 1; spinner i is ← ≡ gi = 0
• traverse the Gray code from 1 . . . 1 to 0 . . . 0 in reversedirection
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0 ↑ ←↑ ↑ ↑ ↑ ↑ ↑ ←↑ ↑ ←←↑ ←← ←↑← ↑ ↑← ↑↑↑← ↑← ← ↑←← ← ↑← ↑←← ↑← ←←←← done
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 8 - 8
Another puzzle: “Spin-out”• Solution: n spinners from left to right ≡ gn−1 . . . g0
• spinner i is ↑ ≡ gi = 1; spinner i is ← ≡ gi = 0
• traverse the Gray code from 1 . . . 1 to 0 . . . 0 in reversedirection
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0 ↑ ←↑ ↑ ↑ ↑ ↑ ↑ ←↑ ↑ ←←↑ ←← ←↑← ↑ ↑← ↑↑↑← ↑← ← ↑←← ← ↑← ↑←← ↑← ←←←← done
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 8 - 9
Another puzzle: “Spin-out”• Solution: n spinners from left to right ≡ gn−1 . . . g0
• spinner i is ↑ ≡ gi = 1; spinner i is ← ≡ gi = 0
• traverse the Gray code from 1 . . . 1 to 0 . . . 0 in reversedirection
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0 ↑ ←↑ ↑ ↑ ↑ ↑ ↑ ←↑ ↑ ←←↑ ←← ←↑← ↑ ↑← ↑↑↑← ↑← ← ↑←← ← ↑← ↑←← ↑← ←←←← done
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 8 - 10
Another puzzle: “Spin-out”• Solution: n spinners from left to right ≡ gn−1 . . . g0
• spinner i is ↑ ≡ gi = 1; spinner i is ← ≡ gi = 0
• traverse the Gray code from 1 . . . 1 to 0 . . . 0 in reversedirection
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0 ↑ ←↑ ↑ ↑ ↑ ↑ ↑ ←↑ ↑ ←←↑ ←← ←↑← ↑ ↑← ↑↑↑← ↑← ← ↑←← ← ↑← ↑←← ↑← ←←←← done
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 8 - 11
Another puzzle: “Spin-out”• Solution: n spinners from left to right ≡ gn−1 . . . g0
• spinner i is ↑ ≡ gi = 1; spinner i is ← ≡ gi = 0
• traverse the Gray code from 1 . . . 1 to 0 . . . 0 in reversedirection
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0 ↑ ←↑ ↑ ↑ ↑ ↑ ↑ ←↑ ↑ ←←↑ ←← ←↑← ↑ ↑← ↑↑↑← ↑← ← ↑←← ← ↑← ↑←← ↑← ←←←← done
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 8 - 12
Another puzzle: “Spin-out”• Solution: n spinners from left to right ≡ gn−1 . . . g0
• spinner i is ↑ ≡ gi = 1; spinner i is ← ≡ gi = 0
• traverse the Gray code from 1 . . . 1 to 0 . . . 0 in reversedirection
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0 ↑ ←↑ ↑ ↑ ↑ ↑ ↑ ←↑ ↑ ←←↑ ←← ←↑← ↑ ↑← ↑↑↑← ↑← ← ↑←← ← ↑← ↑←← ↑← ←←←← done
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 8 - 13
Another puzzle: “Spin-out”• Solution: n spinners from left to right ≡ gn−1 . . . g0
• spinner i is ↑ ≡ gi = 1; spinner i is ← ≡ gi = 0
• traverse the Gray code from 1 . . . 1 to 0 . . . 0 in reversedirection
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0 ↑ ←↑ ↑ ↑ ↑ ↑ ↑ ←↑ ↑ ←←↑ ←← ←↑← ↑ ↑← ↑↑↑← ↑← ← ↑←← ← ↑← ↑←← ↑← ←←←← done
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 8 - 14
Another puzzle: “Spin-out”• Solution: n spinners from left to right ≡ gn−1 . . . g0
• spinner i is ↑ ≡ gi = 1; spinner i is ← ≡ gi = 0
• traverse the Gray code from 1 . . . 1 to 0 . . . 0 in reversedirection
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0 ↑ ←↑ ↑ ↑ ↑ ↑ ↑ ←↑ ↑ ←←↑ ←← ←↑← ↑ ↑← ↑↑↑← ↑← ← ↑←← ← ↑← ↑←← ↑← ←←←← done
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 8 - 15
Another puzzle: “Spin-out”• Solution: n spinners from left to right ≡ gn−1 . . . g0
• spinner i is ↑ ≡ gi = 1; spinner i is ← ≡ gi = 0
• traverse the Gray code from 1 . . . 1 to 0 . . . 0 in reversedirection
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0 ↑ ←↑ ↑ ↑ ↑ ↑ ↑ ←↑ ↑ ←←↑ ←← ←↑← ↑ ↑← ↑↑↑← ↑← ← ↑←← ← ↑← ↑←← ↑← ←←←← done
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 8 - 16
Another puzzle: “Spin-out”• Solution: n spinners from left to right ≡ gn−1 . . . g0
• spinner i is ↑ ≡ gi = 1; spinner i is ← ≡ gi = 0
• traverse the Gray code from 1 . . . 1 to 0 . . . 0 in reversedirection
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0 ↑ ←↑ ↑ ↑ ↑ ↑ ↑ ←↑ ↑ ←←↑ ←← ←↑← ↑ ↑← ↑↑↑← ↑← ← ↑←← ← ↑← ↑←← ↑← ←←←← done
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 8 - 17
Another puzzle: “Spin-out”• Solution: n spinners from left to right ≡ gn−1 . . . g0
• spinner i is ↑ ≡ gi = 1; spinner i is ← ≡ gi = 0
• traverse the Gray code from 1 . . . 1 to 0 . . . 0 in reversedirection
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0 ↑ ←↑ ↑ ↑ ↑ ↑ ↑ ←↑ ↑ ←←↑ ←← ←↑← ↑ ↑← ↑↑↑← ↑← ← ↑←← ← ↑← ↑←← ↑← ←←←← done
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 8 - 18
Another puzzle: “Spin-out”• Solution: n spinners from left to right ≡ gn−1 . . . g0
• spinner i is ↑ ≡ gi = 1; spinner i is ← ≡ gi = 0
• traverse the Gray code from 1 . . . 1 to 0 . . . 0 in reversedirection
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0 ↑ ←↑ ↑ ↑ ↑ ↑ ↑ ←↑ ↑ ←←↑ ←← ←↑← ↑ ↑← ↑↑↑← ↑← ← ↑←← ← ↑← ↑←← ↑← ←←←← done
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 8 - 19
Another puzzle: “Spin-out”• Solution: n spinners from left to right ≡ gn−1 . . . g0
• spinner i is ↑ ≡ gi = 1; spinner i is ← ≡ gi = 0
• traverse the Gray code from 1 . . . 1 to 0 . . . 0 in reversedirection
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0 ↑ ←↑ ↑ ↑ ↑ ↑ ↑ ←↑ ↑ ←←↑ ←← ←↑← ↑ ↑← ↑↑↑← ↑← ← ↑←← ← ↑← ↑←← ↑← ←←←← done
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 8 - 20
Another puzzle: “Spin-out”• Solution: n spinners from left to right ≡ gn−1 . . . g0
• spinner i is ↑ ≡ gi = 1; spinner i is ← ≡ gi = 0
• traverse the Gray code from 1 . . . 1 to 0 . . . 0 in reversedirection
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0 ↑ ←↑ ↑ ↑ ↑ ↑ ↑ ←↑ ↑ ←←↑ ←← ←↑← ↑ ↑← ↑↑↑← ↑← ← ↑←← ← ↑← ↑←← ↑← ←←←← done
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 8 - 21
Another puzzle: “Spin-out”• Solution: n spinners from left to right ≡ gn−1 . . . g0
• spinner i is ↑ ≡ gi = 1; spinner i is ← ≡ gi = 0
• traverse the Gray code from 1 . . . 1 to 0 . . . 0 in reversedirection
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0 ↑ ←↑ ↑ ↑ ↑ ↑ ↑ ←↑ ↑ ←←↑ ←← ←↑← ↑ ↑← ↑↑↑← ↑← ← ↑←← ← ↑← ↑←← ↑← ←←←← done
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 8 - 22
Another puzzle: “Spin-out”• Solution: n spinners from left to right ≡ gn−1 . . . g0
• spinner i is ↑ ≡ gi = 1; spinner i is ← ≡ gi = 0
• traverse the Gray code from 1 . . . 1 to 0 . . . 0 in reversedirection
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0 ↑ ←↑ ↑ ↑ ↑ ↑ ↑ ←↑ ↑ ←←↑ ←← ←↑← ↑ ↑← ↑↑↑← ↑← ← ↑←← ← ↑← ↑←← ↑← ←←←← done
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 8 - 23
Another puzzle: “Spin-out”• Solution: n spinners from left to right ≡ gn−1 . . . g0
• spinner i is ↑ ≡ gi = 1; spinner i is ← ≡ gi = 0
• traverse the Gray code from 1 . . . 1 to 0 . . . 0 in reversedirection
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0 ↑ ←↑ ↑ ↑ ↑ ↑ ↑ ←↑ ↑ ←←↑ ←← ←↑← ↑ ↑← ↑↑↑← ↑← ← ↑←← ← ↑← ↑←← ↑← ←←←← done
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 8 - 24
Another puzzle: “Spin-out”• Solution: n spinners from left to right ≡ gn−1 . . . g0
• spinner i is ↑ ≡ gi = 1; spinner i is ← ≡ gi = 0
• traverse the Gray code from 1 . . . 1 to 0 . . . 0 in reversedirection
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0 ↑ ←↑ ↑ ↑ ↑ ↑ ↑ ←↑ ↑ ←←↑ ←← ←↑← ↑ ↑← ↑↑↑← ↑← ← ↑←← ← ↑← ↑←← ↑← ←←←← done
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 8 - 25
Another puzzle: “Spin-out”• Solution: n spinners from left to right ≡ gn−1 . . . g0
• spinner i is ↑ ≡ gi = 1; spinner i is ← ≡ gi = 0
• traverse the Gray code from 1 . . . 1 to 0 . . . 0 in reversedirection
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0 ↑ ←↑ ↑ ↑ ↑ ↑ ↑ ←↑ ↑ ←←↑ ←← ←↑← ↑ ↑← ↑↑↑← ↑← ← ↑←← ← ↑← ↑←← ↑← ←←←← done
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 8 - 26
Another puzzle: “Spin-out”• Solution: n spinners from left to right ≡ gn−1 . . . g0
• spinner i is ↑ ≡ gi = 1; spinner i is ← ≡ gi = 0
• traverse the Gray code from 1 . . . 1 to 0 . . . 0 in reversedirection
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0 ↑ ←↑ ↑ ↑ ↑ ↑ ↑ ←↑ ↑ ←←↑ ←← ←↑← ↑ ↑← ↑↑↑← ↑← ← ↑←← ← ↑← ↑←← ↑← ←←←← done
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 8 - 27
Another puzzle: “Spin-out”• Solution: n spinners from left to right ≡ gn−1 . . . g0
• spinner i is ↑ ≡ gi = 1; spinner i is ← ≡ gi = 0
• traverse the Gray code from 1 . . . 1 to 0 . . . 0 in reversedirection
Ex:n = 4
0 0 0 00 0 00 00 0 0
00
0000
111
1110110
1111
1111111111
1111
11
11
11
0000
00
00
00 0
0 ↑ ←↑ ↑ ↑ ↑ ↑ ↑ ←↑ ↑ ←←↑ ←← ←↑← ↑ ↑← ↑↑↑← ↑← ← ↑←← ← ↑← ↑←← ↑← ←←←← done
Graz University of TechnologyInstitute for Software Technology
Oswin Aichholzer (slides TH): Enumerative Combinatoric Algorithms, 2020 9
Generate next Gray code numberG[i] → G[i+ 1]
• count the number #1 of “1”s in G[i]
• if #1 is even then “flip” g0 of G[i] to get G[i+ 1]
• else “flip” the left neighbor (bit position) of therightmost “1”
func nextGray(g):
if (#1(g) is even) then flip g[0];elser ← index of rightmost “1” of gif (r < n−1) then flip g[r+1]; else flip g[r]; fi
return (g)
. . . g = g[n− 1] . . . g[0]
fi
. . . next Gray number (G[0] if input was G[2n−1])