You are on page 1of 2

Invariants

Monday, December 22, 2014


8:42 PM

An expression/quantity is an invariant (under a looping or an assignment operation, in


general transformations) if under changes of program state "transformations" the
expression/quantity is unchanged.

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

if E[transformation] = E upon carrying out the


transformation for any set of values
then E is invariant.

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.

Some more examples:


(m + 3*n)[m, n := m + 3, n - 1] = (m + 3) + 3(n - 1) = m + 3 +
3n - 3 = m + 3*n - invariant

(m + n + p)[m, n, p := 3*n, m+3, n -1] = 3*n + (m + 3) + (n - 1)


= 4*n + m + 2 - not
when m , n, p := 1, 2, 3
m + n + p = 6 not equal to 4*n + m + 2 = 11

Each cut corresponds to


p += 1
c += 1

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

From the base case, p - c = 1. and since p - c is invariant then


when p = n, c = n - 1 (induction)
base case is important otherwise the reduction will never halt.
Knockout tournament
1234 initial number of players.
each round a player is laid off. thus

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.

# of empty large boxes


# of empty medium boxes

total # of empty boxes = b + m + s = 102


11 - m0/8 + m0 - s0/8 + s0 = 102
7/8 (m0 + s0) = 91

m0 + s0 = 104

Algorithmic problem solving Page 1

total # of boxes = b0 + m0 + s0 = 115

solution 2: using invariants. why solution exists is clear.


e - empty boxes
f - full boxes
Note that the total # of boxes = e + f since two boxes can only have one of the
two states. i. e. a box is empty xor filled.
(This follows from theorem |A |+ | B|= |A u B| + |A intersection B|
since intersection is null, |A| + |B| = |A u B|)
Act of putting boxes
e, f := e - k , f + k cross out k empty boxes to be filled, then
e, f := e + 8k, f
put 8 empty boxes in the k boxes marked, # of full boxes
since we are putting empty boxes to boxes already marked filled.

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.

Black and white balls

Algorithmic problem solving Page 2

You might also like