8 puzzle: Solvability and shortest solution

Ranjith picture Ranjith · Feb 17, 2013 · Viewed 11k times · Source

I have built a 8 puzzle solver using Breadth First Search. I would now want to modify the code to use heuristics. I would be grateful if someone could answer the following two questions:

Solvability

How do we decide whether an 8 puzzle is solvable ? (given a starting state and a goal state ) This is what Wikipedia says:

The invariant is the parity of the permutation of all 16 squares plus the parity of the taxicab distance (number of rows plus number of columns) of the empty square from the lower right corner.

Unfortunately, I couldn't understand what that meant. It was a bit complicated to understand. Can someone explain it in a simpler language?

Shortest Solution

Given a heuristic, is it guaranteed to give the shortest solution using the A* algorithm? To be more specific, will the first node in the open list always have a depth ( or the number of movements made so fat ) which is the minimum of the depths of all the nodes present in the open list?

Should the heuristic satisfy some condition for the above statement to be true?

Edit : How is it that an admissible heuristic will always provide the optimal solution? And how do we test whether a heuristic is admissible?

I would be using the heuristics listed here

Manhattan Distance 
Linear Conflict 
Pattern Database 
Misplaced Tiles
Nilsson's Sequence Score 
N-MaxSwap X-Y 
Tiles out of row and column

For clarification from Eyal Schneider :

enter image description here enter image description here enter image description here


Answer

Eyal Schneider picture Eyal Schneider · Feb 17, 2013

I'll refer only to the solvability issue. Some background in permutations is needed.

A permutation is a reordering of an ordered set. For example, 2134 is a reordering of the list 1234, where 1 and 2 swap places. A permutation has a parity property; it refers to the parity of the number of inversions. For example, in the following permutation you can see that exactly 3 inversions exist (23,24,34):

1234
1432

That means that the permutation has an odd parity. The following permutation has an even parity (12, 34):

1234
2143

Naturally, the identity permutation (which keeps the items order) has an even parity.

Any state in the 15 puzzle (or 8 puzzle) can be regarded as a permutation of the final state, if we look at it as a concatenation of the rows, starting from the first row. Note that every legal move changes the parity of the permutation (because we swap two elements, and the number of inversions involving items in between them must be even). Therefore, if you know that the empty square has to travel an even number of steps to reach its final state, then the permutation must also be even. Otherwise, you'll end with an odd permutation of the final state, which is necessarily different from it. Same with odd number of steps for the empty square.

According to the Wikipedia link you provided, the criteria above is sufficient and necessary for a given puzzle to be solvable.