Fork/Join Sudoku Solver

539 views Asked by At

I've a problem with an exercise. I need to find all the solutions for a given sudoku, using fork/join parallelism. I made an algorithm but it seems it doesn't work. It stops at some point and I can't figure it out why.

Here's the code:

private static int counter;
private Cella[][] sudoku;
private int i;
private int j;
private int theCounter = 0;

public SudokuMulti(Cella[][] sudoku) {
    this.sudoku = sudoku;
}

public SudokuMulti(Cella[][] sudoku, int i, int j) {
    this.sudoku = sudoku;
    this.i = i;
    this.j = j;
}

//DELETED

// Copy the sudoku matrix
private Cella[][] createCopy() {
    Cella[][] toReturn = new Cella[9][9];
    for (int i = 0; i < 9; i++) {
        System.arraycopy(sudoku[i], 0, toReturn[i], 0, 9);
    }
    return toReturn;
}

And the code for the object Cella:

public class Cella {

private int current;

public Cella() {
    current = 0;
}

public Cella(int current) {
    this.current = current;
}

//Getter and Setter

My idea is to give to each thread the faculty to solve its own sudoku, given the "legal values" of the candidate cell. I then collect all threads in an ArrayList and ask them to fork with the last for. Every thread should return an Integer (0 for no success, 1 for success) in order to count how many possible sudokus can be solved.

However, the algorithm only covers 1/3 of the sudoku: after a certain point, it stops filling the cells and it just returns without completing it.

Can someone suggest me where I'm doing mistake(s) ?

2

There are 2 answers

0
Riccardo Orlando On BEST ANSWER

I've found a solution. Here is the error:

// Copy the sudoku matrix
private Cella[][] createCopy() {
    Cella[][] toReturn = new Cella[9][9];
    for (int i = 0; i < 9; i++) {
        // !!ERROR!!
        System.arraycopy(sudoku[i], 0, toReturn[i], 0, 9);
    }
    return toReturn;
}

When I copy the array I fill it with the Cella object reference and not with a new one, so it causes data races.

The correct way to copy the matrix is:

private Cella[][] createCopy() {
    Cella[][] toReturn = new Cella[9][9];
    for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++) {
            toReturn[i][j] = new Cella(sudoku[i][j].getCurrent());
        }
    }
    return toReturn;
}
1
sadakatsu On

