in variants

2
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. 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 # of empty large boxes m := m0 -s0/8 # 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 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. Example: the quantity m + 3*n is invariant under m, n= m+3, n-1. i.e. m + 3*n = const. Some notation: (p-c)[p, c := p + 1, c + 1] = (p + 1) - (c+1) = p - c for any 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. 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 Invariants Monday, December 22, 2014 8:42 PM Algorithmic problem solving Page 1

Upload: ron-medina

Post on 18-Jul-2016

212 views

Category:

Documents


0 download

DESCRIPTION

notes on ch2 of algorithmic problem solving by r. backhouse

TRANSCRIPT

Page 1: In Variants

Chocolate problem:Determine the number of cuts needed to cut a chocolate into gridless pieces.relevant info:1. vertical and horizontal grids2. each cut is made along a groove. thus it always happens that when a cut is madea 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.

Each cut corresponds top += 1c += 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 thenwhen p = n, c = n - 1 (induction)

base case is important otherwise the reduction will never halt.

Knockout tournament1234 initial number of players.each round a player is laid off. thus

n - # of remaining players. r - total # of rounds.k - # of rounds heldassuming n is even

n, r := n - k, r + kn + r is invariant!

winner is decided when n = 1initially n + r = 1234, therefore r = 1233!

(makes sense: 1233 rounds will eliminate 1233 players)

Empty boxesEleven 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 := 11b := b0 - m0/8 # of empty large boxesm := m0 - s0/8 # of empty medium boxes

total # of empty boxes = b + m + s = 10211 - m0/8 + m0 - s0/8 + s0 = 1027/8 (m0 + s0) = 91

m0 + s0 = 104

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.

Example:the quantity m + 3*n is invariant underm, n= m+3, n-1. i.e. m + 3*n = const.

Some notation:(p-c)[p, c := p + 1, c + 1] = (p + 1) - (c+1) = p - c for any p, c.thus the expression (p-c) is invariant.

in general

if E[transformation] = E upon carrying out thetransformation for any set of values

then E is invariant.

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, 3m + n + p = 6 not equal to 4*n + m + 2 = 11

InvariantsMonday, December 22, 2014

8:42 PM

Algorithmic problem solving Page 1

Page 2: In Variants

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

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

e - empty boxesf - full boxesNote that the total # of boxes = e + f since two boxes can only have one of thetwo 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 boxese, 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 unchanged since we are putting empty boxes to boxes already marked filled.

thuse, f := e + 7k , f + k

thereforee - 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 = 1191 = 7ff = 13.

total boxes = e + f = 115.(the # of empty boxes and filled boxes are disjoint)

Tumblersconcept 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 + 2u := u -2skip

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. likewisewith 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