*83*

# Zebra Puzzle in Java

The **zebra puzzle** is a complex puzzle that requires a lot of effort and mental exercise to solve the puzzle. Sometimes is also known as **Einstein’s Puzzle** or **Einstein’s Riddle** because it is invented by the famous German physicist **Albert Einstein**.

The puzzle is widely used in the evaluation of computer algorithms for solving **constraint satisfaction problems (CSP)**. Most of the AI problems can be modeled as constraint satisfaction problems (CSP).

## Constraint Satisfaction Problem (CSP)

Generally, the CSP problem is composed of a finite set of variables, each of which has a finite domain of values, and a set of constraints. Each constraint is defined over some subset of the original set of variables and restricts the values these variables can simultaneously take. The task is to find an assignment of a value for each variable such that the assignments satisfy all the constraints, in some problems, the goal is to find all such assignments.

Therefore, we can define CSP as a set of objects whose state must satisfy a number of constraints or limitations.

Some popular examples of the problem are the **N-queens problem, graph coloring problem, crossword puzzle**, etc.

## General Approaches for Solving CSP

The general approach to solving a CSP is the generate-and-test method. Each possible assignment of values to variables is systematically generated and then tested to see if it satisfies all the constraints. The first assignment that satisfies all the constraints is the solution. In the worst-case (or when we are trying to find all the solutions for a CSP, the number of assignments to be considered is the size of the Cartesian product of all the variable domains. Thus, the time complexity of this approach is exponential in the number of variables. Empirically this method performs very poorly.

Randomized generate-and-test algorithms that select the assignments to test at random in accord with some biased distribution (e.g. the distribution might be biased by the most recently tested assignments as in randomized hill-climbing) can sometimes perform extremely well, but unfortunately, lose systematicity. That is, these randomized methods are unable to prove that no solution exists since they do not necessarily test all assignments.

Generally, there are the following three approaches to solve the CSP:

- Tree Search
- Constraints Propagation
- Combining Backtracking search and Constraints Propagation

## The Zebra Puzzle

The puzzle has several variants. Other versions of the puzzle have various differences from the Life International puzzle, in which various colors, nationalities, cigarette brands, drinks, and pets are substituted. But the logic will not change. The following variant was published in Life International.

- There are five houses.
- The English man lives in the red house.
- The Swede has a dog.
- The Dane drinks tea.
- The green house is immediate to the left of the white house.
- They drink coffee in the green house.
- The man who smokes Pall Mall has birds.
- In the yellow house they smoke Dunhill.
- In the middle house they drink milk.
- The Norwegian lives in the first house.
- The man who smokes Blend lives in the house next to the house with cats.
- In a house next to the house where they have a horse, they smoke Dunhill.
- The man who smokes Blue Master drinks beer.
- The German smokes Prince.
- The Norwegian lives next to the blue house.
- They drink water in a house next to the house where they smoke Blend.

Note that each of the five houses is painted a different color, and their residents are of different national extractions, own different pets, drink different beverages, and smoke different brands of American cigarettes. One other thing: in statement 5, left means your’s right.

The question is, **who owns the zebra**? Additionally, list the solution for all the houses. Optionally, show the solution is unique.

## The Solution to the Zebra Puzzle

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**, etc. But in this section, we will use the general approach i.e. **generate-and-test.**

From the above 16 constraints (statements), let’s summarize the attributes of the puzzle.

**Color:** red, green, ivory, yellow, blue

**Nationality:** Englishman, Spaniard, Ukrainian, Norwegian, Japanese

**Drink:** coffee, tea, milk, orange juice, Water

**Smoke:** Old Gold, Kools, Chesterfields, Lucky Strike, Parliaments

**Pet:** dog, snail, fox, horse, Zebra

Let’s organize the given data in table form for better understanding.

House | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|

Color | Yellow | Blue | Red | Ivory | Green |

Nationality | Norwegian | Ukrainian | Englishman | Spaniard | Japanese |

Drink | Water | Tea | Milk | Orange juice | Coffee |

Smoke | Kools | Chesterfield | Old Gold | Lucky Strike | Parliament |

Pet | Fox | Horse | Snails | Dog | Zebra |

The following Java program includes the following four user-defined classes:

- The Zebra Class
- The PossibleLine Class
- The PossibleLines Class
- The Solver Class

**Zebra.java**

**Output:**

After general rule set validation, remains 60 lines. After removing out of bound neighbors, remains 52 lines. After 1 recursive iteration, remains 17 lines After 2 recursive iteration, remains 6 lines After 3 recursive iteration, remains 5 lines ------------------------------------------- 1 - Norwegian - Yellow - Cats - Water - Dunhill 2 - Danish - Blue - Horse - Tea - Blend 3 - English - Red - Birds - Milk - Pall Mall 4 - German - Green - Zebra - Coffee - Prince 5 - Swedish - White - Dog - Beer - Blue Master