Sei sulla pagina 1di 24

Exercises

Chapter 1 (Introduction to AI)

1.1. Define in your own words: (a) intelligence, (b) artificial intelligence, (c) agent.

1.2. Many well-known inference processes, learning processes, etc. are intractably difficult
for computers. Does it mean AI impossible?

1.3. Give reasons for the unsuitability of the Turing test as a definition of “artificial
intelligence” in practical AI.

Chapter 2 (Solving Problems by Search)

2.1 Does a finite state space always lead to a finite search tree? How about a finite state space
that is a tree?

2.2 Give the initial state, goal state, successor function and cost function for the following
problem.
a) You have to color a planar map using only four colors, in such a way that no two adjacent
regions have the same color.
b) There is a monkey at the door into a room. In the middle of the room a banana is hanging
from the ceiling. The monkey is hungry and wants to get the banana, but he cannot stretch
high enough from the floor. At the window of the room there are two stackable, movable,
climbable boxes the monkey may use. Can the monkey get the banana?”
c) A farmer wants to bring a cabbage, a goat, and a wolf across a river, but his boat is so
small that he can only take them across one at a time. The farmer thought that it over and then
said to himself: “If I first bring the wolf to the other side, then the goat will eat the cabbage. If
I transport the cabbage first, then the goal will be eaten by the wolf. What should I do?
d) The missionaries and cannibals problem is described as follows. Three missionaries and
three cannibals are on the one side of a river, along with a boat that can hold one or two
people. Find a way to get everyone to the other side, without ever leaving a group of
missionaries in one place outnumbered by the cannibals in that place.

2.3. Consider a state space tree where the start state is number 1 and the two child nodes of
state number n are the nodes with numbers 2n and 2n+1.
a. Draw the portion of the state space tree for states 1 to 15.
b. Suppose the goal state is 11. List the order in which nodes will be visited for breadth-first-
search and depth-first-search.

2.4. Give the search tree as in the following figure. The goal state is E. List the order in which
nodes will be visited for depth-first-search.

1
2.5. Specify state, rules and a termination condition for a search method to solve the
following water-jug problem.
Given a 7-liter jug filled with water, an empty 5-liter jug, and an empty 3-liter jug, how can
one obtains precisely 1-liter in the 5-liter jug? Water may be poured from one jug into
another, but never out of a jug into oblivion.
Draw that part of the search tree corresponding to the moves you tried in finding a solution.

2.6. Consider a miniature version of the 8-puzzle, called the 3-puzzle, with the start and goal
states:

- 1 1 2
3 2 - 3

Draw the state space tree for this problem with the depth limit of 4.

2.7. Given the starting and goal states of the 8-puzzle as follows.

1 2 1 2 3
4 5 3 4 5
7 8 6 7 8 6

a) Give the initial state, goal state, successor function and cost function for the problem.
b) Draw the space state tree for solving the 8-puzzle by breadth-first-search.

2.8 Given a city map in southern of Germany as in the following figure. We want to find the
way to go from Linz to Ulm. Draw the space state tree for solving the problem by breadth-
first-search.

2
2.9. Given a complete and weighted graph consisting of 4 vertices A, B, C, D as in the
following figure.

Assume that vertex A denotes the starting city of a Traveling Salesman Problem (TSP) with
the above weighted graph.
Solve the TSP using nearest-neighbor heuristic. Give the solution and the total cost of this
tour.

2.10 Define in one sentence the following concepts:


(a) state space
(b) expand a state
(c) branching factor

2.11 What is the main difference between uninformed search and informed search.

3
Chapter 3 (Heuristic Search)

3.1. Given the following problem:


Set of variables: X, Y.
Let assign values from the set of values {2, 3, 4, 5} to the variables such that the assignment
satisfies the following constraints: X + Y is a multiple of 3, X ≠ 3, X<Y.
Solve this problem by generate-and-test algorithm.
Hint: Draw the space state tree describing the process of the problem solving.

