views:

647

answers:

4

Hello there, i've been trying to write a java class to solve the n queens problem using some sort of stacking and recursion, the answers are stored in grids(two dimensionnal arrays), but i've hit a dead wall which is stack overflow for recursion at n=8 (max recursion depth reached 2298) So i've been wondering if there is some way to bypass this dead by doing something complex like allocating more heap space in java(if possible?) or using multi thread(point me out to a tutorial/examples)... or please do advice on how to optimize the code... Thanks in advance

    public void resoudre(){

        this.gridPile.push(copyGrid(grid));
        try{
            int row = gridPile.size()-1;
            if(gridPile.size()==0)row = 0;
            chooseGridSpace(this.grid, locateFirstAvailable(grid, row));
            if(gridPile.size() == this.taille){
                gridSolutions.push(copyGrid(grid));
                grid = gridPile.pop();
                boolean erronous = true;
                while(erronous){
                    try{
                        MakeNextUnavailable(grid, gridPile.size());
                        erronous = false;
                    }
                    catch(UnavailabilityException r1){
                        try{
                            grid = gridPile.pop();
                        }
                        catch(java.util.EmptyStackException r2){
                            return;
                        }
                    }
                }
            }

        }
        catch(InvalidPositionException e1){
            this.grid = gridPile.pop();
            boolean error = true;
            while(error){
                try{
                    MakeNextUnavailable(grid, gridPile.size());
                    error = false;
                }
                catch(UnavailabilityException er){
                    try{
                        this.grid = gridPile.pop();
                    }
                    catch(java.util.EmptyStackException err){
                        return;
                    }
                }
            }
        }
        catch(java.lang.ArrayIndexOutOfBoundsException e2){
            return;
        }
        this.resoudre();
    }

    private static void chooseGridSpace(int[][] grid, Position a){
        grid[a.getLigne()][a.getColonne()] = 1;
        fillNotAvailable(grid, a);
    }
A: 

In Java, the commands -ss and -oss can both be used to change the stack size.

$java -ss156k (native) 
$java -oss600k (Java)

The argument is the stack size you would like in kbytes or mbytes. Experiment with some increased values until you don't overflow.

Dan Lorenc
your answer was helpful in bypassing my problem for n=8 and 9 but i bumped into the dead wall again, but once i eliminated the recursive structure in the solve(resoudre) function i did not need to use those commands... But thanks for help :)
A: 

Reading the code, it looks like your program is using a Stack<..> and not Java recursion. Therefore it is probably running out of Java heap space rather than Java stack space. If that is the case, you can use the -Xms and -Xmx options to increase the initial and maximum heap sizes.

Stephen C
But @vos is right, your algorithm is inelegant and horribly inefficient, and fixing that is better that throwing more memory at the problem.
Stephen C
+2  A: 
vog
Hello, thanks for your time... i must concur your code is very eloquent and concise, and my first try failed because of using a memory consuming iterative structure in a recursive way... But once i eliminated the recursivity, i reaped the benefits of my not so concise code... for example, for n=10, the highly eloquent runs for more than 3 mins(then i stopped it) while my code took 3 seconds and it outputted all the solutions... @stephen c, now i can say i highly disagree ;)
This has nothing to do with concise vs. non-concise! It's just my isSolution() function which is somewhat stupid. It checks stuff in the wrong order and doesn't perform checks of partial solutions. My code would grow about 1-2 lines if you optimize it.
vog
A: 

No reason to reach stack depth of 2298 for N = 8!

The correct algorithm is to represent the queens by an array of 8 integers, each representing the row position of the ith queen (queen per column).

Your max stack size should be 8.

ripper234
I reached a maximum recursion depth of 2298... not a stack depth of 2298... but the stack size is lower than n..