enumerative combinatoric algorithms (716.035)enumerative combinatoric algorithms (716.035) 2020...

57
Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara Gigerl in 2017. These notes contain some material not or only partially covered by the available slides. The notes are incomplete, might contain errors, and can be updated on an irregular base, especially after class using appropriate feedback. It is not intended that these notes cover all the topics presented in the course / relevant for the exams. By using these notes you understand that this is on your own risk and that you have to attend classes on a regular base. If you would like to contribute to these notes, please contact me. Please also report typos and other bugs in class, thank you. 1

Upload: others

Post on 24-Jan-2021

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

Enumerative Combinatoric Algorithms(716.035)

2020

Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and byBarbara Gigerl in 2017.

These notes contain some material not or only partially covered by the available slides.The notes are incomplete, might contain errors, and can be updated on an irregularbase, especially after class using appropriate feedback. It is not intended that thesenotes cover all the topics presented in the course / relevant for the exams. By usingthese notes you understand that this is on your own risk and that you have to attendclasses on a regular base.

If you would like to contribute to these notes, please contact me. Please also reporttypos and other bugs in class, thank you.

1

Page 2: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Index

Contents

1 Enumerating vs. Counting 41.1 Permutation of Letters . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.2 Crossing-free Spanning Path for Points in Convex Position . . . . . . . . 4

2 Pigeonhole-Principle 72.1 Example 1: Socks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.2 Example 2: Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.3 Example 3: Hairs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82.4 Example 4: Party . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82.5 Example 5: Numbers II . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

3 Polyominos 10

4 Gray codes 134.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134.2 Conversions: Binary ↔ Gray Code . . . . . . . . . . . . . . . . . . . . . 144.3 Towers Of Hanoi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154.4 Spin-out . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164.5 Generation of the next Gray code number . . . . . . . . . . . . . . . . . 16

5 Inclusion-Exclusion 17

6 Spanning trees in ladders 18

