Solution

KING SAUD UNIVERSITY
COLLEGE OF COMPUTER AND INFORMATION SCIENCES
COMPUTER SCIENCE DEPARTMENT
CSC 361
1st Semester 1429/1430
MIDTERM I
MODEL ANSWER
---------------------------------------------------------------------------------------Question 1. [10 marks] Answer by true or false.
1. Breadth first search is not optimal in case actions have different cost. …..true
2. Greedy search is a special case of uniform cost search. ………………….false
3. If uniform cost search is complete, it is also optimal. ……………………true
4. If a heuristic function h() is admissible, greedy search is optimal. ……….false
Question 2. [10 marks] A robot has to move in the environment represented in the figure
below in order to reach its goal G. The starting point S can be any position in the
environment.
1
A
B
2
3
4 5
6
7
8
9
10 11
S
C
D
E
G
Give a suitable representation (state representation, initial state, goal state, operators, and path
cost).
Solution:
-
State representation: coordinates (row, column) of the robot in the grid
-
Initial state: (B, 1)
-
Goal state: (C, 9)
-
Operators: Go-up, Go-down, Go-left, Go-right
-
Path cost: number of steps or moves to reach the goal state.
Question 3. [15 marks] Consider the following map. The task is to color the map using the
four colors red, blue, yellow, and green, such that no two adjacent regions take the same
color.
3
6
4
5
2
1
Give a suitable representation (state representation, initial state, goal state, operators, and path
cost).
Solution:
-
State representation: a vector of size 6, each cell (region) is assigned a color from the
following set: (red, blue, green, yellow).
-
Initial state: (-, -, -, -, -, -) no region is assigned a color.
-
Goal state: Each region is assigned a color so that two adjacent regions take different
colors.
-
Operators: Assign a color to a region.
-
Path cost: number of steps (assignments) to reach the goal state.
Question 4. [25 marks] Given the following graph where S is the starting node and G is the
goal node. Numbers related to arcs between nodes indicate step costs.
Trace Graph-Search algorithm using BFS and DFS strategies. In each case, show the order in
which nodes are added to the fringe as well as the generated tree and the solution path. Do
not add a state as a leaf if that state is on the path from the root to the current node of the
generated tree. Nodes are added to the tree in alphabetical order. Indicate if found solutions
are optimal.
Solution:
-
BFS
S
1
2
5
A
B
3
2
1
2
D
1
G
4
G
5
1
C
Found solution is optimal: S → A → G, cost: 4.
-
DFS
S
1
2
A
5
B
2
1
D
2
3
G
1
C
4
3
G
5
Found solution is not optimal: S → A → D → C → G, cost: 7.
Question 5. [15 marks] Consider a best-first search algorithm in which the evaluation
function f is given by:
f (n) = (2 − x ) g (n) + xh(n)
-
Assuming that h(n) is admissible, for what values of x is this algorithm guaranteed to be
optimal?
f (n) = (2 − x ) g (n) + xh(n)
may be rewritten as follows:
x


f ( n ) = ( 2 − x )  g ( n) +
h ( n) 
2−x


x
h( n) . A* search is optimal
2 −x
x
h( n) is
when the heuristic function is admissible. As h(n) is admissible,
2 −x
which behaves exactly like A* search with a heuristic
admissible for x satisfying the following inequalities:
x
h ( n) ≤ h ( n ) ⇔
2−x
x
≤1 ⇔
2 −x
x ≤ 2 − x ⇔ x ≤1 .
Consequently, the algorithm using f (n) = (2 − x ) g (n) + xh(n) will be optimal for
x ≤1 .
-
What kind of search does it perform when x = 0? When x = 1? When x = 2?
•
x = 0 gives f(n) = 2 g(n). This behaves like UCS.
•
x = 1 gives f(n) = g(n) + h(n). This is A* search.
•
x = 2 gives f(n) = 2 h(n). This is greedy best-first search.
Question 6. [25 marks] Consider the problem of sorting numbers into ascending order:
4
-
1
3
1
2
2
3
4
State representation: a sequence of four numbers
Initial state: 4, 1, 3, 2.
Goal state: 1, 2, 3, 4.
Operators:
o Swapleft: swaps leftmost numbers e.g., in the initial state, 4 and 1.
o Swapmiddle: swaps numbers in the middle e.g., in the initial state, 1 and 3.
o Swapright: swaps rightmost numbers e.g., in the initial state, 3 and 2.
Operators should be applied in this order: Swapleft, Swapmiddle, then Swapright.
-
Path cost: number of swaps.
Apply A* search algorithm to find the minimum number of swaps required to sort the
numbers. Draw the search tree, showing the nodes generated and their f-cost values. Show the
content of the fringe (priority queue) at each step. The heuristic function that can be used is
the Manhattan distance, the sum of the distances of the numbers from their correct locations.
For instance, the heuristic value h(n) in the initial state is 3 + 1 + 0 + 2 = 6.
Solution:
4 1 3 2
s le ft
f = 1 + 4
s m id d le
1 4 3 2
s m id d le
s r ig h t
1 3 4 2
f = 2 + 4
4 3 1 2
f = 1 + 8
1 4 2 3
f = 2 + 4
s r ig h t
s le ft
3 1 4 2
f = 3 + 6
1 3 2 4
s le ft
f = 3 + 2
s m id d le
3 1 2 4
1 2 3 4
f = 4 + 4
f = 4 + 0
f = 0 + 6
s r ig h t
4 1 2 3
f = 1 + 6