## Zebra Puzzle in JavaThe The puzzle is widely used in the evaluation of computer algorithms for solving ## 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 ## General Approaches for Solving CSPThe 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 PuzzleThe 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, ## The Solution to the Zebra PuzzleIn the last few years, several ways have been presented for solving this problem such as From the above 16 constraints (statements), let's summarize the attributes of the puzzle.
Let's organize the given data in table form for better understanding.
The following Java program includes the following four user-defined classes: - The Zebra Class
- The PossibleLine Class
- The PossibleLines Class
- The Solver Class
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 |

For Videos Join Our Youtube Channel: Join Now

- Send your Feedback to [email protected]