0% found this document useful (0 votes)
118 views18 pages

Backhouse Problems

The document introduces principles of algorithm construction through fun problems like games and puzzles. It discusses using assignments and invariants to solve the chocolate bar cutting problem in 3 cuts. It also covers Hoare triples, problem decomposition, winning strategies for impartial two-person games by leaving the opponent in a losing position, and defining functions to characterize winning positions in sum games.

Uploaded by

Jason Smith
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views18 pages

Backhouse Problems

The document introduces principles of algorithm construction through fun problems like games and puzzles. It discusses using assignments and invariants to solve the chocolate bar cutting problem in 3 cuts. It also covers Hoare triples, problem decomposition, winning strategies for impartial two-person games by leaving the opponent in a losing position, and defining functions to characterize winning positions in sum games.

Uploaded by

Jason Smith
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

1

Algorithmic Problem Solving

Roland Backhouse January 29, 2004

Outline

Goal Introduce principles of algorithm construction Vehicle Fun problems (games, puzzles)

Chocolate-bar Problem

How many cuts are needed to cut a chocolate bar into all its individual pieces?

Assignment and Invariants

Let p be the number of pieces, and c be the number of cuts.

The process of cutting the bar is modelled by: p , c := p+1 , c+1 . We observe that (pc) is an invariant. That is, (pc)[p , c := p+1 , c+1] = (p+1) (c+1) = pc

Initially, pc is 1. So, number of cuts is always one less than the number of pieces.

Hoare Triples

Eg. Jealous couples: Three couples Aa, Bb and Cc. One boat which can carry at most two people. Wives (a, b and c) may not be with a man (A, B and C) unless their husband is present.

Construct a sequence of actions S0 satisfying { AaBbCc | } S0 { | AaBbCc } .

Problem Decomposition
Exploit symmetry!

Decompose into { AaBbCc | } S1 ; { ABC | abc } S2 ; { abc | ABC } S3 { | AaBbCc }

(Impartial, Two-Person) Games

Assume number of positions is nite. Assume game is guaranteed to terminate no matter how the players choose their moves. Game is lost when a player cannot move.

A position is losing if every move is to a winning position. A position is winning if there is a move to a losing position. Winning strategy is to maintain the invariant that ones opponent is always left in a losing position.

Winning Strategy

Maintain the invariant that ones opponent is always left in a losing position. { losing position, and not an end position } make an arbitrary (legal) move ; { winning position, i.e. not a losing position } apply winning strategy { losing position }

Example Winning Strategy

One pile of matches. Move: remove one or two matches. Winning strategy is to maintain the invariant that ones opponent is always left in a position where the number of matches is a multiple of 3. { n is a multiple of 3, and n = 0 } if 1 n n := n1 ; 2 2 n n := n2 { n is not a multiple of 3 } n := n (n mod 3) { n is a multiple of 3 }

10

Sum Games

Given two games, each with its own rules for making a move, the sum of the games is the game described as follows. For clarity, we call the two games the left and the right game. A position in the sum game is the combination of a position in the left game, and a position in the right game. A move in the sum game is a move in one of the games.

11

Sum Games (cont)

Dene two functions L and R, say, on left and right positions, respectively, in such a way that a position (l,r) is a losing position exactly when L.l = R.r. How do we specify the functions L and R?

12

Sum Games (Cont)


First: L and R have equal values on end positions. Second: { L.l = R.r (l is not an end position r is not an end position) } if l is not an end position change l 2 r is not an end position change r { L.l = R.r } Third, { L.l = R.r } apply winning strategy { L.l = R.r }

13

Sum Games (cont)

Satisfying the rst two requirements: For end positions l and r of the respective games, L.l = 0 = R.r. For every l such that there is a move from l to l in the left game, L.l = L.l . Similarly, for every r such that there is a move from r to r in the right game, R.r = R.r .

14

Winning strategy (third requirement): { L.l = R.r } if L.l < R.r change r 2 R.r < L.l change l { L.l = R.r } .

15

Winning strategy (third requirement): { L.l = R.r } if L.l < R.r change r 2 R.r < L.l change l { L.l = R.r } . For any number m less than R.r, it is possible to move from r to a position r such that R.r = m. (Similarly, for left game.)

16

Summary of Requirements

Satisfying the rst two requirements: For end positions l and r of the respective games, L.l = 0 = R.r. For every l such that there is a move from l to l in the left game, L.l = L.l . Similarly, for every r such that there is a move from r to r in the right game, R.r = R.r . For any number m less than R.r, it is possible to move from r to a position r such that R.r = m. (Similarly, for left game.)

17

MEX Function

Let p be a position in a game G. The mex value of p, denoted mexG .p, is dened to be the smallest natural number, n, such that There is no legal move in the game G from p to a position q satisfying mexG .q = n. For every natural number m less than n, there is a legal move in the game G from p to a position q satisfying mexG .q = m.

18

Characterising Features

Non-mathematical, easily explained problems (requiring mathematical solution) Minimal notation. Challenging problems. Simultaneous introduction of programming constructs and principles of program construction.

You might also like