From what code you have posted, I cannot see any issue that explains your problem. However, you have not posted code that I can compile and execute myself (known as a Minimum Working or Verifiable Example, see Wikipedia and StackOverflow's guide on creating one), nor have you posted a stack trace or output for your application. This makes it difficult to help you solve your problem. If you can provide me with more, I am willing to continue helping you with your problem.

In the meantime, I tried to throw together a program that solves the same problem following your approach. It seems to work, though I have not thoroughly unit-tested it. Perhaps you can compare it to what you wrote and use the differences to discover the problem. You will need at least Java 7 to compile and run this code.

If this is for a homework assignment, I recommend checking with your professor or TA before looking at this listing.

public class Main {
    public static void main( String[] args ) {
        Sudoku puzzle = new Sudoku();
          // Uncomment these lines to have a uniquely solvable Sudoku puzzle.  They are commented out to prove that this code can count multiple solutions.
//        puzzle.set(1, 0, 2);
//        puzzle.set(2, 0, 9);
//        puzzle.set(4, 0, 5);
//        puzzle.set(7, 0, 4);
//        puzzle.set(8, 0, 1);
//        puzzle.set(3, 1, 8);
//        puzzle.set(6, 1, 3);
//        puzzle.set(2, 2, 3);
        puzzle.set(3, 2, 7);
        puzzle.set(4, 2, 4);
        puzzle.set(5, 2, 9);
        puzzle.set(6, 2, 6);
        puzzle.set(3, 3, 4);
        puzzle.set(6, 3, 2);
        puzzle.set(7, 3, 1);
        puzzle.set(1, 4, 6);
        puzzle.set(3, 4, 3);
        puzzle.set(4, 4, 7);
        puzzle.set(5, 4, 1);
        puzzle.set(7, 4, 8);
        puzzle.set(1, 5, 4);
        puzzle.set(2, 5, 1);
        puzzle.set(5, 5, 6);
        puzzle.set(2, 6, 5);
        puzzle.set(3, 6, 9);
        puzzle.set(4, 6, 2);
        puzzle.set(5, 6, 8);
        puzzle.set(6, 6, 7);
        puzzle.set(2, 7, 4);
        puzzle.set(5, 7, 7);
        puzzle.set(0, 8, 3);
        puzzle.set(1, 8, 7);
        puzzle.set(4, 8, 6);
        puzzle.set(6, 8, 5);
        puzzle.set(7, 8, 2);

        SudokuSolver solver = new SudokuSolver(puzzle);
        long start = System.nanoTime();
        int totalSolutions = solver.compute();
        long end = System.nanoTime();
        System.out.println(totalSolutions);
        System.out.format("%f ms", (end - start) / 1e6);
    }

    private static class Sudoku {
        private final int[][] cells;

        Sudoku() {
            cells = new int[9][9];
        }

        Sudoku( Sudoku original ) {
            cells = new int[9][9];
            for (int column = 0; column < 9; ++column) {
                for (int row = 0; row < 9; ++row) {
                    set(column, row, original.get(column, row));
                }
            }
        }

        int get( int column, int row) {
            return cells[column][row];
        }

        void set( int column, int row, int value ) {
            cells[column][row] = value;
        }

        boolean isPlausible() {
            return columnsArePlausible() && rowsArePlausible() && blocksArePlausible();
        }

        private boolean columnsArePlausible() {
            boolean result = true;

            for (int column = 0; result && column < 9; ++column) {
                result = isColumnPlausible(column);
            }

            return result;
        }

        private boolean isColumnPlausible( int column ) {
            boolean result = true;

            boolean[] seen = new boolean[10];
            for (int row = 0; result && row < 9; ++row) {
                int value = get(column, row);
                if (value > 0 && seen[value]) {
                    result = false;
                } else {
                    seen[value] = true;
                }
            }

            return result;
        }

        private boolean rowsArePlausible() {
            boolean result = true;

            for (int row = 0; result && row < 9; ++row) {
                result = isRowPlausible(row);
            }

            return result;
        }

        private boolean isRowPlausible( int row ) {
            boolean result = true;

            boolean[] seen = new boolean[10];
            for (int column = 0; result && column < 9; ++column) {
                int value = get(column, row);
                if (value > 0 && seen[value]) {
                    result = false;
                } else {
                    seen[value] = true;
                }
            }

            return result;
        }

        private boolean blocksArePlausible() {
            boolean result = true;

            for (int column = 0; result && column < 9; column += 3) {
                for (int row = 0; result && row < 9; row += 3) {
                    result = isBlockPlausible(column, row);
                }
            }

            return result;
        }

        private boolean isBlockPlausible( int column, int row ) {
            boolean result = true;

            boolean[] seen = new boolean[10];
            for (int x = 0; result && x < 3; ++x) {
                for (int y = 0; result && y < 3; ++y) {
                    int value = get(column + x, row + y);
                    if (value > 0 && seen[value]) {
                        result = false;
                    } else {
                        seen[value] = true;
                    }
                }
            }

            return result;
        }
    }

    private static class SudokuSolver extends RecursiveTask<Integer> {
        private static final long serialVersionUID = 8759452522630056046L;

        private Sudoku state;
        private int column;
        private int row;

        SudokuSolver( Sudoku state ) {
            this.state = state;
            // These settings allow the search loop in compute() to increment first without asking questions about
            // whether this cell has been checked yet.
            column = -1;
            row = 8;
        }

        SudokuSolver( Sudoku state, int column, int row ) {
            this.column = column;
            this.row = row;
            this.state = state;
        }

        @Override
        protected Integer compute() {
            int viableSolutions = 0;

            if (state.isPlausible()) {
                int originalColumn = column;
                int originalRow = row;

                do {
                    if (row + 1 >= 9) {
                        ++column;
                        row = 0;
                    } else {
                        ++row;
                    }
                } while (column < 9 && state.get(column, row) != 0);

                if (column >= 9) {
                    viableSolutions = 1;
                } else {
                    List<SudokuSolver> solvers = new ArrayList<>();
                    for (int value = 1; value <= 9; ++value) {
                        Sudoku copy = new Sudoku(state);
                        copy.set(column, row, value);
                        solvers.add(new SudokuSolver(copy, column, row));
                    }
                    invokeAll(solvers);
                    for (SudokuSolver solver : solvers) {
                        viableSolutions += solver.join();
                    }
                }
            }

            return viableSolutions;
        }
    }
}

Since this code times how long it takes to count the solutions, the output can vary, but I got

354
709.848410 ms