Skip to content
Advertisement

Running out of java heap space- 15 puzzle problem

G’day all, I tried the solution for eight puzzle problem posted here by joel Neely and played around with it and modified it so that can be used to solve for higher grids[Changed the String representation of the grid to two dimensional integer representation and modified the logic accordingly]. However the modified code can solve the 3×3 grids but quickly run out of heap space for 4×4 grid. I guess this is the restriction caused by the algorithm used which i think is some variation of branch and bound and not that of java. If my assumptions are right, can someone suggest any other good algorithms for solving this problem?. If not, please hint what can be done to make this program work for higher order grids.

JavaScript

Thanks in advance, paul.

Advertisement

Answer

Your algorithm lacks a heuristic. In other words, it’s exploring the search space with no guidance. For the 15-puzzle, that space is quite large, close to 3**(depth of the solution).

If you order your queue by the sum of the Manhattan distance of each tile from its destination, it might be sufficient to make it solvable. At each step, expand the item on the agenda with the least “error”.

Also, are you sure the start state you’ve chosen is solvable? If you randomly order the tiles, you only have a 50-50 chance.

Finally, you might switch from Integer to byte to save memory. How much depends on the java implementation, but since Integer is a class and byte a primitive type, it could be significant.

Updated

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement