Can we generate a valid $9\times 9$ sudoku using this algorithm

algorithmsprobability distributionspuzzlerecreational-mathematics

Begin with a board of $9*9$ cells, each of the cells has no value but is possible to contain a number from $1$ to $9$ (I will call the numbers can be assigned to a cell is guesses; the amount of numbers in a guesses is number of guesses; and a board of $9\times 9$ guesses is guess map). Repeat these following steps:

  1. Determine the collection of cells that have the least number of guesses in the guess map.

  2. Two situations can happen:

    2.1. If the number of guesses is not $1$: randomly choose a cell in the collection, randomly choose a number from the guesses, and mark that number as the value of that cell.

    2.2. If the number of guesses is $1$: we use a queue to handle this. If the number of cells in the collection are more than one, which cell turned to be $1$ (number of guesses is $1$) first will be added to the queue first and so on; in case several cells turned to be $1$ at the same time, randomly choose the order of these cells and add them to the queue. Get the cell at the front of the queue and mark the last guess of that cell as the value of it.

  3. Remove the value of that cell from the guesses of its row, column and its neighbor. For instance, the cell at coordinates $(3,2)$ ($0$-based, the origin is at the top left of the board) and contains number $5$, $5$ will get removed from the guesses of row $3$ and column $2$. The neighbor of $(3,2)$ cell are $3*3$ cells where rows are from $3$ to $5$ and columns are from $0$ to $2$.

Does this strategy work?


Example of the procedure:

Suppose we have a board which has no value and contains the guess map like below:

enter image description here

Step 1: since all the cells have number of guesses of $9$, the collection is all 81 cells.

Step 2: the number of guesses is $9$, not $1$, we will go with step 2.1. The cell that is chosen is $(3,2)$ and the value is $5$, so we write $5$ to that cell.

Step 3: remove $5$ from row $3$, column $2$ and its neighbor. The result is the image below.

enter image description here

Best Answer

The answer is no, because it is possible to set up situations where the exact value of certain cells is not known, but they still restrict the possible values in other cells.

A simple example is something known as a "naked pair". This is a pair of cells in the same row, column or box that share the same two candidate values. For example, suppose that the cells in r1c1 and r2c1 have both been reduced to containing only the values 3 and 5, but it is not known which order.

Whichever value r1c1 takes, r2c1 must take the other - if one is a 3, the other must be a 5 and vice versa. As a result, both 3 and 5 can be eliminated as candidates from every other cell in column 1 (and in the top-left box, in fact). However, your algorithm does not do this, and so it is feasible that it might pick, for example, r8c1 and try to set it as a 5 which will break the sudoku.