Professional Documents
Culture Documents
Some notation:
(p-c)[p, c := p + 1, c + 1] = (p + 1) - (c+1) = p - c
thus the expression (p-c) is invariant.
in general
Example:
the quantity m + 3*n is invariant under
m, n= m+3, n-1. i.e. m + 3*n = const.
Chocolate problem:
Determine the number of cuts needed to cut a chocolate into gridless pieces.
relevant info:
1. vertical and horizontal grids
2. each cut is made along a groove. thus it always happens that when a cut is made
a piece is divided into two and nothing else. p := p +1 when c := c + 1.
Each cut increases the number of chocolates by 1.
Initially,
p = 1, c = 0
Where p denotes pieces and c cuts.
p, c := p+1, c+1
p` - c` = p + dp - (c + dc) = p - c + (dp - dc) = p - c.
since dp = dc = 1.
hence p-c is invariant
n - # of remaining players.
r - total # of rounds.
k - # of rounds held
assuming n is even
n, r := n - k, r + k
n + r is invariant!
winner is decided when n = 1
initially n + r = 1234, therefore r = 1233!
(makes sense: 1233 rounds will eliminate 1233 players)
Empty boxes
Eleven large (empty) boxes from which 8 are arbitrarily selected. into each 8 medium boxes
placed.
into 8 randomly chosen medium boxes 8 small boxes placed. How many boxes are left.
Solution 1: w/o using invariants. this solution does not express succinctly why an answer exists
for any choice of m0 and s0 .
b0 := 11
b := b0 - m0/8
m := m0 - s0/8
for any p, c.
m0 + s0 = 104
unchanged
thus
e, f := e + 7k , f + k
therefore
e - 7f is an invariant (the invariant must not depend on k -- the choice of k being arbitrary)
initially e = 11 f = 0. Thus e - 7f = 11.
final value of e is 102 as given.
102 - 7f = 11
91 = 7f
f = 13.
total boxes = e + f = 115.
(the # of empty boxes and filled boxes are disjoint)
Tumblers
concept of a Boolean valued invariant
u - number of upside down tumbler
three ops:
u := u + 2 (turning two right side down tumbler up)
u := u -2
(turning two upside down tumbler down)
u := u
(turning tumblers with different orientation)
third op -> better use 'skip' keyword since we want to explicitly denote
that it does not depend on any variable.
u := u + 2
u := u -2
skip
invariant: parity -> true if u is even (i.e. all upside down tumblers can be paired)
false if u is odd.
for x in three ops:
(u even)[x] = u even.
(u odd)[x] = u odd.
since whenever we subtract 2, we end up with even same with adding. likewise
with u odd.
base case u = 0 which is even. Thus the problem is solvable whenever the initial u is even.
since if u is odd we can never reach 0 which is even.