Zebra Puzzle in JavaThe 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 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:
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.
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 PuzzleIn 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.
The following Java program includes the following four user-defined classes:
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 |