You are on page 1of 2

Emil Sekerinski: Computational Problem Solving, Draft, September 2010!

Chapter 3

Problem Solving

The key to solving a "real-world" algorithmic problem is to represent it appropriately:


a good representation abstracts from irrelevant details but keeps essential ones. The
difficulty is deciding what is irrelevant and what is essential! Mathematical puzzles
illustrate this particularly well.

Chocolate Bars
Consider the problem of breaking a rectangular chocolate bar
with m × n squares into individual squares. Each horizontal or
vertical cut breaks one piece into two. The picture to the right
shows a 3 × 4 bar after three cuts. How many cuts are needed
to completely cut the bar into individual squares? What is the
best way to do so?

The first step is to introduce variables for the 0<m∧0<n


relevant quantities of the problem. As we are
interested in the number of cuts, let c stand for the p := 1
number of cuts so far, with an initial value of 0. c := 0
The algorithm ends when we have m × n pieces, c=p–1∧
so let p stand for the current number of pieces, 1 ≤ p ≤ m×n
which is initially 1. In each step, we cut a piece, p<m×n
which means c is incremented by 1. How does
this affect p? Cutting any piece gives two pieces, +
which means p is incremented by 1 as well. With
p := p + 1
the initial condition, this means that c = p – 1 is
c := c + 1
an invariant. As finally p is m × n, we know that
finally c must be m × n – 1. This is independent
of the order we choose to cut the pieces! The p = m×n ∧
annotated algorithm is to the right. c=m×n–1


17
Emil Sekerinski: Computational Problem Solving, Draft, September 2010!

The key here was not to represent the shapes of the pieces in intermediate states.
This would have made the solution only more complicated.

River Crossing
A farmer needs to bring a wolf, a goat, and a cabbage across the river. The boat is
tiny and can only carry only one of them at a time. If he leaves the wolf and the goat
alone together, the wolf will eat the goat. If he leaves the goat and the cabbage alone
together, the goat will eat the cabbage. How can he bring all three safely across the
river?

For representing the problem, we observe that there are four individuals, the farmer,
the wolf, the goat, and the cabbage that change positions: they are either on the left
bank, in the boat, or on the right bank. In the boat the goat and cabbage are safe, as
the farmer is with them, so the only critical positions are the left and right bank.
Thus we introduce variables f, w, g, c –for the farmer, wolf, goat, cabbage– with val-
ues L, R –for the left and right bank. Initially all of f, w, g, c are L and finally they
should be R. The constraint that the wolf must not be left with the goat alone is ex-
pressed by:

# f=g=w∨g≠w

The constraint that goat and cabbage must not be left alone is expressed by:

# f=g=c∨g≠c

These constraints have to hold during the computation at all states. f g c w


Such a restriction on variables is called a data invariant. Unlike a loop L L L L
invariant, which has to only at the beginning and end of a loop, a
L L L R
data invariant must always hold.
L L R L
With four variable and two possible values for each, there are 24 = 16 L L R R
possible states. Let us enumerate all states that satisfy the data in- L R L L
variants. This technique is called state exploration. This results in ten
R L R R
different states that are shown to the right.
R R L L
R R L R
R R R L
R R R R

18

You might also like