3.2. Given the following heuristic search algorithm for solving the Traveling Salesman
Problem:
- Connect all the cities into an arbitrary path.
- Pick two points along the path at random.
- Split the path at those points, producing three pieces.
- Try all six possible ways to connect the three pieces.
- Keep the best one, and reconnect the path accordingly.
- Iterate the steps above until no improvement is observed for a while.
Give the name of the above algorithm.

3.3. Given the city map as in the following figure. In the figure, links are labeled by their
lengths and the numbers in the boxes are flying distances to goal city t.
Assume that s is the start city. Construct the A* search tree step by step in the process of
applying A* search algorithm to find the shortest route from s to t. Take care that each city
only appears once per path.

3.4. Construct the A* search tree step by step for the city graph from Figure 3 in slides of
Chapter 3 and use the flying distances to Ulm as the heuristic. Start in Bern with Ulm as the
destination. Take care that each city only appears once per path.

3.5. Why is hill-climbing algorithm prone to ending in local optimum rather than the global
optimum of the search space?

3.6. Why can simulated annealing escape from local minimum points?

4
3.7. Given the city map in Romania as in the following figure

And given the flying distances to Bacharest as follows:

Arad 366 Mehadia 241


Bucharest 0 Neamt 234
Craiova 160 Oradea 380
Drobeta 242 Pitesti 100
Eforie 161 Rimnicu Vilcea 193
Fagaras 176 Sibiu 253
Giurgiu 77 Timisoara 329
Hirsora 151 Urziceni 80
Iasi 226 Vasui 199
Lugoj 244 Zerind 374

Construct the search tree step by step in applying A* algorithm for the city graph and use the
flying distances to Bucharest as the heuristic h. Start in Arad with Bucharest as the
destination.

3.8 Given the same data as in Exercise 3.7. Construct the search tree step by step in applying
A* algorithm to find the shortest path from Lugoj to Bucharest.

3.9. Suppose we have a graph with nodes S, A, B, C, D and G, where S is the start node and
G is the goal node. The distances between connected nodes are given in the following figure.

5
The heuristic estimates of the distances to G are:

from S A B C D G
distance 22 20 8 12 10 0

a) Construct the A* search tree step by step for finding the shortest path from S to G.
b) Is the heuristic estimate optimistic?

3.10 Given the graph as in the following figure. The children of a node are given in
alphabetic order when the node is expanded. You are doing graph search so that nodes are
never expanded twice (i.e. graph search eliminates repeated states). “S” is the start node, and
“G” is the goal node. The number inside each node is an estimate of the remaining distance to
the goal node from that node. The number by each arc is the step cost for that arc.

a) Draw the search tree when applying greedy best- first-search to find the shortest path from
S to G. Write the order in which the search expands nodes.

6
b) Draw the search tree when applying A* search to find the shortest path from S to G. Write
the order in which the search expands nodes.

3.11 Given the city graph from Figure 3 in slides of Chapter 3. Construct the search tree step
by step for applying greedy best- first-search to find the shortest path from Linz to Ulm. Use
the flying distances to Ulm as the heuristic.

3.12. Given the graph coloring problem with the graph in the following figure. This problem
can be described as a constraint satisfaction problem (CSP). Assume that any two vertices
connected with an arc must not be colored with the same color. Assume that the set of colors
is {red, blue, yellow}.
a) Describe the problem as a constraint satisfaction problem.

V1 V2

V4 V3

b) Solve the problem step by step by a backtracking algorithm (by drawing the search tree).
Assume that the order of vertices to be considered is < V1, V2, V3, V4>.

3.13. Given the principal states and territories of Australia as in the following figure.
Coloring this map can be viewed as a constraint satisfaction problem. The goal is to assign
colors to each region so that no neighboring regions have the same color. Solving the
problem step by step by a backtracking algorithm (by drawing the search tree). How many
solutions are there for the map-coloring problem? Assume that the set of colors is {red, blue,
yellow}. Assume that the order of vertices to be considered is first, start with SA and then
moving clockwise.

7
3.14. A Latin square is an n  n table whose cells are filled with number from 1 to n in such a
way that each row and column of the table contains each number exactly once. How do you
formulate the task of generating a Latin square as a constraint satisfaction problem?

1 2 3
2 3 1
3 1 2

3.15. Given the following crypt-arithmetic problem:

TWO
+TWO
-----------
FOUR

Each letter stands for a distinct digit. Describe the crypt-arithmetic problem as a constraint
satisfaction problem.

3.16. The secretary of a high school has to come up with a plan for allocating rooms for final
exams. She has the following information: the four teachers Mayer, Hoover, Miller and Smith
give tests for the subjects German, English, Math, and Physics in the following rooms 1, 2, 3
and 4. Every teacher gives a test for exactly one subject in exactly one room. Besides, she
knows the following about the teachers and their subjects.
1. Mr. Mayer never gives tests in room 4.
2. Mr. Miller always tests German.
3. Mr. Smith and Mr. Miller do not give tests in neighboring rooms.
4. Mr. Hover tests Math.
5. Physics is always tested in room 4.
6. German and English are not tested in room 1.
Who gives a test in which room?

Describe the above problem as a constraint satisfaction problem.

8
3.17. What is the difference between Simple Hill Climbing and Steepest Ascent Hill
Climbing?

3.18.
(True/false) Breadth-first-search is best-first-search with f(n) = depth(n).
(True/false) A* becomes greedy best-first-search when f(n) = h(n), i.e. when g(n) is assumed
to be 0.

3.19. What is admissible heuristic function h(n) in A* algorithm for finding shortest path
problem?

3.20. Define in one sentence the following concepts:


(a) optimal search
(b) complete search
(c) greedy best-first search
(d) A* search.

3.21 What is the difference between Steepest Ascent Hill Climbing and Best-first-search?

Chapter 6 (Knowledge Representation)

6.1. Give a one or two sentence definition of each of the following:


a) knowledge representation
b) fact
c) object
d) inheritance
e) inferential knowledge
f) procedural knowledge

Chapter 7 (Using Logic)

Propositional Logic

7.1. Show that [p (q r)] is logically equivalent to (p  q)  (p  r)

7.2. Solving the following case with the help of a resolution proof: “If the criminal had an
accomplice, then he came in a car. The criminal had no accomplice and did not have a key, or
he had the key and an accomplice. The criminal had the key. Did the criminal come in a car
or not?”

7.3. Given the following clause set. By resolution, prove “H is true”.


A
B
C
D
E
A B  C  F

9
ADG
CFEH

7.4. Given the following clause set. By resolution, prove “Q is true”.


PQ
LMP
BLM
ABL
A
B

Predicate Logic

7.5. Formalize the following statements in predicate logic:


(a) Every person has a father and a mother.
(b) Some people have children
(c) All birds that are not penguins fly
(d) There is an animal that eats grain-eating animals
(e) Every animal eats plants or plant-eating animals which are much smaller than itself.

7.6. Consider the following sentences:


 John likes all kinds of food
 Apples are food.
 Chicken are food.
 Anything anyone eats and isn’t killed by is food.
 Bill eats peanuts and is still alive.
 Sue eats everything Bill eats.

a) Translate these sentences into formulas in predicate logic.


b) Prove that “John likes peanuts” using resolution.

7.7. Consider the following sentences:


 Boris is Colin’s boss
 Anna is Boris’s boss
 Anna is getting a paycut.
 If a person is getting a paycut, all the employees under her or his supervision are also
getting a paycut.

a) Translate these sentences into formulas in predicate logic.


b) Prove that “Colin is getting a paycut” using resolution.

7.8.
Consider the following sentences:
 Anyone who does not sink and weights the same as a duck is a witch
 Everyone who is made of wood weights the same as a duck
 Everyone who is a witch is burned.
 Bob is made of wood and does not sink.

a) Translate these sentences into formulas in predicate logic.

10
b) Prove that “Bob is burned” using resolution

7.9. Given the following clause set in predicate logic:


SL(x, l)  Sl(y, l)  Und(x, y)
Und(x, y)  Friend(x, y)
Friend(x, y)  Friend(y, z)  Friend(x, z)
SL(Ann, French)
SL(Bob, French)
SL(Bob, German)
SL(Cal, German)
where:
SL(x, l) means x speaks language l.
Und(x, y) means x can understand y.
Friend(x, y) means x is a friend of y.

Prove that: Friend(Ann, Cal).

7.10. Given the following clause set in predicate logic:


Rule1: Breeze(I) Pit(E)  Pit(J)  Pit(M)
Rule2: Breeze(y)  Adjacent(x, y)   Pit(x)
Rule3: Adjacent(x, y)  Adjacent(y, x)
Fact1: Breeze(I)
Fact2: Breeze(N)
Fact3: Adjacent(E, I)
Fact4: Adjacent(J, I)
Fact5: Adjacent(M, I)
Fact6: Adjacent(M, N)
Fact7: Adjacent(J, N)

Prove that “Pit(E) is true”.

7.11. What is the advantage of using logic over other method of representation?

Chapter 8 (Using Rules)

8.1. Given the rules in Prolog:


/* R1 */ goal1 :- fact1.
/* R2 */ goal1 :- a, b.
/* R3 */ goal2(X):- c(X)
/* R4 */ a:- not(d).
/* R5 */ b:- d.
/* R6 */ b:- e.
/* R7 */ c(2):- not(e).
/* R8 */ d:- fact2, fact3.
/* R9 */ e:- fact2, fact4.
Assume that only the facts fact2, fact3 are true in the Prolog program. Describe step by step
the backward chaining process to solve the queries:
?- goal1.
?- goal2(Z).
11
8.2. Given the rules in Prolog:
/* R1 */ goal1 :- fact1.
/* R2 */ goal1 :- a, b.
/* R3 */ goal2(X):- c(X).
/* R4 */ a:- not(d).
/* R5 */ b:- d.
/* R6 */ b:- e.
/* R7 */ c(2):- not(e).
/* R8 */ d:- fact2, fact3.
/* R9 */ e:- fact2, fact4.

Assume that only the facts fact2, fact3 are true in the Prolog program. Describe step by step
the forward chaining process to solve the query:
?- goal2(Z).

8.3. Given a rule-based system. This system consists of eight rules as follows:
/* R1 */ k(X) :- j(X), b(X).
/* R2 */ f(X) :- a(X), not(g(X)).
/* R3 */ a(X):- b(X), i.
/* R4 */ d:- i.
/* R5 */ d:- e(X),c.
/* R6 */ g(X):- h, a(X).
/* R7 */ g(X):- l.
/* R8 */ b(X):- c.

Assume that the goal is d and the facts are c, l, e(a), and j(b), in that order.
a) Suppose we use pure forward chaining. List the rule invocations, successes, and failures in
order as they occur. Use the rule numbers to indicate rules.
b) List the rule invocations, successes, and failures for backward chaining, in order.

8.4. Give the rules:


R1: if hot and smoky then fire
R2: if alarm_beeps then smoky
R3: if fire then sprinklers_on
F1: alarm_beeps
F2: hot
Assume that the two facts F1, F2 are true, Describe step by step the forward chaining process
to solve the goal sprinklers_on.

8.5. State the differences between backward chaining and forward chaining.

Chapter 9 (Structured Knowledge)

9.1. Given a semantic network in the following figure. The semantic network has four
categories and two objects (John, Mary).

12
Represent this semantic network in Prolog.

9.2. Given a semantic network in the following figure.

Represent this semantic network in Prolog.

9.3. a) Draw a semantic network representing the following facts:

13
Ships are things.
Carriers are ships.
Ships have a position.
Ships have a crew.
Carriers have planes.
Planes are things.
A crew consists of people.
People are things.

b) Represent this semantic network in Prolog.

9.4. a) Draw a semantic network representing the following facts:

A bird is a kind of animal.


Flying is the normal moving method of birds.
Birds are active at daylight.
An albatross is a bird.
A kiwi is a bird.
Albert is an albatross, and so is Ross.
Kim is a kiwi.
Kiwis always walk.
Kiwis are active at night.
The color of kiwis is brown.
The color of albatrosses is black-and-white.

b) Represent this semantic network in Prolog.

9.5. Why are object-oriented programming languages more suitable to implement frame-
based knowledge representation?

Chapter 10 (Uncertain Knowledge Representation and Reasoning)

10.1. What is the difference between conditional and joint probabilities between two random
variables?

10.2.
a) State Bayes’s Theorem
b) Given the following facts:
P(A|B) = 2/3; P(A|~B) = 1/3; P(B) = 1/3
Compute the probability P(B|A).

10.3. What type of questions can you answer with a Bayes net?

10.4. “I don’t have a car. I come to work either by bike or by bus. If I take a bus, there is a
10% that I’m lake. If I take the bike, there is a 2% chance that I’m late. I take the bike 4 days
out of 5. Today I was late.”
Calculate the probability that I took the bus today.

10.5. a) Prove:
If P(a| b, c) = P(b|a, c) then P(a|c) = P(b|c)

14
b) Prove:
P(A, B|E) = P(A| B, E).P(B|E)

10.6. Briefly describe what each of the following means:


a) P(A, B)
b) P(A| B,C)
c) P(A| B, C) = P(A|B)
d) P(A, B, C) = P(A|B,C).P(B|C).P(C)

10.7. Given a Bayesian Network as in the following figure.

Half of all people are stupid. If you’re stupid then you’re more likely to be confused. A
random-chosen person is confused. What is the chance he is stupid?

10.8. Given a Bayesian network with the three binary variables A, B, C and P(A) = 0.2 and
P(B) = 0.9, as well as the CPT shown below:

A B P(C) A B
T F 0.1
T T 0.2
F T 0.9
F F 0.4
C

a) Compute P(A|B).
b) Compute P(C|A).

10.9. Given the Bayes network in the following figure which models that rain or sprinkler can
cause the grass to get wet. It rains on 40% of the days. The sprinkler is turned on 20% of the
days.

15
a) Calculate P(W|S)
b) Calculate P(S|W)
c) Calculate P(S|R,W)

10.10. For the alarm example which is described as in the following figure.

Calculate the following conditional probabilities:


a. Calculate the a priori probabilities P(A), P(J), P(M).
b. Calculate P(M|B).
c. Use Bayes’s formulat to calculate P(B|M).
d. Compute P(A|J,M)

Chapter 11 (Machine Learning)

11.1. Given the table with training data as follows:

Num. x1 x2 x3 Class

16
1 1 3 1 +
2 3 5 2 +
3 3 2 2 -
4 5 2 3 -

Each sample has three attributes. The two samples X1, X2 belong to Class + and X3, X4
belong to Class -.
a) Classify the vector (2, 4, 2) with the 1-nearest-neighbor method.
b) Classify the vector (2, 4, 2) with the 3-nearest-neighbor method.
Note : use the Euclidean distance.

11.2. Determine the class of P = [1, 22] using the 1-nearest-neighbor classifier and the 3-
nearest-neighbor classifier, both using the training examples from the following table.

x1 1 1 1 2 3 3 3 4 5
x2 1 2 4 3 0 2 5 4 3
class + - - + + + - - -

Explain why the two classifiers differ in their classification behavior.

11.3. Determine the entropy impurity for the following distributions.


a) The dataset has 1/2 of patterns belonging to the first class, 1/4 of patterns belonging to the
second class, 1/8 of patterns belonging to the third class, 1/16 of patterns belonging to the
fourth class, and 1/16 of patterns belonging to the fifth class.
b) The dataset consists of five classes and each class has 1/5 of patterns.
c) The dataset has 5/8 of patterns belonging to the first class and 3/8 of patterns belonging to
the second class.

11.4. Consider the following data set for a binary classification problem.
A B Class label
T F +
T T +
T T +
T F -
T T +
F F -
F F -
F F -
T T -
T F -

Each pattern has two binary attributes and one class label (+ or -).
Let use the information gain when determining the splitting attributes. Build the decision tree
for the data set.

11.5. Consider the following Weather dataset for a binary classification problem. Each
pattern has four discrete attributes and one class label (Yes or No).

Outlook Temperature Humidity Windy Play Tenis

