0% found this document useful (0 votes)
12 views

Anewalgorithmforgeneratingauniquesolution Sudoku

The document describes a new algorithm for generating unique-solution Sudoku puzzles. Unlike common algorithms, it guarantees a unique solution itself rather than relying on unique-solution tests. The algorithm starts with a filled grid and gradually empties spaces until reaching a unique-solution puzzle. Its time complexity is polynomial, improving on non-polynomial generation algorithms.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Anewalgorithmforgeneratingauniquesolution Sudoku

The document describes a new algorithm for generating unique-solution Sudoku puzzles. Unlike common algorithms, it guarantees a unique solution itself rather than relying on unique-solution tests. The algorithm starts with a filled grid and gradually empties spaces until reaching a unique-solution puzzle. Its time complexity is polynomial, improving on non-polynomial generation algorithms.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/251863893

A New Algorithm for Generating Unique-Solution Sudoku

Article · January 2008


DOI: 10.1109/ICNC.2008.788

CITATIONS READS
2 12,111

5 authors, including:

Yue Wu
University of Strathclyde
60 PUBLICATIONS 267 CITATIONS

SEE PROFILE

All content following this page was uploaded by Yue Wu on 11 May 2015.

The user has requested enhancement of the downloaded file.


Fourth International Conference on Natural Computation

A New Algorithm for Generating Unique-solution Sudoku

Baochen SUN Xiwei SUN Yue WU Yilong YIN Gongping YANG

Shandong University
[email protected]

Abstract 2. Previous work


This paper describes a new algorithm for generating Generally, there are two approaches available, both
unique-solution Sudoku puzzles. Distinguished from based on the solving algorithms, for the puzzle generation:
common algorithms, it guarantees a unique solution itself · Starting with an empty Sudoku square and add
rather than relaying on some unique-solution test preset values one by one until meet some metrics.
algorithms. What is more, the time complexity of our · Staring with a full-filled Sudoku square and remove
algorithm is polynomial, which is a significant progress values until meet some metrics.
as most of the generation algorithms are non-polynomial. When use those algorithms we need to test whether the
new puzzle has a unique solution as every entry added or
1. Introduction moved. This process of common algorithms relies on
some unique-solution test algorithms, which are mostly
Sudoku, an abbreviation for a Japanese phrase, based on the solving algorithms of the Sudoku puzzle.
meaning “the digits must remain single”, is usually a
puzzle presented on a square grid with 9*9 entries, 3. A new generating algorithm
consisting of nine 3*3 regions such that every row,
column and region should be filled with the digits 1 to 9. 3.1. Terminologies and symbols
An example follows:
2 8 4 Strategies Strategies are hints in a Sudoku puzzle,
which can be concluded by applying reasoning. The
5 4 3 7
simplest strategy is to determine the candidates of blank
4
entries according to the constraints of row, column and
1 3 7 5
regions. There are lots of strategies of different levels,
2 9
such as “Naked pair”, “Hidden triple”, “Swordfish” [1],
1 4 8 6
6 5 etc.
9 7 2 NC The number of candidates that can be filled in
one entry and do not cause any inconsistent temporarily.
Figure1. Sudoku puzzle
N{C} Suppose C is one set, then function N{C}
shows the number of elements in C.
7 9 3 5 2 6 8 4 1 Chain A loop, formed by straight lines that link
8 2 6 1 9 4 5 7 3
entries with some two values from 1 to 9, make a chain.
5 1 4 8 3 7 2 9 6
An example follows:
2 4 9 7 1 5 6 3 8
6 8 1 9 4 3 7 2 5
3 7 5 2 6 8 4 1 9
1 5 2 4 8 9 3 6 7
4 3 8 6 7 1 9 5 2
9 6 7 3 5 2 1 8 4
Figure2. Answer for the puzzle above

Through researching existed relevant information, all


the generating algorithms are based on solving algorithms
of the puzzle. We present a much more direct generating Figure3. A chain consist of 5s and 9s
algorithm, which don’t rely on the solving algorithms to We also have some relevant conclusions:
guarantee the uniqueness of solution. ·Every two values from 1 to 9 in a certain full-filled
Sudoku square have at least one chain.

978-0-7695-3304-9/08 $25.00 © 2008 IEEE 215


