Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

print(E33, and, W01+:, end= ) print(isNeighbor(E33,W01)) print(E33, and, W11+:, end= ) print(isNeighbor(E33,W11)) print(E33, and, E32+:, end= ) print(isNeighbor(E33,E32)) print(E32, and, W03+:, end= ) print(isNeighbor(E32,W03)) print(W11,

image text in transcribed

print("E33", "and", "W01"+":", end=" ") print(isNeighbor("E33","W01"))

print("E33", "and", "W11"+":", end=" ") print(isNeighbor("E33","W11"))

print("E33", "and", "E32"+":", end=" ") print(isNeighbor("E33","E32"))

print("E32", "and", "W03"+":", end=" ") print(isNeighbor("E32","W03"))

print("W11", "and", "E32"+":", end=" ") print(isNeighbor("W11","E32"))

print("W22", "and", "E32"+":", end=" ") print(isNeighbor("W22","E32"))

print() print("(d) The graph:") G = genGraph(setLegalStates)

for n in G.keys(): print(n+":", G[n]) print()

The expected outputs are:

(a) All possible states:

E00 E01 E02 E03

E10 E11 E12 E13

E20 E21 E22 E23

E30 E31 E32 E33

W00 W01 W02 W03

W10 W11 W12 W13

W20 W21 W22 W23

W30 W31 W32 W33

(b) All legal states:

E01 E02 E03 E11 E22 E30

E31 E32 E33 W01 W02 W03

W11 W22 W30 W31 W32 W33

(c) Checking whether direct neighbors:

E33 and W02: True

E33 and W01: True

E33 and W11: True

E33 and E32: False

E32 and W03: True

W11 and E32: True

W22 and E32: False

(d) The graph:

E01: ['W33']

E02: ['W32', 'W33']

E03: ['W31', 'W32']

E11: ['W32', 'W33']

E22: ['W22', 'W31']

E30: []

E31: ['W03', 'W22']

E32: ['W02', 'W03', 'W11']

E33: ['W01', 'W02', 'W11']

W01: ['E33']

W02: ['E32', 'E33']

W03: ['E31', 'E32']

W11: ['E32', 'E33']

W22: ['E22', 'E31']

W30: []

W31: ['E03', 'E22']

W32: ['E02', 'E03', 'E11']

W33: ['E01', 'E02', 'E11']

(e) [5 marks] Which legal states are not on any shortest path and why? Print your answer after the code for (a)-(d).

(f) [5 marks] What is the length of a shortest path in terms of the number of states? Give such a path. Print your answer after your answer for (e).

1. [40 marks] (The robber-police-river-crossing problem revisited) There is a group of three policemen and another group of three robbers. They cross a river using a boat which has a capacity of two people. The boat must be operated by anyone of the six people. A main constraint is that on each side of the river, the number of the policemen, if any, must not be less than the number of the robbers. Otherwise, the outnumbered policemen will be wiped out by the robbers. Starting with the six people on one side of the river (say east), the problem is to bring all of them safely to the other side with a minimum number of trips. In the data abstraction phase, we model each state by a 3-tuple: (the side of the river where the boat is, the number of the policemen at that side of the river, the number of the robbers at that side of the river). The side of the river could be E/W (east/west). The number of the policemen/robbers ranges from 0 to 3, inclusive. To implement the state, we use a string, such as "E33" which means that all the policemen and robbers are on the east side. This is incidentally the initial state and clearly "W33" is the destination state. As another example, "E21" means that the boat is on the east side and there are 2 policemen and I robber (and I policeman and 2 robbers on the west side). You are now asked to implement the following four functions using Python. (a) [6 marks] Generating all possible states def genstates(): A function to generate all possible states for this problem Input: None Output: Return a tuple of all possible states. (b) [6 marks] Generating all possible legal states def gen Legal states (setStates) : A function to generate all legal states Input: setStates is a tuple of all possible states. Output: A tuple of all legal states (e) [12 marks] Determining whether two legal states are direct neighbors. Provide sufficient explanation of the algorithm you implement in your code as doctring after the function signature. def isNeighbor (astate, State) : A function to determine whether two legal states are direct neighbors (i.e. there is a link between them in the graph.) Input: a state and bstate are two legal states. Output: Return True if they are direct neighbors False, otherwise (d) [6 marks] Generating a graph. If you cannot implement is Neighbor in the last part, just use is Neighbor to implement genGrapho. In this case. we will just examine your code without running it. def genGraph (S): A function to generate a graph from all legal states Input: S is a tuple of all legal states. Output: Return a graph based on S and the constraints given in the problem. Please include the code below to test your functions. setAllstates genstates () print(" (a) All possible states:") Count = 0 for in BetAllstates: print ( s end Count + 1 it count 4 0 : print print print(" (b) All legal states:") setLegalstates = genLegalstates (setAllstates) count Cor Sin Set Legalstates: prints, end ) count + 1 if count 6 0 : print) print print(" (c) Checking whether direct neighbors:") print("E33""and" "WO2+ end ) print (isNeighbor ("E33","WO2")) and print eghbor ( 3--01) PELE (E . ang box-33, print("33",and", -232 , peint eighbor ( 33","32"> print (232", "and"," 3 ghbor ( printi ":" 2 W . end- end" PELLEN - -- int printe -and- r 2 3 .232 print d) The graph ) G- genGraph (setLegalStates) tors in G.laya(): The expected se 1. [40 marks] (The robber-police-river-crossing problem revisited) There is a group of three policemen and another group of three robbers. They cross a river using a boat which has a capacity of two people. The boat must be operated by anyone of the six people. A main constraint is that on each side of the river, the number of the policemen, if any, must not be less than the number of the robbers. Otherwise, the outnumbered policemen will be wiped out by the robbers. Starting with the six people on one side of the river (say east), the problem is to bring all of them safely to the other side with a minimum number of trips. In the data abstraction phase, we model each state by a 3-tuple: (the side of the river where the boat is, the number of the policemen at that side of the river, the number of the robbers at that side of the river). The side of the river could be E/W (east/west). The number of the policemen/robbers ranges from 0 to 3, inclusive. To implement the state, we use a string, such as "E33" which means that all the policemen and robbers are on the east side. This is incidentally the initial state and clearly "W33" is the destination state. As another example, "E21" means that the boat is on the east side and there are 2 policemen and I robber (and I policeman and 2 robbers on the west side). You are now asked to implement the following four functions using Python. (a) [6 marks] Generating all possible states def genstates(): A function to generate all possible states for this problem Input: None Output: Return a tuple of all possible states. (b) [6 marks] Generating all possible legal states def gen Legal states (setStates) : A function to generate all legal states Input: setStates is a tuple of all possible states. Output: A tuple of all legal states (e) [12 marks] Determining whether two legal states are direct neighbors. Provide sufficient explanation of the algorithm you implement in your code as doctring after the function signature. def isNeighbor (astate, State) : A function to determine whether two legal states are direct neighbors (i.e. there is a link between them in the graph.) Input: a state and bstate are two legal states. Output: Return True if they are direct neighbors False, otherwise (d) [6 marks] Generating a graph. If you cannot implement is Neighbor in the last part, just use is Neighbor to implement genGrapho. In this case. we will just examine your code without running it. def genGraph (S): A function to generate a graph from all legal states Input: S is a tuple of all legal states. Output: Return a graph based on S and the constraints given in the problem. Please include the code below to test your functions. setAllstates genstates () print(" (a) All possible states:") Count = 0 for in BetAllstates: print ( s end Count + 1 it count 4 0 : print print print(" (b) All legal states:") setLegalstates = genLegalstates (setAllstates) count Cor Sin Set Legalstates: prints, end ) count + 1 if count 6 0 : print) print print(" (c) Checking whether direct neighbors:") print("E33""and" "WO2+ end ) print (isNeighbor ("E33","WO2")) and print eghbor ( 3--01) PELE (E . ang box-33, print("33",and", -232 , peint eighbor ( 33","32"> print (232", "and"," 3 ghbor ( printi ":" 2 W . end- end" PELLEN - -- int printe -and- r 2 3 .232 print d) The graph ) G- genGraph (setLegalStates) tors in G.laya(): The expected se

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions