### Algorithms

We now present an algorithm that finds all such chains in polynomial time. The first stage is to find the best bipartite matching using the Ford Fulkerson maximum flow algorithm or any other bipartite matching algorithm (see *Introduction to Algorithms*, Second Edition, by Thomas H. Cormen et al., MIT Press, 2001).

Let `W`

be the set of `n`

cells in a row, column, or subsquare.

That means that there are `n`

cells or vertices in `W`

when the entire Sudoku puzzle has `n×n`

cells or vertices. Let `D`

be the set of numbers that are matched to `W`

. In an `n×n`

Sudoku, `D`

is the set of numbers 1,2,3,...,`n.`

Let `S`

_{k} be a subset of `k`

cells out of the `n`

cells in which the possible numbers are exactly `k<n`

numbers `T`

_{k}. We say that `S`

_{k }is a "chain" if it doesn't contain a smaller set `Q`

_{r} such that `r<k`

to which only `r`

numbers can be matched. Moreover, in our model, if the number `v`

in `D`

can be matched to the cell `u`

in `W,`

then we say that our Permutation Bipartite Graph `G`

contains the edge ∈ =(`u,v`

) between the vertex `u`

from `W`

and `v`

from `D`

. In addition, let `M`

denote a maximal match between `W`

and `D.`

A match is a set of edges connecting cells or vertices in `W`

to numbers or vertices in `D`

. Each cell in `W`

is allowed to have only one edge in `M`

that connects it to a number in `D`

.

Each number in `D`

is allowed to have only one edge in `M`

that connects it to a cell or vertex in `W`

. Let `E`

denote the entire set of possible vertices between `W`

and `D`

.

Because the vertices of `S`

_{k} are only connected to vertices of `T`

_{k}, the condition |`M`

|=|`W`

| implies that each element `s∈`

is matched to exactly one element `S`

_{k}`t ∈ T`

and vice versa. Start scanning a vertex _{k}`u`

. Suppose _{1} ∈ W`u`

_{1}∈`S`

_{k}. We check all the vertices `v`

_{i1} in `D`

such that the edges (`u`

_{1},`v`

_{i1}) exist in `E`

. Because `S`

_{k} is connected only to `T`

_{k},`v`

_{i1} are all in `T`

_{k}. We mark `u`

_{1} such that we will not consider `u`

_{1} again. We remove `u`

_{1} from the list of vertices the algorithm will visit. Now we look for all the edges (`u`

_{i1},`v`

_{i1}) in `M`

that are connected to one of these vertices `v`

_{i1}. Obviously, `u`

_{i1} are in `S`

_{k} for all indices `i`

_{1} because all the vertices in `T`

_{k} are matched by `M`

to vertices in `S`

_{k}. We continue the process recursively. Now, for each `u`

_{i1} we look for all `v`

_{i2} in `D`

that are connected to at least one of the `u`

_{i1} vertices by edges. We mark `u`

_{i1 }such that these vertices will not be considered again. Again, the `v`

_{i2} must be in `T`

_{k} and we continue and look for all the edges (`u`

_{i2},`v`

_{i1})∈`M`

. Obviously, `u`

_{i2} are in `S`

_{k}.

Because `u`

_{1} and the vertices `u`

_{i1 }were removed from the list of vertices that the algorithm visits, there are fewer vertices that can be visited by the algorithm. The process is repeated until all the vertices in `W`

it can visit are reached.

Because all the vertices it can reach are in `S`

_{k}, what is left to prove is that there is no vertex `u`

in `S`

_{k} that is not visited by the algorithm. Suppose there is such a vertex `u.`

Then, obviously, the vertex `v`

in `T`

_{k }that is matched to `u`

could not be visited either; otherwise, `u`

could be visited. But then, `v`

is also not connected to any one of the vertices in `S`

_{k} that were visited. So we can define `Q`

_{k-1}=`S`

_{k}-{`u`

}, which is connected to |`S`

_{k}|-1 vertices in `D.`

That is a contradiction to the requirement that `S`

_{k} is minimal.

A simpler algorithm is the Pile algorithm. Let `G`

be a Permutation Bipartite Graph. We would like to find if there exist vertices `v`

_{i1},...,`v`

_{ik} in `D`

such that they are all connected to the same `k`

vertices in `W`

, `u`

_{j1},...,`u`

_{jk}. Such vertices are called "Pile" or "Set." The algorithm is trivial. Start traversing the vertices `v`

_{k} in `D`

serially in a loop. Activate a second loop within the first loop and count all the other vertices in `D`

that are connected to the same `k`

vertices `u`

_{j1},...,`u`

_{jk} in `W`

that `v`

_{k} is connected to. If there are `k`

such vertices `v`

_{i1},...,`v`

_{ik} in `D`

, then you are done. Although this algorithm can be improved, it is efficient and its runtime is |`W`

|^{2}/2.

After finding a chain, the algorithm can erase all the edges that are connected to the vertices in `T`

_{k }that are not connected to `S`

_{k}. This operation is called "Chain Exclusion." That is, if ∈=(`u,v`

) and `v∈T`

_{k} and `u∈S`

_{k }, then ∈ is removed from the Permutation Bipartite Graph `G.`

After finding a Pile, remove all the edges that connect `u`

_{j1},...,`u`

_{jk} to `D-`

{`v`

_{i1},...,`v`

_{ik}}.

### Implementation

We've used the Chain Exclusion and Pile Exclusion algorithms described here to build a Windows-based Sudoku solver in Visual Studio C++ 6.00. Executables and the complete source code are available electronically here. You will need to rename the file Sudoku.ex1 to Sudoku.exe. Alternatively, you can compile the entire project Sudoku.dsw or Sudoku.dsp with Visual Studio 6.00. We've included a simple console application, Logic.ex1, that demonstrates Chain Exclusion. This file should be renamed to Logic.exe. In Sudoku.exe, the Logic button calls the class Square3.cpp, and Square3.cpp calls Bipartite.cpp. The Logic button fills as many cells as possible with numbers, by using logic only. You can generate puzzles that usually have more than one solution by clicking on the Test button when the Unique option doesn't have a check mark. If the Unique option is checked, the Sudoku puzzle is solved by logic only. The Very Hard option automatically checks the Unique option and takes 5 seconds. You can also enter your own puzzle and press either the Logic button or the OK button. If the puzzle can't be solved because of contradictions, OK won't fill it and Logic will fill as many cells as possible. When the contradiction is encountered, it inserts a double number in row, column, or square. That can be verified by the Check button.

In addition, Sudoku.exe also solves illogical puzzles via the OK button that calls recursion that in each iteration fills in numbers in locations in which there are less degrees of freedom. For more information on Sudoku puzzles, see the Daily Sudoku) or the *Times Online*).

*Eytan, Raviv, and Eitan are software engineers in Israel. They can be contacted at esuchar[email protected], [email protected], and [email protected], respectively. *