DOI 10.1109/ICNC.2008.788
· All the straight lines that link entries filled with 3. Find the entry whose NC is the most, move it to
some two values, form chains exactly. S11, turn to step 1.
·Suppose in a full-filled Sudoku square, we moved 4. When N {S12} =0, move all the entries in S’ to
all the entries of a given chain. As long as one entry S12 and the algorithm ends.
refilled, sometimes even with no entry refilled, the chain Based on Greedy technique, we can conclude that N
could be recurred by reasoning. {S12} increasing during running the greedy eliminating
algorithm.
3.2. Puzzle generate algorithm As the generation algorithm executes N {S1} =18+9*i
after running i times of greedy eliminating algorithm, so
the process eventually leads to N {S1} =81. A unique-
We designate the 81 entries of a given Sudoku square
solution Sudoku puzzle therefore generated.
as the universal set S, and another pair of subsets which
are complementary: one is named S1 while the other is S2.
In addition, S1 is divided into two complementary 3.3. Proof of the correctness of the generation
subsets: S11 represents all the filled entries in S1 while S12 algorithm
represents all the blank entries in S1. If we put some filled
entries into S12, it indicates these entries are blank Initialization: Initially, N {S1} =18, and so the
automatically. Similarly, if we put some empty entries invariant is trivially true.
into S11, it indicates these entries will be filled again with Maintenance: When each iteration the generation
the initial values. There is one important property of S1: algorithm runs, N {S1} increased by 9. Because of the
the entries in S11 can uniquely determine the value of greedy eliminating algorithm, each time N {S1}
all the entries in S12. increased the property of S1 is warranted.
Essentially, our algorithm starts with a full-filled Termination: At termination, N {S1} =81. Thus, a
Sudoku square. Unlike the common algorithms, we do unique-solution Sudoku puzzle generated.
not need unique-solution test algorithms. We generate a
Sudoku puzzle through expanding S1 to the universal set 3.4. Complexity of the generation algorithm
S. As the property of S1, when S1 equals S a unique-
solution Sudoku puzzle generated. Though the data of the algorithm is very small, it is
safe that we do not consider the space complexity.
3.2.1. Pseudocode of the generation algorithm The time complexity of the inner algorithm—the
greedy eliminating algorithm is polynomial since the time
1. First search for the longest chain in the given complexity of applying each strategy is polynomial. And
square, and put all 18 entries filled with the the times of executing the outer algorithm is constant (8
related two values into S1. Suppose that these times), so the overall time complexity of this algorithm is
entries also form j chains besides the initial chain. polynomial.
We withhold the value of one entry and empty all
the other entries in each of (j+1) chains. Then 3.5. Metrics of difficulty
there are (j+1) entries in S11, and (18-(j+1))
entries in S12. We can use many metrics to define the difficulty levels,
2. Then randomly, we put another 9 entries of some such as the number of blank entries and the position of
value into S12. To guarantee the property of S1, the blank entries [1]. As the generation algorithm
we move some entries from S12 to S11 by terminates, when the most difficult puzzle generated, we
applying greedy eliminating algorithm (explained should refill some entries according to the difficulty level
below). needed.
3. Do step 1 to 2 till S1 =S.
3.6. Extension
3.2.2 Pseudocode of the greedy eliminating
algorithm When each iteration the algorithm described above
runs, N {S1} increased by 9. We can easily expand the
We set up a temporary set S’ to store all the entries algorithm that each iteration N {S1} increased by 9*k (k
whose values can be determined by strategies. is a natural number that lower than 7). By comparisons
1. Determine candidates of each entry in S12 by of those algorithms, we can find the most feasible
applying strategies. algorithm.
2. If NC of some entry is 1, move it to S’ and refill
the entry, turn to step 1.
4. Conclusion

216
[1] Wei-Meng Lee, Programming Sudoku, Apress,
In this paper we present a brand-new algorithm for Berkeley, CA, USA, 2006
generating unique-solution Sudoku puzzles. Its [2] Thomas H. Cormen, Charles E. Leiseison, Ronald L.
complexity is practicable and it is very easy to expand. Rivest, Clifford Stein, Introduction to Algorithms, Second
Edition, MIT press, Cambridge, MA, USA, 2001
5. References

217

View publication stats

You might also like