7 Polya-Redfield Enumeration Theorem aka Burnside’s Lemma 227.1 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227.2 Counting Orbits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237.3 Example: Strings (1) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247.4 Example: Strings (2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267.5 Example: Strings (3) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277.6 Example: Grids (4) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287.7 Example: Grids (5) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317.8 Grids (6) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337.9 Grids (7) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357.10 Cube (8) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367.11 Cube (10) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377.12 Tik Tak Toe (11) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

8 Reverse Search 408.1 Description of the algorithm . . . . . . . . . . . . . . . . . . . . . . . . . 408.2 Implementation of the algorithm . . . . . . . . . . . . . . . . . . . . . . . 428.3 Example: Triangulations . . . . . . . . . . . . . . . . . . . . . . . . . . . 458.4 Example: Tik Tak Toe . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

2

Page 3: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Index

8.5 Example: Enumeration of Polyominos . . . . . . . . . . . . . . . . . . . . 49

9 Combinatorial 2 Player Games 509.1 Example: Tik Tak Toe . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509.2 Basics on 2 Player Games . . . . . . . . . . . . . . . . . . . . . . . . . . 509.3 Example: Connect-4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55

3

Page 4: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Enumerating vs. Counting

1 Enumerating vs. Counting1

1.1 Permutation of Letters2

Example: How many words can we make out of the letters A B C using each letter3

once?4

� ABC � BAC � CAB5

� ACB � BCA � CBA6

When we list all objects as above we call it enumeration, whereas counting is only7

concerned with the total number of objects. If we consider the example above, how8

many words would be possible for A B C D?9

It’s best to find a formula, as using it is a very efficient way to count objects. For n = 410

letters we end up with 24 permutations.11

The formula for the number of different words with n letters is n!12

1.2 Crossing-free Spanning Path for Points in Convex Position13

How many crossing-free spanning paths exist for n ≥ 2 points in convex position?14

Figure 1: An example illustrating a spanning path for n = 9 points

Let’s look at some examples for n ≥ 2 and try to determine a suitable formula.15

4

Page 5: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Enumerating vs. Counting

• n = 2

1.

• n = 3

1. 2. 3.

• n = 4

1. 2. 3. 4.

5. 6. 7. 8.

Figure 2: Enumeration of crossing free-spanning paths up to n = 4

As we can see from these examples, enumeration can become a tedious and error prone16

task very fast. Can you list all paths for n = 5?17

It is better to abstract the problem and find an inductive solution. When constructing18

the path we start with a point, and from it we only see two immediate choices. After19

one of those points is added, we have two choices again. This goes on for a while until20

n− 2.21

2 · 2 · 2 · · · 2 · 2︸ ︷︷ ︸n−2 times

= 2n−2

Now in order to construct all paths we need to start at all possible points. However,22

when we do that double counting occurs.23

n · 2n−2 ⇒ n · 2n−2

2⇒ n · 2n−3 for n ≥ 2

We can use this formula to find the number of crossing-free spanning paths for n = 5,24

which gives us 5 · 22 = 20 paths.25

5

Page 6: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Enumerating vs. Counting

5× with no diagonal

2× 5× with one diagonal

5× with two diagonals

Figure 3: Another method of enumeration, do not explicitly listsimilar objects

6

Page 7: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Pigeonhole-Principle

2 Pigeonhole-Principle26

Also known as Dirichlet principle, or in German ”Schubfach Prinzip”.27

If you have n + 1 elements (pigeons) which you put into n boxes (pigeonholes), then28

there is a box with at least two elements (two pigeons).29

General form: If you have k elements which you put into n boxes, then there is a box30

with at least⌈kn

⌉and one with at most

⌊kn

⌋elements.31

2.1 Example 1: Socks32

You have n black socks and m white socks in a drawer. How many socks do you have33

to pick out in order to get a matching pair?34

If you abstract the problem using the pigeonhole principle it leads to the following setup.35

In total there are n + m elements that are distributed upon two boxes, one for white36

socks and one for black socks. If we pick a sock out of the drawer and put it into one37

of the two boxes, we have to repeat the process at most three times to have a box with38

two elements in it (a matching pair of socks).39

For n = 2 boxes there have to be n+1 = 3 elements, such that one of the boxes contains40

at least⌈32

⌉= 2 elements.41

2.2 Example 2: Numbers42

Let S be a subset of {1, 2, · · · , 2n} of cardinality n+ 1. Prove or give a counter example43

for: S contains two numbers a and b such that44

� a+ b = 2n+ 145

� a− b = n46

To solve this problem we have to think of appropriate labels for the boxes.47

{1, 2n} n boxes

{2, 2n− 1}{3, 2n− 2}

...{n, n+ 1}

48

There is a subset of cardinality n + 1, so taking a number and putting it into the box49

with its name on the label, there has to be at least one box with two elements, thus50

adding up to 2n+ 1.51

7

Page 8: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Pigeonhole-Principle

Similarly, solve the second problem by using different labels52

{1, n+ 1} n boxes

{2, n+ 2}{3, n+ 3}

...{n, n+ n︸ ︷︷ ︸

2n

}

53

2.3 Example 3: Hairs54

Prove: In Austria there exist at least nine people with the same number of hairs on their55

head.56

Facts:57

� On average people have 150.000 hairs58

� It is save to assume that the range is 0− 1.000.00059

� Population of Austria: 8.699.730 (Jan 1st 2016)60

Take the total range of hairs as boxes, leading to 1.000.001 boxes. At least⌈86997301000001

⌉∼61

d8, 7e = 9 people have the same number of hairs on their head in Austria.62

2.4 Example 4: Party63

At a party n ≥ 2 people meet. Some shake hands with others and some don’t. Show or64

provide a counter example:65

� There are at least two people at this party shaking the same number of hands.66

There are two cases to consider for the boxes.67

1. No one shakes zero hands.68

# handshakes1

n− 1 boxes23...n− 1

69

8

Page 9: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Pigeonhole-Principle

2. At least one person shakes zero hands70

# handshakes0

n− 1 boxes12...n− 2

71

In both cases we have n− 1 boxes for n elements, meaning there always has to be a box72

with at least two elements.73

2.5 Example 5: Numbers II74

Let q > 0 be an odd integer number and let S be the set {ai = 2i − 1|1 ≤ i ≤ q} =75

{1, 3, 7, 15, 31, . . . , 2q − 1}.76

Claim: For each odd number q there exists an ai ∈ S which is a multiple of q. That is, q77

divides ai without rest: ai = c·q+ri, c > 0 (integer) and ri = 0.78

To prove the claim we label the boxes with all possible values of ri:79

{ri = 0} q boxes

{ri = 1}{ri = 2}

...{ri = q − 1}

80

If there is an element in the box with label {ri = 0} then we are done. So assume the81

contrary, i.e., we can ignore the box with label {ri = 0}. Thus we have q− 1 boxes, that82

is, there have to be two numbers in S with the same rest. Let them be am = cm · q+ rm83

and an = cn · q + rn with m > n and rm = rn.84

am = cm · q + rm

− an = cn · q + rn

am − an = c∗ · q m > n

am − an = 2m − 1− (2n − 1) =

2m − 2n = 2n (2m−n − 1)︸ ︷︷ ︸am−n

= c∗ · q

As 2 is not a factor of q (recall that q is odd) we conclude that am−n must be a multiple85

of q. This is a contradiction to the assumption that there is no element in the box with86

label {ri = 0} and thus proves the claim.87

9

Page 10: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Polyominos

3 Polyominos88

n=1 n=3 n 36=6=

Figure 4: A polyomino of size n (called n-polyomino) consists of nunit squares connected via edges, aligned on a grid. The

rightmost collection of blue unit squares is not a polyomino.

hole

Figure 5: A polyomino with a hole inside. Polyominos without holesare called animals.

How many n-polyominos do exist? We have to define how to count them, that means89

defining the parameters that decide when two polyominos are the same. Polyominos can90

be compared using three operations:91

1. Translation: Move one polyomino on top of another, if they are congruent, then92

they are the same.93

2. Rotation: Additionally rotate one polyomino. If there is one rotation that makes94

them congruent, they are the same.95

3. Reflection: Move in the 3rd dimension, mirroring (reflecting) the polyomino.96

With these operations polyominos can be classified into these two groups:97

� Fixed polyominos: Only translation is allowed.98

10

Page 11: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Polyominos

� Free polyominos: Translation, rotation and reflection is allowed.99

n # fixed # free1 1 12 2 13 6 24 19 55 63 12...

......

Figure 6: How many n-polyominos do exist?

Is there a method for generating all n-polyominos? Look at a step from n→ n+ 1: we100

can add one unit square to all boundary edges. In the worst case (a straight polyomino)101

that means 2n+ 2 possibilities (prove that this is really the maximum (Hint: count the102

number of boundary edges by induction)). We also have to show that this way we do not103

under count, that is, that we generate all (n + 1)-polyominios. Hint: Look at the dual104

graph and consider a subgraph forming a spanning tree!105

If in that way we generate k new (n + 1)-polyominos, then this can be done in Θ(nk)106

time and space.107

In the described way we will generate all (n + 1)-polyominios, but we might gener-108

ate them more than once, i.e., we over count. How can we best deal with dupli-109

cates?110

Approach 1: Generate all new polyominos, then compare them all. For size n + 1, k111

polyominos are generated. ⇒ O((k2) · n) = O(k2 · n) time is needed. This is not very112

efficient, as k >> n.113

Approach 2: We define a fingerprint for each polyomino: Place the polyomino in the114

first quadrant, alligned to the x- and y-axes. Build a vector for a polyomino that is115

given by the coordinates of the squares. Do this in order for each column bottom up,116

and consider columns from left to right. Repeat this for all (at most eight) copies of the117

polyomino (4x rotation & 2x reflection) and take the lexicographical minimum of the118

vectors! This is the fingerprint of the polyomino. It has size O(n) and can be computed119

in O(n) time.120

11

Page 12: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Polyominos

0 1 2 3

1

2

3

⇓(1,1)(1,2)(1,3)(2,1)

0 1 2 3

1

2

3

⇓(1,1)(1,2)(2,1)(3,1)

Figure 7: Polyomino, two possible vectors, fingerprint on the left.

Idea and analysis for finding duplicates using the fingerprint:121

⇒ Compute fingerprints of all generated polyominos: O(k · n)122

⇒ Sort all fingerprints: O(k · log(k) · n), duplicates are neighboured in the sorting123

and removing them needs O(k · n) time124

⇒ the total runtime is O(k · log(k) · n)125

So far no formula is known for the number of fixed or free polyominos. For the asymp-126

totics bounds exist:127

limn→∞

#(n+ 1) polyominos

#n polyominos= c

where c is some constant with 4.00253 ≤ c ≤ 4.65. That means that the number of128

polyominos grows exponentially like Θ(cn).129

12

Page 13: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Gray codes

4 Gray codes130

4.1 Introduction131

Codes are used to represent information, e.g. text, symbols, images and so on. Binary132

codes use binary numbers (0s and 1s) for representation.133

000b = 0001b = 1010b = 2011b = 3100b = 4101b = 5110b = 6111b = 7

Assume we want to take some measurements of a device. Ifthat device uses binary codes, the measurements for 3 and 4are next to each other. During a transition from 3 to 4, everybit changes. If timing is not absolutely perfect (which neveris) then we might read any number from 0 to 7.

134

Gray codes provide a solution to this problem. When encoding information using135

Gray codes, successive values differ only in one bit. These codes can be created eas-136

ily.137

� We start with n = 1. For this the Gray code is very easily given by 0 and 1.138

� For n = 2 we take the code for n = 1 and reflect it along an imaginary axis as139

shown in Figure 8. For the first part above the axis we add a 0 in front, for the140

second part a 1.141

� The same is done for n = 3. We take the code for n = 2, reflect it and add 0s and142

1s for the first position as shown in Figure 9.143

� This goes on for every n and n− 1.s144

Since we ware actually reflecting codes, Gray codes are also code ”reflected” binary145

codes.146

Note that gray codes are cyclic. This means that the first and the last codeword are147

also ”neighbors”, i.e., the first can be created from the last by changing exactly one148

bit.149

13

Page 14: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Gray codes

0 0

0 1

1 1

1 0

Figure 8: Gray code for n = 2

0 0 0

0 0 1

0 1 1

0 1 0

1 1 0

1 1 1

1 0 1

1 0 0

Figure 9: Gray code for n = 3

G(n)

G(n−

1)G(n−

1)

1

.

.

.10

.

.

.0

Figure 10: Gray code creation in general

Gray codes were initially used for analog to digital conversion for compatible color150

television signals. They are also used in telegraphy and position encoders as well as151

for genetic algorithms.152

4.2 Conversions: Binary ↔ Gray Code153

We want to convert binary into Gray code and vice versa.

bn−1bn−2...b1b0 ↔ gn−1gn−2...g1g0

A first observation is that the first symbol of the binary code and the Gray code haveto be the same:

bn−1 = gn−1

14

Page 15: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Gray codes

We can now construct a binary code from a given Gray code like:

bi = bi+1 XOR gi

The construction of Gray codes from binary codes is very similar:

gi = bi+1 XOR bi

For example, if we want to convert 1010b, we would get 1111g. If we want to convert154

01001g, we would get 01110b.155

4.3 Towers Of Hanoi156

When solving this puzzle, we have a stack of n different sized disks, arranged from largest157

on the bottom to the smallest on top. They are placed on a rod, A. Additionally, we158

have two more empty rods, B and C. A valid move is given by moving one disk from159

one rot to another one but not placing a larger disk on top of a smaller disk. The task160

is to move the stack (tower) from rod A to rod B.161

One of the common solutions for this puzzle is the recursive one. We want to move162

tower, T (n), of size n from A to B using C. We move T (n − 1) form A to C using163

B, then the largest disk from A to B, and then move T (n − 1) from C to B using164

A.165

Solution using Gray codes. However, we can also use Gray codes to solve this166

puzzle. This is a non-recursive solution. First we label the disks from 0 (smallest)167

to n − 1 (largest). gn−1gn−2...g1g0 represent the numbers in the Gray code. The168

changes in the Gray code (if bit i changes) define the disk (disk i) that has to be169

moved.170

� Disk 0 is always moved clockwise (or counterclockwise, depends no the parity)171

� Disk i, 0 ≤ i ≤ n has a unique way to move172

In 1957, Gardner proved that the Tower of Hanoi is isomorphic to finding a Hamiltonian173

path on an n hypercube. An n-hypercube has 2n vertices, a path visiting them all has174

2n − 1 edges. Therefore, we need at least 2n − 1 moves.175

15

Page 16: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Gray codes

4.4 Spin-out176

The puzzle ”Spin-out” is very similar to the ”Chinese Rings puzzle”. 7 spinners take177

one of two possible orientations, ↑ and ←. Initially, all spinners are ↑ and the goal is178

that all spinners are←. The problem is, that only the rightmost spinner can rotate and179

any spinner which is the left neighbor of the rightmost ↑.180

The solution can be constructed as follows: We have n spinners from left to right,181

gn−1, ..., g0. If spinner i is ↑≡ gi = 1. If spinner i is ←≡ gi = 0. Now we traverse the182

Gray code from 1, ..., 1 to 0, .., 0 in reverse direction.183

4.5 Generation of the next Gray code number184

Assume we are given G[i] and we want to construct G[i + 1] from it. We count the185

number a of ”1”s in G[i]. If it is even, then we flip g0 of G[i] to get G[i+ 1]. Otherwise,186

we flip the left neighbor of the rightmost ”1”.187

func nextGray ( g ) :188

i f a(g) i s even then f l i p g[0]189

e l s e190

r ← index o f r ightmost ”1” in g191

i f ( r < n− 1 ) then f l i p g[r + 1]192

e l s e f l i p g[r]193

r e turn g194

Listing 1: PseudoCode for next element in Gray Code,

16

Page 17: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Inclusion-Exclusion

5 Inclusion-Exclusion195

Lemma: For two sets A,B we are interested in |A ∪B|:|A ∪B| = |A|+ |B| − |A ∩B|

Lemma: For three setsA,B,C we are interested in |A∪B ∪C|:|A ∪B ∪ C| = |A|+ |B|+ |C| − |A ∩B| − |A ∩ C| − |B ∩ C|+ |A ∩B ∩ C|

Theorem: General form for n sets A1, . . . , An:196

|A1 ∪ A2∪, . . . ,∪An| =n∑

j=1

(−1)j−1∑|I|=j

∣∣∣∣∣⋂i∈I

Ai

∣∣∣∣∣where I ⊆ {1, . . . , n} is a set of indices.197

Example: How many numbers of the set {1, . . . , 45} are coprime (do not have a common198

divisor) with 45?199

We can use the following equation to find a solution.

X = 45− |{numbers which have at least one common divisor with 45}|

For that we need to take a look at the prime coefficients of 45 which consist of 3 · 3 · 5and count the members of the set divisible by them.

Xi = |numbers of {1, . . . , 45} divisible by i|

X3 =45

3= 15

X5 =45

5= 9

X3·5 = X15 =45

15= 3

In the last line, we made preparations to ensure that no double count occurs.

X = 45−X3 −X5 +X3·5 = 45− 15− 9 + 3 = 24

Example 2: How many numbers of the set {1 . . . 60} are coprime with 60?

60 = 2 · 2 · 3 · 5X = 60− X2︸︷︷︸

30

− X3︸︷︷︸20

− X5︸︷︷︸12

+ X6︸︷︷︸10

+ X10︸︷︷︸6

+ X15︸︷︷︸4

− X30︸︷︷︸2

X = 16

17

Page 18: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Spanning trees in ladders

6 Spanning trees in ladders200

spoke

Figure 11: A ladder of size 3. A ladder of size n has n spokes.

How many spanning trees does a ladder of size n contain as subgraphs (with the same201

vertex set)?202

n = 1:

1x

n = 2:

4x

n = 3: ? → We need to remove 2 edges!

6 6

6 6simplify

18

Page 19: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Spanning trees in ladders

n = 1:

1x

n = 2:

4x

n = 3: ? → We need to remove 2 edges!

6 6

6 6simplify

Removing 2 spokes: = 3x

Removing 1 spoke:

2x 4x 2x

= 8x

Removing 0 spokes:

2x

2x= 4x

The total number of spanning trees in a ladder of size n = 3 is thus 3 + 8 + 4 =203

15.204

How many spanning trees does a ladder of size n = 4, 5, . . . contain as subgraphs? Can205

we find a nice formula for this problem for general n?206

XA(n) . . .# of spanning trees in a ladder of size n

XB(n) . . .# of spanning graphs which consist of 2 components

and are cycle free for a ladder of size n such that

the rightmost 2 vertices are in different

components (after rotating the ladder by 90◦).

19

Page 20: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Spanning trees in ladders

We derive the following table:207

208

n→ n+ 1 start with XA(n) start with XB(n)

XA(n+ 1)

3x 1x

XB(n+ 1)

2x 1x

209

From this table we getXA(n+ 1) = 3XA(n) +XB(n)

XB(n+ 1) = 2XA(n) +XB(n)

and we can transform this into a matrix.210

(XA(n+ 1)XB(n+ 1)

)=

(3 12 1

)(XA(n)XB(n)

)=

(3 12 1

)n(11

)because

XA(1) = 1

XB(1) = 1

and therefore (XA(n)XB(n)

)=

(3 12 1

)n−1(11

)However, we are only interested in XA(n) and want to eliminate XB(n) from the equa-211

tions:212

XB(n+ 1) = 2XA(n) +XB(n)

XA(n+ 1) = 3XA(n) +XB(n) −→ XB(n+ 1) = XA(n+ 1)−XA(n)

→ XB(n) = XA(n)−XA(n− 1)

→ XA(n+ 1) = 3XA(n) +XA(n)−XA(n− 1)= 4XA(n)−XA(n− 1)

With this recursive formula we can compute the required values easily.213

20

Page 21: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Spanning trees in ladders

XA(1) = 1

XA(2) = 4

XA(3) = 4 · 4− 1 = 15

XA(4) = 4 · 15− 4 = 56

...

In fact, we can use linear algebra or generating functions to get rid of this recursion and214

obtain:215

XA(n) =1

2 ·√

3(2 +

√3)n − 1

2 ·√

3(2−

√3)n

Asymptotically this gives: Θ((2 +√

3︸ ︷︷ ︸3,73...

)n)216

21

Page 22: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Polya-Redfield Enumeration Theorem aka Burnside’s Lemma

7 Polya-Redfield Enumeration Theorem aka Burnside’s217

Lemma218

7.1 Definitions219

Objects and operations. Consider a set of objects, X, and a set of n operations,220

R, where R is given by221

R = {Ri(x), 0 ≤ i ≤ n− 1}

Objects might be strings, colored grids, colored cubes and so on. Operations might be222

rotations, reflections, translations, ...223

R must form a group w.r.t ◦ in order to apply the Polya-Redfield Theorem. A group is a224

nonempty set fulfilling the following 4 axioms ∀Ri, Rj ∈ R:225

1. ∃Rk(x) : Ri(x) ◦Rj(x) = Rk(x) (closure)226

2. (Ri(x) ◦Rj(x)) ◦Rk(x) = Ri(x) ◦ (Rj(x) ◦Rk(x)) (associativity)227

3. R0(x) ◦Ri(x) = Ri(x) ◦R0(x) = Ri(x) (identity element R0(x))228

4. ∃Rk(x) : Rk(x) ◦Ri(x) = Ri(x) ◦Rk(x) = R0(x) (inverse element Rk(x))229

In our case, R0(x) is the identity (neutral) operation for objects inX.230

Note that R(x) does not have to be a commutative group, i.e., Ri(x) ◦ Rj(x) 6= Rj(x) ◦231

Ri(x).232

Orbits. An orbit is the set of all objects from X which can be transformed into233

each other by an operation from R. They are said to form an equivalence class. The234

length of an orbit is the number of elements it contains.235

Stabilizers. For an object x ∈ X, the operation Ri(x) ∈ R is a stabilizer if and onlyif Ri(x) = x. Let mx be the number of stabilizers for x. ri is the invariance numberof the operation Ri(x). The invariance number tells you, for how many objects Ri(x) isa stabilizer. We have

n−1∑i=0

ri =∑x∈X

mx

To see this equivalence, we use the idea of double counting. Each pair (operation,236

object) which gives an invariance is counted once on each side, the left side is counted237

by operation, the right side is counted by objects.238

22

Page 23: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Polya-Redfield Enumeration Theorem aka Burnside’s Lemma

7.2 Counting Orbits239

We start by showing that the sum of the number of stabilizers for all elements of an240

orbit equals n, i.e.,241

∑x∈orbit

mx = n (1)

Graz University of TechnologyInstitute for Software Technology

Oswin Aichholzer: Enumerative Combinatoric Algorithms, 2015 7

Counting Orbits 1Deriving a first relation – simplified version (not valid in fullgenerality):

∑x∈orbit mx = n

This implies∑

x∈X mx =∑

all orbits∑

x∈orbit mx =∑all orbits n = n times the number of orbits.

Thus

number of orbits =

∑x∈X mx

n

one orbit

n multi-objects

Two times the sameobject: unify the vertices!For removed copy: removeoutgoin edges, rerouteincoming edges.We get one new stabilizer!

Graz University of TechnologyInstitute for Software Technology

Oswin Aichholzer: Enumerative Combinatoric Algorithms, 2015 7

Counting Orbits 1Deriving a first relation – simplified version (not valid in fullgenerality):

∑x∈orbit mx = n

This implies∑

x∈X mx =∑

all orbits∑

x∈orbit mx =∑all orbits n = n times the number of orbits.

Thus

number of orbits =

∑x∈X mx

n

one orbit

n multi-objects

Two times the sameobject: unify the vertices!For removed copy: removeoutgoin edges, rerouteincoming edges.We get one new stabilizer!

Figure 12: If all objects are different, we obtain n times identity as a stabilizer. If we havethe same object two times, we have to unify the vertices. For the removedcopy of the vertex we remove the outgoing edges and reroute the incomingedges. We loose an identity as a stabilizer, but get one new stabilizer so thetotal number is unchanged.

Let O be the set of all orbits, then we have∑x∈X

mx =∑o∈O

∑x∈orbito

mx

=∑o∈O

n

= n · |O|

⇒ number of orbits = |O| =

∑x∈X

mx

n

With the previous observationn−1∑i=0

ri =∑x∈X

mx we get

number of orbits =

n−1∑i=0

ri

n

242

23

Page 24: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Polya-Redfield Enumeration Theorem aka Burnside’s Lemma

We will use this equation, as usually n << |X| and thus, all invariance numbers ri are243

easier to obtain than all numbers mx of stabilizers.244

Recall that whenever you come up with a new set of operations it is important to prove245

that they fulfill the 4 group axioms!246

7.3 Example: Strings (1)247

Consider strings of length 4 containing characters {A,B}. We can shift each string by i248

positions with 0 ≤ i ≤ 3. How many different strings exist?249

In this case we have:

X = {all strings of length four consisting of only A and B}= {AAAA,AAAB,AABA,ABAA,BAAA,AABB,ABBA,BBAA, ...}

Step 1: Identify all operations, i.e., the set R, and their invariance numbers. Since250

0 ≤ i ≤ 3, we have in sum four shift operations.251

R = {Ri(x), 0 ≤ i ≤ 3}= {R0(x), R1(x), R2(x), R3(x)}= {no shift at all, shift by 1 position, shift by 2 positions, shift by 3 positions}

Now we compute the invariance numbers ri for each operation:252

R0(x) 0 shifts r0 = 24 = 16 the whole set X (2 characters for 4 positions give 24

possibilities)

R1(x) 1 shift r1 = 2 {AAAA} {BBBB}

R2(x) 2 shifts r2 = 4 {AAAA} {BBBB} {ABAB} {BABA}

R3(x) 3 shifts r3 = 2 {AAAA} {BBBB}

Table 1: Example: Strings (1) - invariance numbers for operations R0 to R3

In this case, R1(x) means to shift each letter by one position to the left. The first letter253

is added to the end of the string:254

24

Page 25: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Polya-Redfield Enumeration Theorem aka Burnside’s Lemma

X X X X

255

Accordingly, R2(x) is given by:256

X Y X Y

257

X and Y are used to symbolize different groups.258

259

Step 2: Compute the number of orbits. The number of orbits is given according tothe formula:

number of orbits =

n−1∑i=0

ri

n

=16 + 2 + 4 + 2

4= 6

Now we write down the set X and group the elements into their orbits. The groups260

are formed by strings that are basically the same but shifted.261

AAAAAAABAABAABAABAAAAABBABBABBAABAABABABBABAABBBBBBABBABBABBBBBB

Orbit 1Orbit 2Orbit 3Orbit 4Orbit 5Orbit 6

262

25

Page 26: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Polya-Redfield Enumeration Theorem aka Burnside’s Lemma

7.4 Example: Strings (2)263

Consider strings of length 4 containing characters {A,B,C}. We can shift each string by264

i positions with 0 ≤ i ≤ 3. How many different strings exist?265

X = {all strings of length four consisting of only A,B and C}

R0(x) 0 shifts r0 = 34 = 81 the whole set X (3 characters for 4 positions give 34

possibilities)

R1(x) 1 shift r1 = 3 {AAAA} {BBBB} {CCCC}In this case, the first letter must be equal to the second,the second must be equal to the third and so on. Whenwe look at the positions before the shift, we would see1234 which is transformed to 2341 by the shift. Theonly possibility to reach this equality is, that the lettersare all the same.

R2(x) 2 shifts r2 = 9 We have 1234 first and we obtain 3412, so the first andthe third letter must be equal as well as the second andthe fourth. We get the structure XYXY , which leavesus with 3 possibilities to choose X and 3 possibilities tochoose Y .

R3(x) 3 shifts r3 = 3 see r1 (it is the same but we shift in the other direction)

Table 2: Example: Strings (2) - invariance numbers for operations R0(x) to R3(x)

The number of orbits is: 964

= 24266

26

Page 27: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Polya-Redfield Enumeration Theorem aka Burnside’s Lemma

7.5 Example: Strings (3)

Consider strings of length 4 containing characters {A,B,C} and each character is usedat least once. We can shift each string by i positions with 0 ≤ i ≤ 3. How many differentstrings exist?

R0(x) 0 shifts r0 = 36 We know that when every character has to be used atleast once, one character has to be represented 2 times.We have a choice of 3 characters, {A,B,C}, which needto fit into 4 boxes. For one of these choices we endup with something like ?A?A. In the end we have twopossible ways to fill up the two characters which areleft, for example BACA and CABA. Therefore we get3 ·(42

)· 2 = 3 · 6 · 2 = 36.

R1(x) 1 shift r1 = 0 As we know from Example 7.4, all the letters have to bethe same. Obviously, no such string can exist.

R2(x) 2 shifts r2 = 0 As we know from Example 7.4, two of the letters have tobe the same. Again, no such string can exist accordingto the problem definition.

R3(x) 3 shifts r3 = 0 see r1

Table 3: Example: Strings (3) - invariance numbers for operations R0 to R3

The number of orbits is: 36+0+0+04

= 9

267

27

Page 28: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Polya-Redfield Enumeration Theorem aka Burnside’s Lemma

7.6 Example: Grids (4)268

Consider a 2x2 grid as shown below. Color the unit squares with 2 different colors.269

How many different colorings exist, if we allow (a) rotation (b) rotation and reflec-270

tion?271

First we determine how the operations R look like.272

0◦ 90◦

180◦ 270◦

Figure 13: R0(x) to R3(x), rotation by 0, 90, 180 and 270 degrees.

(a) rotation.273

R0(x) no rotations r0 = 24 = 16 We have 2 colors which can be placed on 4 squares.

R1(x) 90◦ rotation r1 = 2 The only possibility to do a 90◦ rotation such that thesquare looks the same afterwards is to color all squaresin one color.

R2(x) 180◦ rotation r2 = 2 + 2 = 4 Either everything is in the same color or the diagonalshave the same color (2 options for this).

R3(x) 270◦ rotation r3 = 2 see r1

Table 4: Example: Grids (4) - invariance numbers for operations R0(x) to R3(x)

28

Page 29: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Polya-Redfield Enumeration Theorem aka Burnside’s Lemma

The number of orbits for case (a) is: 244

= 6.274

X

X

X

X

X

Y

Y

X

r1 r2

Figure 14: Grouping for r1 and r2 with regard to rotation.

(b) rotation and reflection.275

We still need to take a look at R4(x) to R7(x), our operations concerning reflection. We276

allow horizontal and vertical reflection as well as reflection on either diagonal.277

A

B

A

B

A

A

B

B

r4 r5

A

C

B

A

r6 r7

B

A

A

C

Figure 15: Grouping for r4 to r7 with regard to reflection.

29

Page 30: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Polya-Redfield Enumeration Theorem aka Burnside’s Lemma

R4(x) vertical reflection r4 = 22 = 4 Our square is divided into two halves,the upper half and the lower half. Inorder to get the same result after a ver-tical reflection, the two squares in theupper half and the two in the lower halfmust be in the same color.

R5(x) horizontal reflection r5 = 22 = 4 Our square is divided into two halves,the left half and the right half. Sim-ilar to r4, the two squares on the leftmust have the same color and the twosquares on the right must have thesame color.

R6(x) diagonal reflection r6 = 2 · 2 · 2 = 8 We must make sure that the colorsalong the diagonal, which is orthogo-nal to the diagonal we are reflecting,are the same.

R7(x) diagonal reflection r7 = 8 see r6

Table 5: Example: Grids (4) - invariance numbers for operations R4(x) to R7(x)

The number of orbits for case (b) is therefore 488

= 6. In general, the number of orbits278

should decrease if we add operations. However, in this case it does not because the279

rotations can simulate the reflections.280

30

Page 31: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Polya-Redfield Enumeration Theorem aka Burnside’s Lemma

7.7 Example: Grids (5)281

Consider a 2x2 grid as shown. Color the unit squares with 3 different colors. How many282

different colorings exist, if we allow (a) rotation, (b) rotation and reflection, (c) only283

reflection?284

(a) rotation285

R0(x) no rotation r0 = 34 = 81 We have 3 colors which can be placed on 4 squares.

R1(x) 90◦ rotation r1 = 3 If we consider our square as [[A B][C D]], the 90◦ ro-tation will give us [[C A][D B]] as a result. Therefore,A = C, B = A, C = D and D = B. Hence, we have 3colors and 1 possibility to choose it.

R2(x) 180◦ rotation r2 = 32 = 9 If we consider our square as [[A B][C D]], the 180◦ ro-tation will give us [[D C][B A]] as a result. Therefore,A = D, B = C, C = B and D = A. Hence, we have 3possibilities and 2 regions to color.

R3(x) 270◦ rotation r3 = 3 see r1

Table 6: Example: Grids (5) - invariance numbers for operations R0(x) to R3(x)

The number of orbits is 81+3+9+34

= 964

= 24.286

31

Page 32: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Polya-Redfield Enumeration Theorem aka Burnside’s Lemma

(b) rotation and reflection.287

R4(x) vertical reflection r4 = 32 = 9 Our square is divided in two halvesagain (upper, lower) which have to lookthe same.

R5(x) horizontal reflection r5 = 32 = 9 same as r4 but instead of upper andlower half we have a left and right half

R6(x) diagonal reflection r6 = 33 = 27 We must make sure that the colorsalong the diagonal, which is orthogo-nal to the diagonal we are reflecting,are the same.

R7(x) diagonal reflection r7 = 27 see r6

Table 7: Example: Grids (5) - invariance numbers for operations R4(x) to R7(x)

The number of orbits is 96+9+9+27+278

= 1688

= 21.288

(c) only reflection. If we repeat the same for (c) we would get 9+9+27+274

= 724

= 18 or-289

bits. This is suspicious because we actually allow less operations and therefore the num-290

ber of orbits should be bigger than in (b). Recall that the operations must form a group291

in order to be able to apply the Polya-Redfield Theorem.292

Therefore, we have to check all the group axioms:293

1. Closure: The operations which would equal 180◦ and 270◦ rotations are miss-294

ing. For example, consider Ri(x) = R5(x) and Rj(x) = R6(x). We start with295

[[A B][C D]] and after applying R5(x) we obtain [[C D][A B]]. Then we apply296

R6(x) and get [[B D][A C]]. However, there is no possibility to apply any operation297

to the initial square which will give us [[B D][A C]] instantly.298

2. Identity element: obviously there is currently no identity element, i.e., no operation299

which does not change anything.300

32

Page 33: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Polya-Redfield Enumeration Theorem aka Burnside’s Lemma

7.8 Grids (6)301

Consider a 3x3 grid as shown below. Color the unit squares with 2 different colors.302

How many different colorings exist, if we allow (a) rotation (b) rotation and reflec-303

tion?304

(a) rotation.305

R0(x) no rotation r0 = 29 = 512 We have 2 colors which can be placed on 9 squares.

R1(x) 90◦ rotation r1 = 23 = 8 The square has to be colored like[[A B A][B C B][A B A]] in order to stay thesame after a 90◦ rotation.

R2(x) 180◦ rotation r2 = 25 = 32 The square has to be colored like[[B D A][C E C][A D B]] in order to stay thesame after a 180◦ rotation.

R3(x) 270◦ rotation r3 = 8 see r1

Table 8: Example: Grids (6) - invariance numbers for operations R0(x) to R3(x)

The number of orbits is 512+8+32+84

= 5604

= 140.306

33

Page 34: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Polya-Redfield Enumeration Theorem aka Burnside’s Lemma

(b) rotation and reflection.307

R4(x) vertical reflection r4 = 26 = 64 Our square has to look like[[A F A][B E B][C D C]] in order tostay the same.

R5(x) horizontal reflection r5 = 26 = 64 same as r4

R6(x) diagonal reflection r6 = 26 = 64 We have 3 diagonal colors to choose and thecolors of the two triangles, separated by thediagonals, which consist of 3 squares, have tobe the same.

R7(x) diagonal reflection r7 = 64 see r6

Table 9: Example: Grids (4) - invariance numbers for operations R4(x) to R7(x)

The number of orbits is 560+64+64+64+648

= 8168

= 102.308

B

A

C

B A

B

B AA

r1 ∼ r3

C

A

E

D B

C

D AB

r2

B

C

E

D C

B

F AA

r4 ∼ r5

B

D

E

B A

C

C FA

r6 ∼ r7

Figure 16: Grouping for all relevant cases for a 3x3 grid with up to 2different colors. r4 and r5 as well as r6 and r7 do not lookexactly identical when grouped but the concept is similar,

just using a different axis.

34

Page 35: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Polya-Redfield Enumeration Theorem aka Burnside’s Lemma

7.9 Grids (7)309

Consider a 3x3 grid as shown below. Color the unit squares with 3 different colors.310

How many different colorings exist, if we allow (a) rotation (b) rotation and reflec-311

tion?312

(a) rotation.313

R0(x) no rotation r0 = 39 = 19683 We have 3 colors which can be placed on 9 squares.

R1(x) 90◦ rotation r1 = 33 = 27 The colors used for the four edges have to be the same aswell as the colors used for the four middle edge squares.Additionally, there is one color for the center square([[A B A][B C B][A B A]]).

R2(x) 180◦ rotation r2 = 35 = 243 [[A B C][D E D][C B A]]

R3(x) 270◦ rotation r3 = 33 = 27 see r1

Table 10: Example: Grids (7) - invariance numbers for operations R0(x) to R3(x)

The number of orbits is 19683+27+27+2434

= 199804

= 4995.314

(b) rotation and reflection.315

R4(x) vertical reflection r4 = 36 = 729

R5(x) horizontal reflection r5 = 36 = 729

R6(x) diagonal reflection r6 = 36 = 729

R7(x) diagonal reflection r7 = 36 = 729

Table 11: Example: Grids (7) - invariance numbers for operations R4(x) to R7(x)

The number of orbits is 729+729+729+729+199808

= 228968

= 2862.316

35

Page 36: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Polya-Redfield Enumeration Theorem aka Burnside’s Lemma

7.10 Cube (8)317

For a 3D cube color the 6 faces with 2 colors. How many different colorings are possible,318

if you can rotate the cube as usual in 3-space?319

320

R0(x) no rotation |R0(x)| = 1 r0 = 26 = 64 Since we have 6 faces, we cancolor the cube in 6 different faceswith 2 colors.

RF90 180 270(x) rotation

via face(90◦, 180◦

or 270◦)

|RF90 180 270| =

3 times eachoperation

rF90 = rF270 =23 = 8rF180 = 24 =16

For 90◦ and 270◦ we have 3 dif-ferent faces (front, back, coat).For 180◦ we have 4 different faces(front, back, 2 parts of coat).

RE180(x) rotation

via edge(180◦)

|RE180(x)| =

6 times eachoperation

rE180 = 23 = 8 In this case, front = back, left= bottom and top = right whichmakes 3 possibilities.

RV120 240(x) rotation

via vertex(120◦ or240◦)

|RV120 240(x)| =

4 times eachoperation

rV120 = rV240 =22 = 4

We must color the cube suchthat we can group three squarescomprising one edge. Obviously,there are 2 such three-square-regions.

Table 12: Example: Cube (8) - invariance numbers for operations

The number of orbits is 64+3·(8+8+16)+6·8+2·4·41+9+6+8

= 64+96+48+3224

= 24024

= 10.321

Note that for 3 or k colors (as suggested in Cube (9)) only the base of number of colors322

changes. So e.g. for R0(x) we would get r0 = k6 colorings.323

36

Page 37: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Polya-Redfield Enumeration Theorem aka Burnside’s Lemma

7.11 Cube (10)324

For a 3D cube place the numbers 1 to 6 on the 6 faces. How many different labelings are325

possible, if you can rotate the cube as usual in 3-space? The orientation of a number326

within a face can be ignored, that is, we only consider the assignement of numbers to327

faces.328

This task is trivial because only the identity operation has to be considered which gives329

r0 = 6! = 720 labellings. All the other operations would give invariance numbers of 0330

because the labellings have to be different. Therefore, the number of orbits we get is331

72024

= 30.332

7.12 Tik Tak Toe (11)333

Enumerate all different valid game positions of Tic Tac Toe after k half-moves, con-334

sidering symmetries. o starts, x follows → bk+12co and bk

2cx tokens are placed after k335

half-moves.336

k = 1.337

R0(x) no rotation r0 = 9

R1(x) 90◦ rotation r1 = 1 one center element

R2(x) 180◦ rotations r2 = 1 see r1

R3(x) 270◦ rotations r3 = 1 see r1

R4(x) vertical reflections r4 = 3 middle row

R5(x) horizontal reflection r5 = 3 middle column

R6(x) diagonal reflection r6 = 3 diagonal

R7(x) diagonal reflection r7 = 3 diagonal

Table 13: Example: Tick Tack Toe (11) - invariance numbers for operations R0(x) toR3(x) for k = 1

The number of orbits is: 248

= 3.338

37

Page 38: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Polya-Redfield Enumeration Theorem aka Burnside’s Lemma

k = 2. Consider: 1 circle and 1 cross.339

R0(x) no rotation r0 = 72 9 · 8

R1(x) 90◦ rotation r1 = 0 We would have to place both, circle andcross, in the center but we cannot place 2elements in the center.

R2(x) 180◦ rotations r2 = 0 see r1

R3(x) 270◦ rotations r3 = 0 see r1

R4(x) horizontal reflections r4 = 3 · 2 = 6 3 possibilities for the first move, 2 for thesecond (in the middle row).

R5(x) vertical reflection r5 = 6 see r4

R6(x) diagonal rotation r6 = 6 see r4

R7(x) diagonal rotation r7 = 6 see r4

Table 14: Example: Tick Tack Toe (11) - invariance numbers for operations R0(x) toR7(x) for k = 2

The number of orbits is: 968

= 12.340

38

Page 39: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Polya-Redfield Enumeration Theorem aka Burnside’s Lemma

k = 3. Consider: 2 circles and one cross or two crosses and one circle.341

R0(x) 0 rotations r0 =(92

)·(71

)= 252

R1(x) 90◦ rotation r1 = 0

R2(x) 180◦ rotations r2 = 4 cross in center, circle opposite

R3(x) 270◦ rotations r3 = 0 see r1

R4(x) horizontal reflections r4 = 12 cross in the middle row (3 possibilities),the two circles either in the middle row(1 possibility) or in the opposite places(3 possibilities): 3 · (1 + 3) = 12

R5(x) vertical reflection r5 = 12

R6(x) diagonal rotation r6 = 12

R7(x) diagonal rotation r7 = 12

Table 15: Example: Tick Tack Toe (11) - invariance numbers for operations R0(x) toR7(x), k = 3

The number of orbits is: 3048

= 38.342

39

Page 40: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Reverse Search

8 Reverse Search343

Reverse Search is a search technique which aims at the efficient enumeration of objects.344

It was introduced by Avis and Fukuda in 1992 [1]. Initially, it was developed for enu-345

merating vertices in polyhedra, but in general it is an algorithm which can be applied346

to a wide variety of problems.347

Reverse search is often applied in fields where we have a lot of unstructured data which348

cannot be stored simultaneously. When enumerating, we want to get all objects exactly349

once, so how can we guarantee that no element is reported twice? Previously generated350

objects cannot simply be looked up because in most cases we cannot store that much351

data.352

8.1 Description of the algorithm353

When doing reverse search, we organize all elements in an abstract graph G = (V,E).354

The set of vertices V represents the objects which we want to enumerate. Each object355

appears exactly once in the graph. The vertices are connected by edges, E, which rep-356

resent some neighbor-relation between some of the objects. The basic idea is to traverse357

the graph from the root and visit each node exactly once.358

At this point, we have to take into account a few important things:359

� How can we ensure each element is visited exactly once?360

� Once we are at a vertex, how can we find it’s neighbors?361

� How do we choose the root of the graph?362

First of all, the graph has to be connected, i.e., each vertex must be reachable. Oth-363

erwise, some elements are not reported at all.364

We can define neighbors by a so-called neighborhood relation. If the neighborhood365

relation is applied to an object, it transforms the object into a set of similar objects, its366

neighbors. In the abstract graph, there will be an edge between two objects if applying367

the relation to one of them, the other results.368

Example: Consider enumerating subsets of a given set. A possible neighborhood rela-369

tion could be to remove/add an element from the set. This will lead to the following370

graph:371

40

Page 41: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Reverse Search

{A,B,C}

{A,C}{A,B} {B,C}

{A} {B} {C}

{}

Figure 17: In this example we also see that one vertex was selected to be the root. Whichobject is considered as the root depends on the application, but is very oftena basic element or the most complex element in the set.

In summary, we have:

Γ(v), v ∈ V...neighborhood relation

v0 ∈ V...root element

This relation has to be symmetric as the algorithm will have to traverse edges for-372

wards and backwards. Recall: A relation R is symmetric iff ∀a, b ∈ X : aRb =373

bRa.374

Special neighbors (predecessors). Now we have defined our abstract graph with ver-375

tices, edges and the root element. Still we are missing something: How can we actually376

traverse the graph and how do we know that we are finished? We need to add directions377

to the graph.378

The solution to this is a concept called special neighbors or predecessors. Eachvertex has exactly one predecessor. The basic idea is to use this predecessor in order tofind the root node after a finite number of steps, so:

∀v ∈ V \ {v0} : ∃f(v) ∈ Γ(v)

fk(v) = f(f(f(...f(v)))) = v0

f(v0) = v0

This means that if we go to the predecessor, find its predecessor, the predecessor of that379

node and so on we will after k steps be at the root element v0. Obviously it is very380

important to ensure that f does not introduce cycles. This actually transforms our381

graph into a spanning tree T : (V,E ′), T ⊆ G. The tree T has the same vertex set V382

but usually less edges than the graph because we only use those edges which lead to a383

predecessor.384

41

Page 42: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Reverse Search

v0

Figure 18: Initial graph (gray dotted lines) and the tree which is obtained by applyingthe function for predecessors.

In summary, we can define our ”cooking recipe” as:385

1. Define a neighborhood relation which is symmetric.386

2. Choose a root element.387

3. Define the function to obtain predecessor (predecessor function). It must ensure388

that the tree is connected.389

8.2 Implementation of the algorithm390

391

v ←v0 ; j ← 0 ; count ← 1392

r e p e a t393

wh i l e ( j < δ ( v ) ) do394

j ← j + 1395

w ← j−th ne i ghbo r o f v396

i f ( f (w) == v ) then397

v ← w; j ← 0398

count ← count + 1399

end i f400

end wh i l e401

i f ( v 6= v0 ) then402

w ←v ; v ← f (w) ; j ← 0403

r e p e a t404

j v0 j + 1405

u n t i l (w == j−th ne i ghbo r o f v )406

end i f407

u n t i l ( ( v == v0 ) AND ( j == δ ( v0 ) )408

Listing 2: Pseudocode describing Reverse Search

Listing 2 describes the Reverse Search algorithm. The following section describes the409

algorithm in more detail.410

42

Page 43: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Reverse Search

411

412

v ←v0 ; j ← 0 ; count ← 1413

r e p e a t414

wh i l e ( j < δ ( v ) ) do415

j ← j + 1416

w ← j−th ne i ghbo r o f v417

i f ( f (w) == v ) then418

v ← w; j ← 0419

count ← count + 1420

end i f421

end wh i l e422

i f ( v 6= v0 ) then423

w ←v ; v ← f (w) ; j ← 0424

r e p e a t425

j ← j + 1426

u n t i l (w == j−th ne i ghbo r o f v )427

end i f428

u n t i l ( ( v == v0 ) AND ( j == δ ( v0 ) )429

430

Listing 3: Pseudocode describing Reverse Search in sections

Initialization

431

Reverse Traverse (≡ DFS)

δ(v) ... degree of v

γ(v,j) ∈ Γ v(r1)(r2)

432

Forward Traverse restore j

(f1)

(f2)

433

As it can be seen in Listing 3, the algorithm can be divided into three stages:434

1. Initialization435

2. Reverse traverse436

3. Forward traverse437

First of all, in the initialization phase, we assign the root vertex to v. v will be the vertex438

we are currently visiting and we start traversing at the root vertex. Then we also set439

j to 0. This is our local counter which keeps track of the number of neighbors we have440

already visited. In contrast, count is the global counter which keeps track of all visited441

nodes.442

In the next phase, the Reverse traverse, we visit all neighbors of v. Each neighbor is443

temporarily stored in variable w. Then we check if w has as the predecessor vertex v.444

If it has, we set w to v and reset out local counter j. This describes a step in the depth445

first search we are doing. It is called reverse traverse because we are actually going in446

the opposite direction than depicted in Figure 18. We do this as long as we have visited447

all neighbors of a v. This is checked by j < δ(v). The function δ returns the degree of448

v.449

43

Page 44: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Reverse Search

j

δ(v)

2

1

f(1)

f(2)

f(3)

v

××

Figure 19: Reverse traverse phase

After this we enter the Forward traverse. Here we check first if we are at the root again.450

If not, we do a forward step, in which we go closer to the root by finding the predecessor451

of w, and resetting the local counter j. In a loop we have to restore j, i.e., go back to452

the point where we stopped before entering BFS.453

j

δ(v)

v

2

1

w

Figure 20: Forward traverse phase

Reverse traverse and Forward traverse are repeated until we reach the root and all454

neighbors of the root node were visited. Therefore it is possible to reach the root note455

more than once, but we only stop if all neighbors of it were visited. Then we can be456

sure that all nodes were visited.457

44

Page 45: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Reverse Search

Complexity analysis. We can estimate the algorithm’s runtime as:458

O(|V |2 · t)

Let tN the time we need to compute neighbors. Let tf be the time to compute prede-459

cessors. Let t = tN + tf . Then:460

(r1) Θ(|E| · tN)

(r2) Θ(|E| · tf )

(f1) Θ(|V | · tf )

(f2) Θ(|V |2 · tN)

with |V | − 1 ≤ |E| ≤ |V |2 the above resultfollows

461

8.3 Example: Triangulations462

Enumerate all triangulations of 6 points in convex position.463

1 2

3

45

6

Figure 21: Example of convex polygon comprised of 6 points

We first define the lexicographic order of a triangulation in the polygon. Let P =p1, p2, ...pn be the set of points comprising the polygon. The diagonals in the polygonare represented as:

pipj

where pi, pj ∈ P and i ≤ j. A diagonal d1 = pipj is lexicographically larger than a464

diagonal d2 = p′ip′j if either pi > p′i, or pi = p′i and pj > p′j.465

45

Page 46: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Reverse Search

1 2

3

45

6

Figure 22: Example of a triangulated convex polygon. The triangulation would be givenby 26, 35, 36, where the edges are sorted in increasing lexicographically order.

According to our cooking recipe we defined in 8.1, we now have to find the three ingre-466

dients we need for reverse search.467

Neighborhood relation γ(v, k): flip the kth diagonal of v (see Figure 23)

Root element v0 is the lexicographic minimal triangulation (see Figure 24)

Predecessor function f(v): flip the lexicograpically largest edge to a smaller edge.This is always possible, except when v = v0 (see Figure 25)

468

w = γ(v, 3)

v26, 35, 36

w26, 25, 35

1 2

3

45

6

1 2

3

45

6

Figure 23: Example of two neighbors in the tree.

1 2

3

45

6 v013, 14, 15

Figure 24: Root v0 (lexicographic minimal triangulation).

46

Page 47: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Reverse Search

v = f (w)

v26, 25, 35

w26, 35, 36

1 2

3

45

6

1 2

3

45

6

Figure 25: Example of finding the predecessor of a vertex.

1 2

3

45

6

×

×

×

××

×

××

Figure 26: Abstract tree during reverse search.

47

Page 48: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Reverse Search

8.4 Example: Tik Tak Toe469

Enumerate all valid game positions (not considering symmetries) of Tik Tak Toe. The470

first player is o, the second x.471

We again start to define our abstract graph G(V,E). The vertices V are the game posi-472

tions. We assume that our board cells are labeled as shown in Figure 27.473

Neighborhood relation γ(v, k): legally add the kth mark (or legally remove one mark)

Root element empty board as shown in Figure 27

Predecessor function f(v): remove the highest legal mark

474

1 2 3

4 5 6

7 8 9

Figure 27: Root v0.

1 2 3

4 5 6

7 8 9

O O

O

O X O

X

X O

X

O

X

O

X

O

X

OO

X

O

O

X

OO

Figure 28: Abstract tree during reverse search (incomplete)

48

Page 49: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Reverse Search

8.5 Example: Enumeration of Polyominos475

Enumerate all n-polyominos.476

Neighborhood relation add/remove a unit square (see predecessor function for de-tails)

Root element v0 is the 1-polyomino

Predecessor function f(v): Compute the fingerprint. Delete the lexicographicallylargest allowed unit square. Allowed are those for which adual spanning tree exists so that the unit square is dual to aleave.

477

Figure 29: Spanning tree of 14-polyomino - we are only allowed to remove the leaves.

49

Page 50: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Combinatorial 2 Player Games

9 Combinatorial 2 Player Games478

9.1 Example: Tik Tak Toe479

Assume we want to enumerate all different valid game positions of Tic Tac Toe after k480

half-moves, considering symmetries. If o starts and x follows, after k half moves there481

will be⌊k+12

⌋o and

⌊k2

⌋x tokens on the board.482

Now we want to store a board. We use 2 bit per square (x, o and empty) which will483

give 2 × 9 = 18 bit per board. This gives us the possibility to store 218 = 262144484

boards.485

However, actually we have only three possibilities per square (an not four, as depicted486

by the first idea). This gives us the number of different boards which can occur during487

any game, namely 39 = 19683. Each board would then require dlog2 39e = 15 bit of488

space.489

n half-moves game-tree different boards0 1 11 9 32 72 123 504 384 3024 1085 15120 1746 60480 2287 181440 1748 362880 899 362880 23

sum 362880 850

Table 16: Tik Tak Toe in numbers

Table 16 compares the growth of the game tree with the number of different game boards490

after a certain number of half moves. The game-tree complexity which is obtained in491

the end is 362880. This describes the sketch of the game-tree after 9 half-moves. In492

contrast to that, there is the state space complexity. This is the number of different493

boards which can exist in the game.494

9.2 Basics on 2 Player Games495

In the following we consider 2 player perfect information games. This means:496

� There are exactly 2 players, e.g. Alice and Bob.497

50

Page 51: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Combinatorial 2 Player Games

� No hidden information, randomness or change exists. Both players have all infor-498

mation.499

� Players play in turns, not simultaneously.500

� The number of game states is finite. Nevertheless, the game might last forever in501

case of a draw.502

� The game might be asymmetric, i.e., Alice and Bob have different tasks (e.g. Fox503

and Geese where the fox (first player) has to capture geese (second player), the504

geese have to surround the fox)505

A game state needs to be stored memory efficient and complete. The so-called move506

generator computes all successors of a game state while the backwards move gener-507

ator does the reverse thing, i.e. finds the predecessors of a game state. Note that this is508

not always possible at all. Win, lose and draw states comprise final states.509

Equality of game states. Two game states are equivalent if they allow the same510

moves w.r.t the state, resulting in the same successor states w.r.t the state. Typ-511

ically reflection, rotation, inversion, color change and similar operations can be ap-512

plied.513

Now we only need to store the move information for one of the equivalent states. This514

is what we call a canonical state (fingerprint).515

Example: Nine Men’s Morris. Assume we want to enumerate all different game posi-516

tions for Nine Men’s Morris. How many non-equivalent states exist after two white to-517

kens and one black token have been placed (white player starts)?518

Figure 30: Example state in Nine Men’s Morris

This is a perfect application area of the Polya-Redfield Enumeration Theorem. We have519

to consider 16 operations:520

51

Page 52: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Combinatorial 2 Player Games

R0(x) No rotation r0 =((242)

)· 22 = 6072 We have to choose two

from 24 positions for thewhite tokens and one ofthe remaining 22 posi-tions for the black token.

R1(x) 90◦ rotation r1 = 0 There is no chance to ro-tate the board 90◦ andget the same board as be-fore.

R2(x) 180◦ rotation r2 = 0 see r1

R3(x) 270◦ rotation r3 = 0 see r1

R4(x)...R7(x) Reflections r4 = r5 = r6 = r7 =6 · (9 +

(52

)) = 114 TODO

R8 In-Out Inversion r8 = 8 · (8+(72

)) = 232

TODO

R9(x)...R15(x) In-Out-Inversion plusR1...R7

r9 = r10 = r11 = 0,r12 = r13 = r14 =r15 = 2 · 11 = 22

TODO

Table 17: Example: Nine Men’s Morris game after 2 white tokens and 1 black tokenhave been placed - invariance numbers for operations R0 to R3

The number of orbits is given by: 6072+4·114+232+4·2216

= 684816

= 428.521

Levels of Game Solutions. There are different levels of game solutions which will tell522

us about how much we know about the winning strategy.523

1. Ultra-weakly solved: We know which player can win, but not how. There is524

basically no strategy.525

2. Weakly solved: A strategy is known from a start situation.526

3. Strongly solved: A strategy is known from any valid state.527

4. Ultra-strongly solved: For any valid game state and any possible move it is528

known whether it is a win, draw or lose and in how many half-moves this happens.529

52

Page 53: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Combinatorial 2 Player Games

We aim for ultra-strongly solved.530

Game-Tree Complexity vs. State-Space Complexity Game-Tree Complexity de-531

scribes the number of nodes the complete decision tree for a whole game has. In contrast,532

State-Space Complexity gives the number of states which can be reached from the533

start state by valid moves. For most games, a state might be reachable via many dif-534

ferent sequences of valid moves. This is why the game-tree complexity will in most535

cases be greater than the state-space complexity. Obviously, game-tree complexity can-536

not be smaller than state-space complexity because in the ”worst” case we have states537

which cannot be summarized into equality classes, so each state is its own equality538

class.539

game state-space complexity game-tree complexity branching factor

Tic Tac Toe 103 105 5

Nine Men’s Morris 1010 1050 10-30

Pyraos 1011 1033 9

Awari 1012 1032 5-6

Connect-4 1014 1021 5-7

Abalone 1025 10180 65-70

Reversi 1028 1058 5-15

Chess 1050 10123 35

Go 10171 10360 300-400

Table 18: Game-Tree Complexities and State-Space Complexities for different games

Enumerating all states. We store all non-equivalent states in a set S. The approach540

is based on the state space complexity.541

542

I n i t i a l i z e S with the s t a r t i n g s t a t e543

544

∀ non−p r o c e s s e d s t a t e s s ∈ S DO545

/* p r o c e s s newly added s t a t e s */546

∀ s u c c e s s o r s t o f s DO547

compute c a n o n i c a l s t a t e t′ o f t548

IF t′ /∈ S THEN add t′ to S549

53

Page 54: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Combinatorial 2 Player Games

/* S c o n t a i n s a l l s t a t e s which a r e r e a c h a b l e from the s t a r t550

s t a t e v i a v a l i d moves */551

Listing 4: Enumerate all states

For every game state we compute a code (integer number ≥ 0) which contains all infor-552

mation when playing starts or continues from this state.553

1. WIN: code odd554

number of half-moves in which a win can be forced (if player plays perfect)555

2. LOSE: code even556

number of half-moves in which the game is at most lost (if opponent plays perfect)557

3. DRAW: special code (e.g. -1), no number of half-moves possible558

559

I n i t i a l i z e a l l s t a t e s w i thou t v a l i d moves w i th code 0 or draw560

/* t e rm i n a l l o s e s t a t e s */561

562

IF s u c c e s s o r s t a t e w i th even code e x i s t s THEN563

code = ( sma l l e s t even code o f a s u c c e s o r s t a t e ) + 1564

/* WIN i n tha t number o f moves */565

566

ELSE IF s u c c e s s o r s t a t e w i th draw code e x i s t s THEN567

code = draw568

/* DRAW */569

570

ELSE571

code = ( l a r g e s t odd code o f a s u c c e s s o r s t a t e ) + 1572

/* LOSE i n tha t number o f moves */573

Listing 5: Computation of codes (general process)

574

I n i t a l l s t a t e s w i thou t v a l i d moves575

/* t e rmmina l s t a t e s w i thou t s u c c e s s o r s */576

I n i t a l l r ema i n i ng s t a t e s w i th ’ unde f i ned ’577

578

FOR k = 1 TO max−depth /* k = o f h a l f−moves */579

∀ s t a t e s s ∈ S with s t i l l u nd e f i n ed code DO580

IF k i s odd THEN581

IF s has a s u c c e s s o r w i th code k − 1 THEN582

code o f s i s k /* Win s t a t e */583

ELSE /* k i s even */584

IF a l l s u c c e s s o r s o f s have odd codes THEN585

code o f s i s k /* Lose s t a t e */586

54

Page 55: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Combinatorial 2 Player Games

587

Set a l l u nd e f i n ed s t a t e s to draw .588

Listing 6: Computation of codes (Pseudocode)

Now we still need to know how to use the code, e.g., how to play for a current state589

s:590

� Compute all possible successors of s and their codes.591

� If a successor with even code exists, make the move which leads to the successor592

with the smallest even code k. Message: ”I will win in k half-moves.”593

� Else if a successor with code draw exists, make the draw move. Message: ”You594

might make a draw.”595

� Else make the move to the successor with the highest (odd) code k. Message: ”You596

might win in k half-moves.”597

9.3 Example: Connect-4598

With how many (few) byte can you store a game state of Connect-4? You are al-599

lowed to use the information on how many half-moves (0 to 42) have already been600

made.601

All game states are stored memory efficient and complete. The move generator just602

adds a token in a non-full column. At most 7 successors exist. The final states for603

lose (i.e., previous player win) is found by just checking up to 11 4-tuples (including604

the new token). If we place 42 tokens and no player wins, we terminate in a draw605

state.606

However, there is still the need for an efficient backwards move generator.607

half-moves

differentboards

half-moves

differentboards

half-moves

differentboards

0 1 8 91295 16 1778411601 4 9 269531 17 3637981952 25 10 809464 18 7674355803 121 11 2148087 19 14488942674 568 12 5832236 20 28189934205 2144 13 14105207 21 49073902006 8231 14 35045629 22 87881320167 27109 15 77785047 23 14066554884

sum 33475164421

Table 19: Number of different boards for 0 to 23 half-moves in Connect-4. 33475164421states stored with 6 byte each: 200 GB + 34 GB

55

Page 56: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA Combinatorial 2 Player Games

How can we store all possible positions?608

� 2 bits per square: 22·6·7 = 284 2× 1025 states: 1 byte each, 17592186000000 TB609

� 3 possibilities per square: 36·7 = 342 1020 states: 1 byte each, 99515990 TB610

� 6 byte per state: 26·8 = 281474976710656 1014 states: 1 byte each, 281 TB611

Storing all possible states up to 23 half-moves needs 234 GB. The maximal remaining612

search depth is 42 - 23 = 19 with 5 possible moves in average.613

56

Page 57: Enumerative Combinatoric Algorithms (716.035)Enumerative Combinatoric Algorithms (716.035) 2020 Course notes for the term 2020, based on a writeup by Patrick Hipp in 2016 and by Barbara

ECA References

References614

[1] David Avis and Komei Fukuda. Reverse search for enumeration. Discrete Applied615

Mathematics, 65(1-3):21–46, 1996.616

57