27
Algorithmic Problem Solving Lecture 3 River Crossing Problems

Algorithmic Problem Solving Lecture 3 River Crossing Problems

Embed Size (px)

Citation preview

Page 1: Algorithmic Problem Solving Lecture 3 River Crossing Problems

Algorithmic Problem Solving

Lecture 3

River Crossing Problems

Page 2: 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.

Page 3: Algorithmic Problem Solving Lecture 3 River Crossing Problems

Drawing a cube

0 1

A line 1st digit0=left, 1=right

00 10

01 11A square.2nd digit0=bottom1=top

000 100

010 110

001 101

011 111

A cube.3rd digit0=front,1=backFor 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.

Page 4: Algorithmic Problem Solving Lecture 3 River Crossing Problems

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 1st, 2nd, 3rd tumbler.

• as e.g. 010 meaning (1st 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.

Page 5: Algorithmic Problem Solving Lecture 3 River Crossing Problems

State Transition Diagram for Tumble Problem 1

• 000 • 100

• 010 • 110

• 001 • 101

• 011 • 111

With 3 tumblers, only 4 allowable states out of 8.Which 4 depends on the initial configuration.Where is the MISSING TRANSITION? MISSING LINE?

Page 6: Algorithmic Problem Solving Lecture 3 River Crossing Problems

State Transition Diagram for Tumble Problem 2

000 100

010 110

001 101

011 111

With 3 tumblers, only 4 allowable states out of 8.Which 4 depends on the initial configuration

Page 7: Algorithmic Problem Solving Lecture 3 River Crossing Problems

0000 1000

0100 1100

0010 1010

0110 1110

0001 1001

0101 1101

0011 1011

0111 1111

Page 8: Algorithmic Problem Solving Lecture 3 River Crossing Problems

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

Page 9: Algorithmic Problem Solving Lecture 3 River Crossing Problems

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

Page 10: Algorithmic Problem Solving Lecture 3 River Crossing Problems

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?

Page 11: Algorithmic Problem Solving Lecture 3 River Crossing Problems

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?

Page 12: Algorithmic Problem Solving Lecture 3 River Crossing Problems

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.

Page 13: Algorithmic Problem Solving Lecture 3 River Crossing Problems

All Possible States 1 – which states are allowed

F G C W

L L L L

L L L R

L L R L

L L R R

L R L L

L R L R

L R R L

L R R R

L L L L

L L L R

L L R L

L L R R

L R L L

L R L R

L R R L

L R R R

Page 14: Algorithmic Problem Solving Lecture 3 River Crossing Problems

All Allowable States 1F G C W

L L L L allowed

L L L R allowed

L L R L allowed

L L R R allowed

L R L L allowed

L R L R NOT allowed

L R R L NOT allowed

L R R R NOT allowed

L L L L NOT allowed

L L L R NOT allowed

L L R L NOT allowed

L L R R allowed

L R L L allowed

L R L R allowed

L R R L allowed

L R R R allowed

Page 15: Algorithmic Problem Solving Lecture 3 River Crossing Problems

State Transition Diagram 1

LLLL

RRLL

LRLL

RRRL RRLR

LLRL LLLR

RLRR

LLRR

RRRR

Representation (farmer, goat, cabbage, wolf)Notice the symmetry of the solution?

Page 16: Algorithmic Problem Solving Lecture 3 River Crossing Problems

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.

Page 17: Algorithmic Problem Solving Lecture 3 River Crossing Problems

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.

Page 18: Algorithmic Problem Solving Lecture 3 River Crossing Problems

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?

Page 19: Algorithmic Problem Solving Lecture 3 River Crossing Problems

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.

Page 20: Algorithmic Problem Solving Lecture 3 River Crossing Problems

All Possible States – which states are allowed

F A B B

L L L L

L L L R

L L R L

L L R R

L R L L

L R L R

L R R L

L R R R

L L L L

L L L R

L L R L

L L R R

L R L L

L R L R

L R R L

L R R R

Page 21: Algorithmic Problem Solving Lecture 3 River Crossing Problems

All Allowable States 2F G C W

L L L L allowed

L L L R allowed

L L R L allowed

L L R R allowed

L R L L allowed

L R L R NOT allowed

L R R L NOT allowed

L R R R NOT allowed

L L L L NOT allowed

L L L R NOT allowed

L L R L NOT allowed

L L R R allowed

L R L L allowed

L R L R allowed

L R R L allowed

L R R R allowed

Page 22: Algorithmic Problem Solving Lecture 3 River Crossing Problems

0000 1000

0100 1100

0010 1010

0110 1110

0001 1001

0101 1101

0011 1011

0111 1111

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

Page 23: Algorithmic Problem Solving Lecture 3 River Crossing Problems

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?

Page 24: Algorithmic Problem Solving Lecture 3 River Crossing Problems

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?

Page 25: Algorithmic Problem Solving Lecture 3 River Crossing Problems

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.

Page 26: Algorithmic Problem Solving Lecture 3 River Crossing Problems

Complete Solution

Page 27: Algorithmic Problem Solving Lecture 3 River Crossing Problems

Last weeks problems

• Go over some of the problems• Are there any questions?