Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Polyominoes: an Exploration in Problem Solving ...

Polyominoes: an Exploration in Problem Solving (with Python)

A "Polyomino" is a geometric figure of adjacent squares. But just how many "n" polyominoes are there?

Hamish Campbell

September 07, 2013

Transcript

30 slides
  1. Let's be specific Fixed Distinct when none is a translation

    of another... One-sided And none is a rotation of another... Free And none is a reflection / glide reflection of another.
  2. Define a Polyomino class Polyomino: def __init__(self, iterable): self.squares =

    tuple(iterable) p = Polyomino(((0,0), (0,1), (1,1)))
  3. Enumerating from Search Space n2 = 36 squares “From n

    choose r “ import gmpy gmpy.comb(36, 6) >> 1947792 GMPY: https://code.google.com/p/gmpy/ Multiple-precision arithmetic for Python
  4. Enumerating from Search Space 1 + (2 x 5) nCr

    6 + (3 x 4) nCr 6 1 + \ gmpy.comb(2*5, 6) +\ gmpy.comb(3*4, 6) >> 1135
  5. Enumeration def count_enumerations(d): import gmpy return 1 + sum( gmpy.comb(i

    * (1 + d ­ i), d) for i in range(1 + d // 2, d) )
  6. Enumeration for i in range(5, 10): print i, count_enumerations(i) 5

    183 6 1,135 7 18,668 8 172,732 9 3,655,850
  7. Building from Lower Order (n = 3) (n - 2)

    x 4 x (n -1) x 4 n = 4 (n – 3) x 4 x (n – 2) x 4 x (n – 1) x 4
  8. Building from Lower Order Assumes 4 edges! In reality we

    can pick a lower number, e.g. 1 import math for i in range(5, 10): print i, math.factorial(i) 5 120 8 40,320 6 720 9 362,880 7 5,040
  9. Identifying a Polyomino But remember the rules def __hash__(self): p

    = self, k = self.key() for _ in range(3): p = self.rotate().translate() k = min(k, p.key()) return k def key(self): return hash(self.squares)
  10. Using the set helpers How does the set `in` operator

    work? hash(a) == hash(b) and (a is b or id(a) == id(b))

Other Decks in Programming