Lecture Notes 28-29 Sept 10 (1)

download Lecture Notes 28-29 Sept 10 (1)

of 2

Transcript of Lecture Notes 28-29 Sept 10 (1)

  • 8/8/2019 Lecture Notes 28-29 Sept 10 (1)

    1/2

    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. Thedifficulty is deciding what is irrelevant and what is essential! Mathematical puzzlesillustrate this particularly well.

    Chocolate Bars

    Consider the problem of breaking a rectangular chocolate barwith mn squares into individual squares. Each horizontal orvertical cut breaks one piece into two. The picture to the rightshows a 3 4 bar after three cuts. How many cuts are neededto completely cut the bar into individual squares? What is thebest way to do so?

    The first step is to introduce variables for therelevant quantities of the problem. As we areinterested in the number of cuts, let c stand for thenumber of cuts so far, with an initial value of 0.The algorithm ends when we have mn pieces,so let p stand for the current number of pieces,which is initially 1. In each step, we cut a piece,which means c is incremented by 1. How doesthis affect p? Cutting any piece gives two pieces,which means p is incremented by 1 as well. With

    the initial condition, this means that c = p 1 isan invariant. As finally p is mn, we know thatfinally c must be mn 1. This is independentof the order we choose to cut the pieces! Theannotated algorithm is to the right.

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

    17

    p := p + 1c := c + 1

    p = mnc = mn 1

    c = p 1 1 pmn

    0 < m 0 < n

    p := 1c := 0

    +

    p

  • 8/8/2019 Lecture Notes 28-29 Sept 10 (1)

    2/2

    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 istiny and can only carry only one of them at a time. If he leaves the wolf and the goatalone together, the wolf will eat the goat. If he leaves the goat and the cabbage alonetogether, the goat will eat the cabbage. How can he bring all three safely across theriver?

    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 leftbank, in the boat, or on the right bank. In the boat the goat and cabbage are safe, asthe 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 off, w, g, c are L and finally theyshould be R. The constraint that the wolf must not be left with the goat alone is ex-pressed by:

    # f=g = wgw

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

    # f=g = cgc

    These constraints have to hold during the computation at all states.

    Such a restriction on variables is called a data invariant. Unlike a loopinvariant, which has to only at the beginning and end of a loop, adata invariant must always hold.

    With four variable and two possible values for each, there are 24 = 16possible states. Let us enumerate all states that satisfy the data in-variants. This technique is called state exploration. This results in tendifferent states that are shown to the right.

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

    18

    f g c w

    L L L L

    L L L R

    L L R L

    L L R R

    L R L L

    R L R R

    R R L L

    R R L R

    R R R L

    R R R R