4th semester AI project
N-puzzle is a generalization of the 8-puzzle game to larger frames. The puzzle starts with a square frame consisting of
In each move, any tile with an adjacent edge to the empty slot may be moved there. Our goal is to arrange the randomly placed tiles in numerical order.
The game may be modelled as a state-search problem and solved using any standard graph traversal algorithm.1
For a game with
Applying DFS or BFS will yield a time complexity of
Before searching for a path, we first run a simple polynomial-time algorithm to determine if the initial state is even solvable.4 If the initial state is found not to be solvable, a search is not required.5
- Place the tile numbers in an array. Ordered from left to right, then top to bottom. Ignore the empty slot.
- Count the numbers of inversions in the array. An inverted pair is any ordered pair
$(u, v)$ where$u > v$ and$u$ occurs before$v$ in the array. - The puzzle is solvable if and only if there an odd number of such inversions.
This procedure may be implemented in
A* has a better average-case performance than its alternatives whenever it is possible to design an admissable heuristic. As such, we will use A* to implement the state-space search.
The nodes will be expanded in order of their priority given by
where
Since Manhattan distance is mostly observed to outperform Hamming distance as a heuristic for the 15-puzzle problem6, we define
Footnotes
-
Russell, S., & Norvig, P. (2020). Artificial Intelligence: A Modern Approach (pp. 70-71). ↩
-
https://webdocs.cs.ualberta.ca/~hayward/355/jem/tile.html#pzl ↩
-
https://math.stackexchange.com/questions/293527/how-to-check-if-a-8-puzzle-is-solvable ↩
-
https://www.cs.princeton.edu/courses/archive/spring21/cos226/assignments/8puzzle/specification.php ↩
-
https://cs.stackexchange.com/questions/37795/why-is-manhattan-distance-a-better-heuristic-for-15-puzzle-than-number-of-til ↩