Presentation is loading. Please wait.

# Algorithmic Problem Solving Lecture 3 River Crossing Problems.

## Presentation on theme: "Algorithmic Problem Solving Lecture 3 River Crossing Problems."— Presentation transcript:

Algorithmic Problem Solving Lecture 3 River Crossing Problems

Review of last lecture State transition diagram of tumbler problem Abstraction, variables to describe problem. Assignments, describe state changes. Invariants, what does not change. Make use of initial conditions.

Drawing a cube 01 A line 1 st digit 0=left, 1=right 0010 0111 A square. 2 nd digit 0=bottom 1=top 000100 010110 001101 011111 A cube.3 rd digit0=front,1=back For example 010 = Left, top, front. Think of coordinates in 3 dimensions The process is as follow; Draw a line – label each end 0, 1. Make a copy of this and join corresponding vertexes. Add a new coordinate 0 or 1. Repeat this process.

Connection to Tumbler Problem In the tumbler problem, we could represent the states of the tumblers. 1 = correct way up, 0 = upside down. The position being 1 st, 2 nd, 3 rd tumbler. as e.g. 010 meaning (1 st tumbler is upside down, 2nd tumbler is correct way up, 3rd tumbler is upside down). A vertex is a state and a line between states is an action or a transition. The whole picture is a state transition diagram.

State Transition Diagram for Tumble Problem 1 000100 010110 001101 011111 With 3 tumblers, only 4 allowable states out of 8. Which 4 depends on the initial configuration. Where is the MISSING TRANSITION? MISSING LINE?

State Transition Diagram for Tumble Problem 2 000100 010110 001101 011111 With 3 tumblers, only 4 allowable states out of 8. Which 4 depends on the initial configuration

00001000 01001100 00101010 01101110 00011001 01011101 00111011 01111111

A better representation U = number of upside down tumblers. Parity = true if U is even, else is false. Two states ONLY. Parity.U = true or false. Two state transitions, which return you to the same state. There is no transition between states. This is one diagram, not two separate ones. Parity.U = true Parity.U = false Action of inverting a pair of tumblers Action of inverting a pair of tumblers

River Crossing Problems Brute force search means systematically trying all possibilities. Problem decomposition Naming of items in a problem State State transition State transition diagram

Farmer, Goat, Cabbage, Wolf A farmer wants to take a goat g, a cabbage c, and a wolf w across a river. The boat is only large enough to take the farmer and one item at a time. Also, the goat cannot be left alone with the cabbage, otherwise the goat will eat the cabbage. The wolf cannot be left along with the goat, otherwise the wolf will eat the goat. How can the farmer do this? Do you understand the problem?

Questions Can you think of a good representation for this problem? How may states are there in total (ignoring the constrains concerning the goat) How many states are there, taking the constraints into contestation? Can you solve the problem?

Constraints An item is either on the left or the right river bank, i.e. one of two values (R, L) therefore think of Boolean algebra. f=g=c V g!=c i.e. the f, g and c are all on same side or g and c are on different banks. f=g=w V g!=w i.e. the f, g and w are all on same side or g and w are on different banks.

All Possible States 1 – which states are allowed FGCW LLLL LLLR LLRL LLRR LRLL LRLR LRRL LRRR LLLL LLLR LLRL LLRR LRLL LRLR LRRL LRRR

All Allowable States 1 FGCW LLLL allowed LLLR allowed LLRL allowed LLRR allowed LRLL allowed LRLR NOT allowed LRRL NOT allowed LRRR NOT allowed LLLL NOT allowed LLLR NOT allowed LLRL NOT allowed LLRR allowed LRLL allowed LRLR allowed LRRL allowed LRRR allowed

State Transition Diagram 1 LLLL RRLL LRLL RRRLRRLR LLRLLLLR RLRR LLRR RRRR Representation (farmer, goat, cabbage, wolf) Notice the symmetry of the solution?

Brute Force Brute force – e.g. think about chess. State space explosion, combinatorial explosion N individuals, 2 ^ N (two to the power of N) size of state space. This grows too fast.

Naming Distinct names are given to the 4 items. There is no distinction between the wolf and cabbage (look at the constraints). The two solutions are symmetrical if move wolf or cabbage first. So why give cabbage and wolf separate names? This is part of the abstraction process.

Restatement of problem A farmer wants to take an A and two Bs across a river. The boat can only take the farmer and one item at a time. Also an A cannot be left alone with a B when the farmer is not present. How can the farmer do this?

Constraints 2 An item is either on the left or the right river bank, i.e. one of two values (R, L) therefore think of Boolean algebra. f=A=B V A!=B i.e. the f, A and B are all on same side or A and B are on different banks.

All Possible States – which states are allowed FABB LLLL LLLR LLRL LLRR LRLL LRLR LRRL LRRR LLLL LLLR LLRL LLRR LRLL LRLR LRRL LRRR

All Allowable States 2 FGCW LLLL allowed LLLR allowed LLRL allowed LLRR allowed LRLL allowed LRLR NOT allowed LRRL NOT allowed LRRR NOT allowed LLLL NOT allowed LLLR NOT allowed LLRL NOT allowed LLRR allowed LRLL allowed LRLR allowed LRRL allowed LRRR allowed

00001000 01001100 00101010 01101110 00011001 01011101 00111011 01111111 A hypercube for farmer problem Getting all 4 items from LLLL to RRRR (i.e. from 0000 to 1111) i.e. from left, bottom, front, inside to Right, top, back outside

Jealous Couples Three couples (husband and wife) wish to cross a river. There is one boat which can carry two people maximum. The husbands are so jealous of each other, that none is willing to allow their wife to be with another man if they are not present too. How can all 3 couples get across the river?

State Describes where the individuals are. Left | Boat | Right describes who is on the left bank, in the boat or on the right bank. 3H||3W = 3 husbands on left bank and 3 wives on right bank What is the start/end state?

Actions An action is when individuals travel across the river. 3H|2W|1W the action of transporting two wives across the river. Potential ambiguity as we do not say the direction of movement.

Complete Solution

Last weeks problems Go over some of the problems Are there any questions?

Download ppt "Algorithmic Problem Solving Lecture 3 River Crossing Problems."

Similar presentations

Ads by Google