snippetMinor
How to implement graph search to solve Sudoku puzzle
Viewed 0 times
graphsearchimplementsolvehowsudokupuzzle
Problem
My teacher pointed out to us during lectures that we could use Graph Search to help us solve Sudoku puzzles which has left me puzzled .
I dont see how this is possible as Graph Search is mostly about getting from Node A to Node B. He mentioned about how its a directed graph where the nodes correspond to partially completed puzzle
What is the general idea behind using Graph Search to solve Sudoku Puzzle
I dont see how this is possible as Graph Search is mostly about getting from Node A to Node B. He mentioned about how its a directed graph where the nodes correspond to partially completed puzzle
What is the general idea behind using Graph Search to solve Sudoku Puzzle
Solution
A graph is defined abstractly as a set of vertices together with a set of edges connecting those vertices. You have to give the vertices/edges meaning to make sense of the problem. For Sudoku, a vertex is a board configuration and an edge from configuration $a$ to configuration $b$ exists if $b$ is obtained from $a$ by inserting a number in an empty cell of $a$. There are $(9^2)^9$ possible configurations; searching this graph for a solution will take a lot of time but you will find one eventually. Many of the configurations are invalid Sudoku grids so you can eliminate lots of them.
A typical Sudoku solver would search an implicit graph: the solver is not given a graph as input but it is given a procedure to generate candidate configurations given some configuration. One such procedure may be described as:
In the implicit graph, the edges are the ones that look like 'config $\to$ candidate' where config is the input to
A typical Sudoku solver would search an implicit graph: the solver is not given a graph as input but it is given a procedure to generate candidate configurations given some configuration. One such procedure may be described as:
generate_candidates(config) {
candidates = [] is a list of configs
for every empty cell (call it C) in config
for every number in [1..9]
C = number
if config is a valid grid
add copy of config to candidates
}In the implicit graph, the edges are the ones that look like 'config $\to$ candidate' where config is the input to
generate_candidates and candidate is a configuration generated by generate_candidates. Of course you have to generate new candidates from the ones just generated; you keep growing that list of candidates until no more can be generated or you've found a solution.Code Snippets
generate_candidates(config) {
candidates = [] is a list of configs
for every empty cell (call it C) in config
for every number in [1..9]
C = number
if config is a valid grid
add copy of config to candidates
}Context
StackExchange Computer Science Q#22695, answer score: 4
Revisions (0)
No revisions yet.