17
Sunny Hot High False No
Sunny Hot High False No
Overcast Hot High False Yes
Rainy Mild High False Yes
Rainy Cool Normal False Yes
Rainy Cool Normal True No
Overcast Cool Normal True Yes
Sunny Warm High False No
Sunny Cool Normal False Yes
Rainy Warm Normal False Yes
Sunny Warm Normal True Yes
Overcast Warm High True Yes
Overcast Hot Normal False Yes
Rainy Warm High True No

Let use the information gain when determining the splitting attribute. Which of the features is
selected as splitting attribute at the root node in the decision tree for the data set.

11.6. Consider the following Food dataset for a binary classification problem. Each pattern
has three discrete attributes and one class label (Yes or No).

Temperature Taste Size Appealing


Hot Salty Small No
Cold Sweet Large No
Cold Sweet Large No
Cold Sour Small Yes
Hot Sour Small Yes
Hot Salty Large No
Hot Sour Large Yes
Cold Sweet Small Yes
Cold Sweet Small Yes
Hot Salty Large No

a) What is the initial entropy of the class label Appealing?


b) Which of the features is selected as splitting attribute at the root node in the decision tree
for the data set?
c) Build the whole decision tree.

11.7. Given a decision tree in the following figure.

18
List all the classification rules extracted from the above decision tree.

11.8. Given the confusion matrix of a classifier with 3 classes as follows:

Predicted
C1 C2 C3
Actual C1 19 4 1
C2 3 20 4
C3 2 3 21

Calculate the accuracy of this classifier.

11.9.
a) (True/false) Decision tree is learned by minimizing information gain.
b) (True/false) The depth of a learned decision tree can be larger than the number of training
examples used to create the tree.
c) (True/false) Decision tree classifier does not need training phase.
d) (True/false) Decision trees handle real-valued attributes by discretizing them.

Prolog

P.1. Given the predicate delete which is defined in Prolog as follows:


delete(X, [X|Tail], Tail). (pr1)
delete(X, [Y|Tail], [Y|Tail1]):- delete(X, Tail, Tail1). (pr2)
Describe step by step the process for Prolog to answer the following query:
?- delete(a, [b, c, a, f], L).

P.2. Given the predicate member which is defined in Prolog as follows:


member(X, [X|Y]).
member(X, [Y|Z]):- member(X, Z).
Describe step by step the process for Prolog to answer the following query:
?-member(A, [1,2,3]).

19
P.3. What is the purpose of the following predicate?
abs(N, M):- N < 0, !, M is - N.
abs(N, N).

P.4. Given the predicate len(L, N) with the list L and the integer N as follows:
len([], 0).
len([_|Ts], N):- len(Ts, M), N is M+1.
What is the purpose of the predicate len ?

P.5. Given the predicate ordered_list(L) with the list of integers L as follows:
ordered_list([]).
ordered_list([H| Ts]) :- ordered(H, Ts).
ordered(_, []).
ordered(H, [Y| Ts]) :- H =< Y, ordered( Y, Ts).
What is the purpose of the predicate ordered_list ?

P.6. Given the predicate gen_list(N1,N2, List) with two integers N1, N2 and the list List as
follows:
gen_list(N, N, [N]).
gen_list(N1, N2, [N1|List]) :- N1 < N2, M is N1 + 1, gen_list(M, N2, List).
What is the purpose of the predicate gen_list ?
P.7. Given the predicate maxlist(L, X) in which L is a list and X is an integer.
maxlist([X], X).
maxlist([X, Y| Rest], Max):- maxlist([Y|Rest], MaxRest), max(X, MaxRest, Max).
max(X, Y, X):- X >= Y.
max(X, Y, Y):- X < Y.
What is the purpose of the predicate maxlist ?
.
P.8. The following predicate classifies numbers into three classes: positive, zero and
negative:
class(Number, positive):- Number >0.
class(0, zero).
class(Number, negative):- Number <0.

Redefine this predicate in a more efficient way using cuts.

P.9. Assume that a square matrix can be represented in Prolog as a list of lists. Write
predicate display(M) which can write each row of the matrix M in one line.

P.10. Given the following Prolog program:


20
dosquares:-
repeat,
read(X), dowrite(X).
dowrite(X):- X = stop,!.
dowrite(X):-
Y is X*X, write(Y), nl, fail.

When we execute the query ?- dosquares.


What we get from the above program?

P.11. Given the predicate cube_table as follows:


cube_table:- L = [0,1,2,3,4,5,6], member(X, L),
Z is X*X*X,
write(Z), nl, fail.
We put the question:
?- cube_table.
What is the result of this question?

P.12. Given the predicate maketable as follows.


:- dynamic product/3.
maketable :- L = [0,1,2,3,4,5,6,7,8,9], member(X,L), member(Y,L),
Z is X*Y, assert( product(X,Y,Z)), fail.
We put the question:
?- maketable.
What happens as the result of this question?
Next, we put the question:
?- retract(product(X,Y,Z)), fail.
What happens as the result of this question?

P.13. The Fibonacci sequence is defined recursively by fib(0) = 1, fib(1) = 1 and fib(n) =
fib(n-1) + fib(n-2). Given two versions of Prolog predicate fib(N, R) which calculates fib(N)
and returns it in R.
(i)
fib(0,1).
fib(1,1).
fib(N, R):- N1 is N-1, fib(N1, R1), N2 is N-2, fib(N2, R2), R is R1+R2, fib(N, R).

(ii)
:-dynamic fib/2.
fib(0,1).
fib(1,1).
fib(N, R):- N1 is N-1, fib(N1, R1), N2 is N-2, fib(N2, R2), R is R1+R2, assert(fib(N, R)).
Why is fib with assert also faster than the first version of fib.

P14. Given the recursive algorithm for solving the Hanoi Tower problem as follows.

procedure hanoi(n, beg, aux, end);


begin

21
if n = 1 then
writeln(beg, end)
else
begin
hanoi(n-1, beg, end, aux) ;
writeln(beg, end);
hanoi(n-1, aux, beg, end);
end
end;
Write the algorithm in Prolog.

P15. Given the algorithm that can merge two sorted arrays a[1..M] and b[1..N] into a third
array c[1..M+N] as follows:
i:= 1; j :=1;
for k:= 1 to M+N do
if a[i] < b[j] then
begin c[k] := a[i]; i:= i+1 end
else begin c[k] := b[j]; j := j+1 end;
Write the algorithm in Prolog.

P16. Suggest an appropriate definition of operators (‘was’, ‘of’, ‘the’) to be able to write
clauses like
diana was the secretary of the department.
and then ask Prolog:
?- Who was the secretary of the department.
Who = diana
?- diana was What.
What = the secretary of the department.

Chapter 4 (Game Playing)

4.1. The search tree for a two-player game is given in the following figure with the ratings of
all leaf nodes:

MAX

MIN

MAX

22
a) Using minimax search, give the optimal resulting rating for each inner node. Mark the
chosen path.
b) Use minimax search with alpha-beta pruning from left to right. Cross out all nodes that are
not visited and give the optimal resulting rating for each inner node. Mark the chosen path.

4.2. The search tree for a two-player game is given in the following figure with the ratings of
all leaf nodes:

O MAX

O O MIN

O O O O MAX

O O O O O O O O
1 4 5 6 2 1 1 1

Use minimax search with alpha-beta pruning from left to right. Cross out all nodes that are
not visited and give the optimal resulting rating for each inner node. Mark the chosen path.

4.3. The search tree for a two-player game is given in the following figure with the ratings of
all leaf nodes. If you use minimax search with alpha-beta pruning from left to right, which
nodes (internal and leaf) would not be examined.

23
4.4. The search tree for a two-player game is given in the following figure with the ratings of
all leaf nodes:

a) Using minimax search, give the optimal resulting rating for each inner node. Mark the
chosen path.

b) Use minimax search with alpha-beta pruning from left to right. Cross out all nodes that are
not visited and give the optimal resulting rating for each inner node. Mark the chosen path.

24

Potrebbero piacerti anche