How to Parallelize Backtracking Algorithm using threads

Viewed 133

I wrote a sudoku solver program implementing a backtracking algorithm. The code works well iteratively but I want to make it parallel using threads and not open mp. After a deep analysis I know I have to change the logic of my implementation but I can't understand how can I parallelize a complete search algorithm and how to make the threads not repeat the same work as other threads. Can someone help me? here is my code.

#include <vector>
using namespace std;


void printSudoku9x9(int sudoku[9][9]) {
    for (int y = 0; y < 9; y++) {
        for (int x = 0; x < 9; x++)
            cout << sudoku[y][x] << " ";
        cout << endl;
    }

}

bool canPlace9x9(int sudoku[9][9], int row, int col, int n)
{
    if (sudoku[row][col] != 0) return false;
    bool status = true;
    int gridx = (col / 3) * 3;
    int gridy = (row / 3) * 3;
    for (int i = 0; i < 9; i++) {
        if (sudoku[row][i] == n) { status = false; break; }
        if (sudoku[i][col] == n) { status = false; break; }
        if (sudoku[gridy + i / 3][gridx + i % 3] == n) { status = false; break; }
    }
    return status;
}

void nextEmpty(int sudoku[9][9], int row, int col, int& rowNext, int& colNext)
{

    int indexNext = 9 * 9 + 1;
    for (int i = row * 9 + col + 1; i < 9 * 9; i++) {
        if (sudoku[i / 9][i % 9] == 0) {

            indexNext = i;
            break;
        }
    }
    rowNext = indexNext / 9;
    colNext = indexNext % 9;
}

void copyArray(int sudoku[9][9], int sudokuCopy[9][9]) {
    for (int y = 0; y < 9; y++)
        for (int x = 0; x < 9; x++)
            sudokuCopy[y][x] = sudoku[y][x];
}
std::vector<int> findPlaceables(int sudoku[9][9], int row, int col) {
    vector<int> placebles = {};
    for (int n = 1; n <= 9; n++)
        if (canPlace9x9(sudoku, row, col, n)) placebles.push_back(n);
    return placebles;
}


bool solveSudoku9x9(int sudoku[9][9], int row, int col)
{
    if (row > 8) return true;
    if (sudoku[row][col] != 0) {
        int rowNext, colNext;
        nextEmpty(sudoku, row, col, rowNext, colNext);
        return solveSudoku9x9(sudoku, rowNext, colNext);
    }

    std::vector<int> placebles = findPlaceables(sudoku, row, col);

    if (placebles.size() == 0) {
        
        return false;
    
    };

    bool status = false;
    for (int i = 0; i < placebles.size(); i++) {
        int n = placebles[i];
        int sudokuCopy[9][9];
        copyArray(sudoku, sudokuCopy);
        //cout << "(" << row << "," << col << ") =>" << n << endl;
        sudokuCopy[row][col] = n;
        int rowNext = row;
        int colNext = col;
        nextEmpty(sudokuCopy, row, col, rowNext, colNext);
        if (solveSudoku9x9(sudokuCopy, rowNext, colNext)) {
            copyArray(sudokuCopy, sudoku);
            status = true;
            break;
        }
    }
    return status;
}


int main(int argc, char** argv)
{
    int board[9][9] = {
        {5,3,0,0,7,0,0,0,0},
        {6,0,0,1,9,5,0,0,0},
        {0,9,8,0,0,0,0,6,0},
        {8,0,0,0,6,0,0,0,3},
        {4,0,0,8,0,3,0,0,1},
        {7,0,0,0,2,0,0,0,6},
        {0,6,0,0,0,0,2,8,0},
        {0,0,0,4,1,9,0,0,5},
        {0,0,0,0,8,0,0,7,9}
    };
    int board2[9][9] = {
        {8,0,0,0,0,0,0,0,0},
        {0,0,3,6,0,0,0,0,0},
        {0,7,0,0,9,0,2,0,0},
        {0,5,0,0,0,7,0,0,0},
        {0,0,0,0,4,5,7,0,0},
        {0,0,0,1,0,0,0,3,0},
        {0,0,1,0,0,0,0,6,8},
        {0,0,8,5,0,0,0,1,0},
        {0,9,0,0,0,0,4,0,0}
    };
    
    if (solveSudoku9x9(board, 0, 0)) cout << "successfully solved board!" << std::endl;
    printSudoku9x9(board);
    return 0;
}
0 Answers
Related