'Divide backtracking into parallel processes
I am trying to solve sudoku 25*25 using the backtracking process in c. Is there any way to divide backtracking into multiple parallel processes so that I could use them as threads?
Solution 1:[1]
Backtracking implies a depth-first search.
A DFS will take on the following form:
- Create a stack of states.
- Push the initial state onto the stack.
- While the stack isn't empty,
- Pop a state from the stack.
- If the search is satisfied by the state,
- Return the state.
- For each possible derived state,
- Push the derived state onto the stack.
- Return a failure.
(Recursion is often used to provide the stack, but that's no requirement.)
In the context of a Sudoku solver,
- Create a stack of states.
- Push the initial state onto the stack.
- While the stack isn't empty,
- Pop a state from the stack.
- If the search is satisfied by the state,
- Return the state.
- Find an empty square.
- For each possible value in the square,
- Clone the current state.
- Set the value for the square in the cloned state.
- Push the cloned state onto the stack.
- Invalid Sudoku
I would use a breadth-first search, though. It's identical to the above, except a queue is used instead of a stack.
We can easily convert that to a worker-based model. Just use a thread-safe queue, and a bunch of threads executing the loop.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ikegami |
