An Optimized Method for Solving Zebra Puzzle

2009 Second International Conference on Computer and Electrical Engineering
An Optimized Method for Solving Zebra Puzzle
Soroor Salavati
Department of Computer
Engineering
Shahid Chamran University
Ahvaz, Iran
E-mail: [email protected]
Sahar Hajjarzadeh
Masoud Mazloom
Department of Computer
Engineering
Shahid Chamran University
Ahvaz, Iran
E-mail: [email protected]
Department of Computer
Engineering
Shahid Chamran University
Ahvaz, Iran
E-mail: [email protected]
10. The judo player lives next to the man who has a fox.
11. The cricketer lives next to the man who has a horse.
12. The poker player drinks orange juice.
13. The Japanese plays polo.
14. The Nigerian lives next to the blue house.
The question is:
Who owns the zebra and who drinks Water [3, 4, 6, 8]?
Abstract— The zebra puzzle is an important and attractive
problem in constraint satisfaction problem (CSP) family. In
the last few years several ways have been presented for solving
this problem such as backtracking, minimum remaining values
(MRV), forward chaining (FC), minimum conflicts, and so on.
In this paper the zebra puzzle is considered and a new
intelligent method is presented that has more speed in
comparison with other methods.
II.
Keywords-Constraint Satisfaction Problem (CSP); Forward
chaining (FC); Minimum remaining value (MRV); Minimum
conflicts.
I.
INTRODUCTION
Formally speaking, a constraint satisfaction problem
(CSP) is defined by a set of variables, X1;X2; : : : ;Xn, and a
set of constraints, C1;C2; : : : ;Cm. Each variable, Xi, has a
non-empty domain, Di, of possible values. Each constraint,
Ci, involves some subsets of the variables and specifies the
allowable combinations of values for that subset. A state of
the problem is defined by an assignment of values to some
or all of the variables, {Xi= vi; Xj =vj; …}. An assignment
that does not violate any constraints is called a consistent or
legal assignment. A complete assignment is one in which
every variable is mentioned, and a solution to a CSP is a
complete assignment that satisfies all the constraints. Some
CSPs also require a solution that maximizes an objective
function [2, 7].
Solution of this logic puzzle, attributed to Albert
Einstein, is known as the zebra puzzle. There are five
houses of different colors, inhabited by different nationals,
with different pets, drinks, and sports. Furthermore, there
are the following 14 additional constraints:
1. The Englishman lives in the red house.
2. The Spaniard owns a dog.
3. The man in the green house drinks coffee.
4. The Irishman drinks tea.
5. The green house is to the right of the ivory house.
6. The go player owns snails.
7. The man in the yellow house plays cricket.
8. The guy in the middle house drinks milk.
9. The Nigerian lives in the first house 1.
978-0-7695-3925-6/09 $26.00 © 2009 IEEE
DOI 10.1109/ICCEE.2009.16
SOLVING ZEBRA PUZZLE MANUALLY
The problem can be modeled as a CSP. We number the
houses (left to right) from 1 to 5 and we define the problem
to the following:
• The number assigned to the person who drinks water
is the same as the number assigned to water.
•
The number assigned to the person who owns the
zebra is the same as the number assigned to the
zebra.
Nationalities: Englishman = A1, Spaniard = A2,
Irishman = A3, Nigerian = A4, Japanese = A5.
Plays: go = B1, cricket = B2, judo = B3, poker = B4,
polo = B5.
Drinks: coffee = C1, tea = C2, milk = C3, orange juice = C4,
water = C5.
Pets: dog = D1, snail = D2, fox = D3, horse = D4, zebra = D5.
Colors: red = E1, green = E2, ivory = E3, yellow = E4,
blue =E5.
If X is a letter then Xi ≠ Xj ↔ i ≠ j. Furthermore, we
have:
1. The Englishman (A1) lives in the red (E1) house:
A1 = E1.
2. The Spaniard (A2) owns a dog (D1): A2 = D1.
3. The man in the green (E2) house drinks coffee (C1):
E2 = C1.
4. The Irishman (A3) drinks tea (C2): A3 = C2.
5. The green (E2) house is to the right of the ivory (E3)
house: E2 - E3 = 1.
6. The Go (B1) player owns snails (D2): B1 = D2.
7. The man in the yellow (E4) house plays cricket (B2):
E4 = B2.
8. The guy in the house in the middle drinks milk (C3):
C3 = 3.
9. The Nigerian (A4) lives in the first house: A4 = 1.
448
10. The Judo (B3) player lives next to the man who has a
fox (D3): |B3 - D3 | = 1.
11. The cricketer (B2) lives next to the man who has a
horse (D4): |B2 - D4| = 1.
12. The poker (B4) player drinks orange juice (C4):
B4 = C4.
13. The Japanese (A5) plays polo (B5): A5 = B5.
14. The Nigerian (A4) lives next to the blue (E5) house:
|A4 -E5 | = 1.
Not the Japanese who plays polo (13). Therefore,
the Irishman lives in the second house where he
drinks tea (4) [1].
•
Step 3. Since judo is played in the second house,
we know from (10) that the fox is kept in either the
first house or the third house.
Let us first assume that the fox is kept in the third
house. Then consider: what is drunk by the man
who plays go and keeps snails (6)? We have
already ruled out water and tea from the above
steps. It cannot be orange juice since the drinker of
that plays poker (12). It cannot be milk because
that is drunk in the third house (8), where we have
assumed a fox is kept. This leaves coffee, which
we know is drunk in the green house (3).
So if the fox is kept in the third house, then
someone plays go, keeps snails and drinks coffee in
a green house. Who can this person be? Not the
Nigerian who lives in the first house (9). Not the
Irishman who drinks tea (4). Not the Englishman
who lives in a red house (1). Not the Japanese who
plays polo (13). Not the Spaniard who owns a dog
(2).
This is impossible. So it follows that the fox is not
kept in the third house, and then the Fox is kept in
the first house [1].
•
Step 4. From what we have found so far, we know
that coffee and orange juice are drunk in the fourth
and fifth houses. It doesn't matter which is drunk in
which; we will just call them the coffee house and
the orange juice house.
So where does the man who plays go and keeps
snails live? Not the orange juice house since poker
is played there (12).
Suppose this man lives in the coffee house. Then
we have someone who plays go, keeps snails and
drinks coffee in a green (3) house. Again, by the
same reasoning in STEP 3, this is impossible.
Therefore, the go playing, snail-keeping man lives
in the third house.
It follows that polo is played in the green, coffee
house, by the Japanese (13). Which means the
Spaniard must be the one who drinks orange juice,
plays poker and keeps a dog which means the
Englishman lives in the third house, which is Red,
which means the Spaniard's house is the Ivory one.
By now we have filled in every ‘blank’ accept one,
and it is clear that the Japanese is the one who
keeps the Zebra [1].
Steps of solving the zebra puzzle are:
•
•
Step 1. As it was mentioned the Nigerian lives in
the first house (9). It does not matter whether this is
counted from the left or from the right. We just
need to know the order, not the direction.
From (9) and (14), the second house is blue. What
color is the first house? Not green or ivory, because
they have to be next to each other (5). Not red,
because the Englishman lives there (1). Therefore
the first house is yellow.
It follows that cricket is played in the first house
(7) and the horse is kept in the second house (11).
So what is drunk by the Nigerian in the first,
Yellow, Cricketer house? Not Tea since the
Irishman drinks that (4). Not Coffee since that is
drunk in the Green house (3). Not Milk since that is
drunk in the third house (8). Not orange juice since
the drinker of orange juice plays poker (12).
Therefore it is water (the missing beverage) that is
drunk by the Nigerian [1].
Step 2. So what is played in the second blue house?
Where we know a Horse is also kept.
Not Cricket which is played in the first house (7).
Not go player since that house has Snails (6).
Let us suppose poker is played here, which means
orange juice is drunk here (12). Then consider:
Who lives here? Not the Nigerian since he lives in
the first house (9). Not the Englishman since he
lives in a red house (1). Not the Spaniard since he
owns a dog (2). Not the Irishman since he drinks
tea (3). Not the Japanese who plays polo (13).
Since this is an impossible situation, poker is not
played in the second house.
Let us suppose polo is played here, which means
the Japanese lives here (13). Then consider: What
is drunk here? Not tea since the Irishman drinks
that (4). Not coffee since that is drunk in the green
house (3). Not milk since that is drunk in the third
house (8). Not Orange Juice since the drinker of
that plays poker (12). Again, since this is an
impossible situation, polo is not played in the
second house.
Therefore, judo is played in the second house.
So who plays judo and keeps a horse in the second
blue house? Not the Nigerian who lives in the first
house (9). Not the Englishman who lives in a red
house (1). Not the Spaniard who owns a dog (2).
449
TABLE I.
1
2
3
4
5
Color
Nationality
Drink
yellow
Nigerian
water
blue
Irishman
tea
red
Englishman
milk
green
Japanese
coffee
Play
Pet
cricket
Fox
judo
horse
go
snails
ivory
Spaniard
orange
juice
poker
dog
III.
NEW PROPOSED METHOD (CUA METHOD)
IV.
SOLUTION OF ZEBRA PUZZLE
In this method by taking the advantage of problem note
puzzle is grouped and variables that are in communication
with each other (are in one house or have a specific distance
from each other) have rest in group. Thus with specifying a
variable in the group, we place other variables of this group
in the same house. Actually, each group place in the house
that satisfied all variables constraints. For example, if
Englishman (A1) is in house number one, then red color (E1)
is in the same house, considering A1=E1 in constraint list.
Therefore, determination of the place of variable A1 specifies
the place of variable E1. Then with this method, we do not
need to renew placement of E1 and also, via this method, the
depth of searching tree decreases.
polo
zebra
METHODS FOR SOLVING THE PROBLEM
We accomplish the zebra puzzle with six different
methods which are backtracking, backtracking and forward
chaining (FC), backtracking and minimum remaining value
(MRV), backtracking and minimum conflict, backtracking
and minimum conflict and minimum remaining value
(MRV), and finally our new method will be described.
V.
EXPERIMENT RESULTS
We accomplish the zebra puzzle with all methods in two
phases and compare their speed of solving the zebra puzzle.
The difference between two phases is the initializing the
variables domain. Now on , in all following figures and
tables number 1 means backtracking method, number 2
means backtracking and FC method, number 3 means
backtracking and MRV method, number 4 means
Backtracking and minimum conflicts method, number 5
means backtracking and MRV and minimum conflicts
method and number 6 is the our new proposed method.
A. Backtracking
The term “backtracking search” is used for a deep-seated
search which chooses value for one of variables at each time
and will backtrack when there is no valid value for assigning
to that variable. Notice that this method uses one value at
each time. Beside, expands current value to creating
successor instead of copying.
Simple backtracking is a non heuristic algorithm and it is
not expected to be efficient for big problems [5] [2] [7].
A. Phase 1
In this phase the domain of all variables is initialized
with numbers 1 to 5 and the constraints are not satisfied at
first. The results of this phase are shown in Figure 1.
B. Backtracking and MRV
The backtracking algorithm by default chooses next
unassigned value by order that is distinct in variable's array.
This static order of variables, seldom presents the most
efficient search.
MRV often chooses the variable that iffy fail soon and
therefore prunes the search tree [2].
30000
25000
20000
C. Backtracking and FC
Within searching, a better way to optimize using of
limitations is FC. After assigning X, its value is removed
from the domain of variables that have confliction whit X. In
this method, backtracking will occur when current variable's
domain becomes empty or made assignments do not consent
the limitations of the problem [2] [7].
15000
10000
5000
0
1
D. Backtracking and minimum conflict
In this method a value will be assigned to current
variable that makes less confliction for other variables [2]
[7].
2
3
4
5
6
Figure 1. The domain of all variables is initialized with numbers 1 to 5
B.
Phase 2
In this phase in initialization of variables domain we
manually satisfy the unit constrains and start the searching.
Thus some methods work better and become faster than
previous phase. For example, with considering the constraint
A4=1, the domain of variables A1, A2, A3, A5 can not have
the number one. Then domain of variable A4 will be {1} and
domains of variables A1, A2, A3, A5 will be {2, 3, 4, and 5}.
The results of this phase are shown in Figure 2.
E. Backtracking and minimum conflict and MRV
In this method which is a combination of two former
methods, variable will be chosen by MRV algorithm and its
assigned value will be defined by Minimum conflicts
algorithm [2].
450
VI.
30000
CONCLUSION
“Backtracking and MRV” in phase 2 is the best and
fastest algorithm for solving the zebra puzzle. “Backtracking
and MRV” in phase 1 starts with first variable that is
Englishman and determines the value of all variables one by
one and in this way “MRV” algorithm has no effect on
searching time. But in “Backtracking and MRV” in phase 2
with satisfying the unit constraints at first, the algorithm
becomes more intelligence and then becomes so fast and in
this way “MRV” algorithm causes to decrease the time of
searching by choosing the best variable at each time. Our
method is in second level from a time point of view.
25000
20000
15000
10000
5000
REFERENCES
0
1
2
3
4
5
[1]
6
[2]
Figure 2. Initialization of variables domain with satisfying the unit
constrains manually
[3]
And all results are shown in Table 2 and Figure 3.
TABLE II.
[4]
[5]
TIME ORDER OF ALL METHODS
1
2
3
4
5
6
Phase1
27676
27676
27676
5546
5546
1591
Phase2
27676
23961
1481
5546
16312
1591
[6]
[7]
[8]
Phase 1
30000
Phase2
25000
20000
15000
10000
5000
0
1
2
3
4
5
6
Figure 3. Bar chart of all results
451
Online Publication available at
www.en.wikipedia.org/wiki/Zebra_Puzzle.
S. J. Russell, and P. Norving, “Artificial Intelligence: A Modern
Approach”, Prentice Hall, Englewood Cliffs, New Jersey, 1995.
ISBN 0-13-103805-2, pp. 932 + 28.
K. H. Rosen, “Discrete Mathematics and Its Applications, W. K.
Barter, 1221 Avenue of the Americas. New York. NY 10020, ISBN
0-07-242434-6.
C. Tomasi, “The Zebra Puzzle, Computer Science, Duke University.
Online Publication available at
www.huminf.aau.dk/cg/Module_II/1063.html.
B. Carpenter, and G. Penn, “Speech Work Research”, Department of
Computer Science, 55 Broad St. New York, NY 10004 USA, Toronto
M5S 3G4 Canada, 1992-1995, 32-36.
T. Muller, R. Bartak, and H. Rudova, “Iterative Forward Search:
Combining Local Search with Maintaining Arc Consistency and a
Conflict-based Statistics”, Faculty of Mathematics and Physics,
Charles University, Faculty of Informatics, Masaryk University.
C. Schulte, and G. Smolka, “Finite Domain Constraint Programming
in Oz”, A Tutorial Book, May 14, 2001, 29-32.