8 and 15 Puzzles | Algorithms and Data Structures (2024)

The (N2 − 1)-puzzle is a collection of N2−1 movable tiles number 1 through N2−1together with one blank arranged in an N×N square. Figure 1 shows aneight-puzzle.

8 and 15 Puzzles | Algorithms and Data Structures (1)
Figure 1. An eight-puzzle.

The only valid moves are to move a tile which is immediately adjacent to the blank into the location ofthe blank. An example of such a move is to move tile 6 into the blank as is shown in Figure 2. Given anyarrangement of the tiles, there are between two and four valid moves.

8 and 15 Puzzles | Algorithms and Data Structures (2)
Figure 2. A valid move of the eight-puzzle.

The objective is to take a permutation of the tiles and the blank; and, by makinga sequence of valid moves, to transform the puzzle into the original shown inFigure 1. Figure 3 shows a permutation with a single move which places 6 intothe correct location. Given a permutation, a solution is a sequence of moves whichtransforms the permutation into the solution.

8 and 15 Puzzles | Algorithms and Data Structures (3)
Figure 3. A move in a permutation of the eight-puzzle.

Please note, only half of all permutations of the tiles and the blankhave solutions.

The algorithm presented usesan A* search to find the solution to the (N2 − 1)-puzzle: arranging the numbers in orderwith a blank in the last location.

The data structure used to efficiently solve the A* algorithm is a modified heapwhich is able to allow the user to update the priority in O(ln(n)) time:a index to each entry is stored in a hash table and when the priority is updated,the index allows the heap to, if necessary, percolate the object up.

Without the hash table, objects in the heap could not be easily accessed andtherefore the run time would be slowed significantly.

The design, shown in Figure 4, is as follows:

  • Each object is placed into the hash table correspondingto its bin, here shown using a chained hash table.
  • The nodes within the chains store not only the object, butalso an index into the heap.
  • The heap only stores pointers back to the nodes in the hashtable.

8 and 15 Puzzles | Algorithms and Data Structures (4)
Figure 4. An updatable priority queue.

For example, Black hashes to 4 and has the highest priority, thereforeit is in the 1st location of the heap and the index 1 is stored in the node.Similarly, Orange hashes to 7 and has priority lower than Brown. Beingstored in index location 4, the node in the hash table stores 4.

There are three distances which can be used to measure the distance between the stateof a puzzle and the solution:

  • The discrete distance (0 if equal and 1 otherwise),
  • The Hamming distance (the number of tiles out of place), and
  • The Manhattan distance (the sum of the minimum number of steps to move each tile (assuming no other tiles) in its correct location),

For example, Figure 5 shows the solution to the eight-puzzle and a permutation of the tiles.

8 and 15 Puzzles | Algorithms and Data Structures (5)
Figure 5. The solution to the eight-puzzle and a permutation of the tiles.

The discrete distances between the permutation and the solution is 1 (they are different). Using the Hamming distance,the distance is 8—only one tile is in the correct location. This is shown on the left of Figure 6. Using theManhattan distance, the distance is the sum of the moves shown in Figure 6: 2 + 0 + 4 + 2 + 1 + 1 + 2 + 3 + 1 = 16.

8 and 15 Puzzles | Algorithms and Data Structures (6)
Figure 6. The Hamming and Manhattan distances of the permutation from Figure 5.

The formula for the average Manhattan distance of a random permutation isgiven by the formula2/3(N−1)(N2+N−3/2), which, for this case is 14.

The Updatable_heap data structure makes use of a heap as an array usingthe complete binary tree representation and a chained hash table. The nodes in thehash table are reasonably independent of the problem being solved, requiring onlythat the class have a member function with the signature int lower_bound() constwhich can be called to calculate the lower bound on the distance from the objectto the solution.

The class also tracks the size and the maximum size of the heap (the maximumnumber of objects in the priority queue).

To demonstrate the algorithm and the solution, Figure 7 shows one puzzle for whichthe solution was found using the discrete, Hamming, and Manhattan distances to guidethe A* search.

8 and 15 Puzzles | Algorithms and Data Structures (7)
Figure 7. A permutation of the eight-puzzle.

Dijkstra's algorithm found the minimum solution of 24 moves after havingconsidered 139466 possible solutions (visited 139466 vertices) during the search and the maximum size of the heap was 24154.

Using the Hamming distance, the number of puzzles considered dropped to 127643.This small reduction is almost certainly due to the fact that the Hamming distanceis only really useful in the last stages of finding the solution. The maximumheap size was still 22899.

Using the Manhattan distance, only 2751 vertices were visited and the maximumheap size was 1501.

Solving fifteen-puzzles is much more difficult: the puzzle in Figure 8 has asolution of 50 moves and required that 84702 vertices (different permutations ofthe puzzle) be visited and the maximum heap size was 72340.

8 and 15 Puzzles | Algorithms and Data Structures (8)
Figure 8. A permutation of the fifteen-puzzle.

8 and 15 Puzzles | Algorithms and Data Structures (2024)
Top Articles
MAJGULL Blackout curtains, 1 pair, gray, 57x98" - IKEA
The 10 Best Blackout Curtains to Help You Sleep Better (Despite Your Neighbor's Finicky Flood Lights)
Thor Majestic 23A Floor Plan
Ups Dropoff Location Near Me
Tesla Supercharger La Crosse Photos
Terraria Enchanting
Videos De Mexicanas Calientes
How To Get Free Credits On Smartjailmail
Encore Atlanta Cheer Competition
Computer Repair Tryon North Carolina
Jesse Mckinzie Auctioneer
Cosentyx® 75 mg Injektionslösung in einer Fertigspritze - PatientenInfo-Service
Becky Hudson Free
Cincinnati Bearcats roll to 66-13 win over Eastern Kentucky in season-opener
Signs Of a Troubled TIPM
Watch TV shows online - JustWatch
Morocco Forum Tripadvisor
Keniakoop
Arboristsite Forum Chainsaw
Trac Cbna
Labby Memorial Funeral Homes Leesville Obituaries
Saritaprivate
Uta Kinesiology Advising
Lola Bunny R34 Gif
Amortization Calculator
Woodmont Place At Palmer Resident Portal
Southwest Flight 238
1773x / >
FAQ's - KidCheck
Is Henry Dicarlo Leaving Ktla
Korg Forums :: View topic
Club Keno Drawings
Beth Moore 2023
Litter-Robot 3 Pinch Contact & DFI Kit
What Are Digital Kitchens & How Can They Work for Foodservice
Tds Wifi Outage
Wsbtv Fish And Game Report
Scottsboro Daily Sentinel Obituaries
The Minneapolis Journal from Minneapolis, Minnesota
Rs3 Bis Perks
Linda Sublette Actress
Mytime Maple Grove Hospital
No Boundaries Pants For Men
Home Auctions - Real Estate Auctions
Hkx File Compatibility Check Skyrim/Sse
Fedex Passport Locations Near Me
Go Nutrients Intestinal Edge Reviews
Advance Auto.parts Near Me
San Diego Padres Box Scores
Craigslist Sarasota Free Stuff
Black Adam Showtimes Near Cinemark Texarkana 14
Texas 4A Baseball
Latest Posts
Article information

Author: Saturnina Altenwerth DVM

Last Updated:

Views: 5527

Rating: 4.3 / 5 (64 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Saturnina Altenwerth DVM

Birthday: 1992-08-21

Address: Apt. 237 662 Haag Mills, East Verenaport, MO 57071-5493

Phone: +331850833384

Job: District Real-Estate Architect

Hobby: Skateboarding, Taxidermy, Air sports, Painting, Knife making, Letterboxing, Inline skating

Introduction: My name is Saturnina Altenwerth DVM, I am a witty, perfect, combative, beautiful, determined, fancy, determined person who loves writing and wants to share my knowledge and understanding with you.