Matrix (arrays) in Lua - swap rows and apply row operation - arrays

I have this code
--[[
Gaussian Elimination in Lua
--]]
-- print matrix
function printmatrix(m)
for r=1,#m do
for c=1,#m[r] do
io.write(m[r][c])
if c < #m[r] then io.write(", ") end
end
print() -- print new line
end
end
-- read matrix in CSV format
function readcsv()
local m = {}
while true do
local line = io.read("l") -- read line not including the end of line character
if line==nil or line=="" then break end -- blank line or bad input ends matrix
local row, index = {}, 0
-- the next line is tricky and goes over all entries in the row
for w in string.gmatch(line,"([^,]*),?") do
local v = tonumber(w) -- convert entry to a number
index = index+1
if v==nil then
row[index] = 0 -- default value if we coudn't read the number
else
row[index] = v -- if number is valid
end
end
m[ #m+1 ] = row
end
return m
end
-- determine the size of m and check it is rectangular
function dim(m)
local rows = #m -- number of rows
local cols = 0 -- number of columns
if rows > 0 then cols = #m[1] end
-- check that matrix is rectangular
for i=2,rows do
if cols ~= #m[i] then error("not rectangular!") end
end
return rows, cols
end
-- if m[r][c] is zero swap row r with some row i>r to make m[r][c] nonzero, if possible
function swap(m,r,c)
local nrows, ncols = dim(m)
if r<=0 or r>nrows or c<=0 or c>ncols then error("position out of range") end
if m[r][c] ~= 0 then
-- nothing to do
return
end
-- find a suitable row
local i=r+1
while i <= nrows do
if m[i][c] ~= 0 then break end
i = i+1
end
if i <= nrows then
-- swap rows i,r
-- DO IT!
end
end
-- if m[r][c] is nonzero apply row operations to make each m[i][c]==0 for i>r
function clear(m,r,c)
local nrows, ncols = dim(m)
if r<=0 or r>nrows or c<=0 or c>ncols then error("position out of range") end
if m[r][c] == 0 then
-- nothing to do
return
end
for i=r+1,nrows do
local f = m[i][c] / m[r][c] do
-- apply row_i = row_i - f*row_r
-- DO IT!**
end
end
end
-- apply Gaussian elimination to m to get it into echelon form
function echelon(m)
local nrows, ncols = dim(m)
local r,c = 1,1 -- current position
while r<=nrows and c<=ncols do
-- try to get a nonzero value at this position
swap(m,r,c)
if m[r][c] == 0 then
-- can't, so move right
c = c+1
else
clear(m,r,c)
-- done, so move diagonally
r = r+1
c = c+1
end
end
end
m = readcsv()
print("original:")
printmatrix(m)
echelon(m)
print("echelon form:")
printmatrix(m)
I was hoping someone could clarify on how to write the code (where it says --DO IT! in Lua, I'm fairly new to this thank you
For some context, I'm just experimenting on Gaussian Elimination to try make my work faster during this specific method for computing echelon form -- I'm not too fussed about having 1's as the first non-zero element
It should return this
original:
1, 3, 5, 7
2, 1, -1, 0
3, 4, 4, 7
5, 5, 3, 7
echelon form:
1, 3, 5, 7
0, -5, -11, -14
0, 0, 0, 0
0, 0, 0, 0

t.lua:
--[[
Gaussian Elimination in Lua
--]]
-- print matrix
local function printmatrix (m)
for _, row in ipairs (m) do
io.write (table.concat (row, ', ') .. '\n')
end
end
-- read matrix in CSV format
local function readcsv (file)
io.input (file)
local m = {columns = 0, rectangular = true}
for line in io.lines () do
local row = {}
-- the next line is tricky and goes over all entries in the row
for w in line:gmatch '[^,]+' do
row [#row + 1] = tonumber (w) or 0
end
m [#m + 1] = row
-- Update matrix dimensions
m.rectangular = m.rectangular and (#row == m.columns or #m == 1)
m.columns = #row > m.columns and #row or m.columns
end
return m
end
-- if m[r][c] is zero swap row r with some row i>r to make m[r][c] nonzero, if possible
local function swap (m, r, c)
local nrows, ncols = #m, m.columns
if r <= 0 or r > nrows or c <= 0 or c > ncols then error 'Position out of range' end
if m [r] [c] ~= 0 then
-- nothing to do
return
end
-- find a suitable row
local i = r + 1
while i <= nrows and m [i] [c] == 0 do
i = i + 1
end
if i <= nrows then
m [r], m [i] = m [i], m [r]
end
end
-- if m[r][c] is nonzero apply row operations to make each m[i][c]==0 for i>r
local function clear (m, r, c)
local nrows, ncols = #m, m.columns
if r <= 0 or r > nrows or c <= 0 or c > ncols then error 'Position out of range' end
if m [r] [c] == 0 then
-- nothing to do
return
end
for i = r + 1, nrows do
local f = m [i] [c] / m [r] [c]
for j = 1, #m [i] do
m [i] [j] = m [i] [j] - f * m [r] [j]
end
end
end
-- apply Gaussian elimination to m to get it into echelon form
function echelon (m)
local nrows, ncols = #m, m.columns
local r, c = 1, 1 -- current position
while r <= nrows and c <= ncols do
-- try to get a nonzero value at this position
swap (m, r, c)
if m [r] [c] == 0 then
-- can't, so move right
c = c + 1
else
clear (m, r, c)
-- done, so move diagonally
r = r + 1
c = c + 1
end
end
end
local m = readcsv (arg [1])
print 'Original:'
printmatrix (m)
if m.rectangular then
echelon (m)
print 'Echelon form:'
printmatrix (m)
else
error 'Matrix not rectangular!'
end
t.scv:
1,3,5,7
2,1,-1,0
3,4,4,7
5,5,3,7
lua t.lua t.csv:
Original:
1, 3, 5, 7
2, 1, -1, 0
3, 4, 4, 7
5, 5, 3, 7
Echelon form:
1, 3, 5, 7
0, -5, -11, -14
0, 0, 0, 0
0, 0, 0, 0
You can also use the standard input (lua t.lua; type the values, terminate with Ctrl + D).
To swap anything in Lua, just do a, b = b, a.
I used a straightforward approach to your second doit (apply row_i = row_i - f*row_r): for j = 1, #m [i] do m [i] [j] = m [i] [j] - f * m [r] [j] end
`,
I made your code somewhat more consize and elegant. I also simplified the regular expression used to parse the CSV lines,
I also made matrix's dimensions and rectangularity calculated during its input.

Related

Maximum Sum of elements of array which is not divisible by M, deleting/avoiding at most K consequtive array elements

Given: N, K, M and A(array)
N = No. of elements in the array
K = Maximum consequtive array elements that can be avoided/not considered in the answer
|A| = N
Starting from the last index of the array, you have to find the maximum sum that can be obtained by using the elements of the array, such that at any instant the sum is not divisibe by M. The sum can be aquired by traversing from the last index to the first index of the array (in order), and you have a choice to either include that element into the final answer, or avoid it.
There are two conditions :
When an item is included, the total sum of all elements included till that moment (including the current element that is being included), should not be divisible by M.
When an item is avoided, it has to be kept in mind that you can avoid at most K consequtive array elements at once.
Note : It is strictly required to start from the last index, and skipping any index will count towards the limit on the maximum consequtive elements that can be avoided (i.e K).
If there exists no way to traverse from the last index to the first index by satisfying the two conditions at all instances of the traversal, we have to return back -1, or else return back the maximum sum possible.
Constraints :
2 <= N <= 10^5
1 <= K <= 10
1 <= M <= 20
Example 1 :
N = 5
K = 1
M = 2
A = [1, 2, 3 ,4, 5]
Output : 5 + 4 + 2 = 11
Example 2 :
N = 5
K = 2
M = 2
A = [3, 4, 2, 6, 8]
Output = -1
Example 3 :
N = 7
K = 2
M = 2
A = [1,4,2,6,3,7,7]
Output : 7 + 6 + 2 + 4 = 19
We can do 3-D dynamic programming to solve this, very similarly to the other post. Here's the definition of the formula I'm using:
Let dp[i][k][r], 0 <= i < N, 0 <= k <= K, 0 <= r < M
be the maximum valid sum achievable after processing indices [i, i+1, ..., N-1]
such that index i is our kth consecutive skip and the sum's remainder is r (mod M).
We want max(dp[0][_][_])
dp[i][k][r] = -INF if k + i > N
= -INF if r == 0 and k + i /= N
= -INF if r /= 0 and k + i == N
= 0 if r == 0 and k + i == N
= dp[i+1][k-1][r] if k > 0 and r /= 0
= A[i] + max(dp[i+1][j][(r-A[i]) % M] over all j), if k == 0
The formula is quite long, due to the fact that the initial empty sum of 0 (which is technically divisible by M) is allowed but all others are not. There's also an initial value of the formula not included above: if A[N-1] (the last element) is not divisible by M, then dp[N-1][0][A[N-1]%M] is A[N-1]. You can shorten this formula by two lines, but you do have at least 4 distinct patterns to match.
The time and space complexity are O(NMK), but the space complexity can be reduced to O(MK) by only ever storing the last two rows of your DP table.
Here's a Python computation of that DP formula:
def f(A: List[int], N: int, K: int, M: int) -> int:
assert N == len(A)
if M == 1:
return -1
dp = [[[-math.inf for _ in range(M)] for _ in range(K + 1)] for _ in range(N)]
for i in range(N):
k = N - i
if 0 <= k <= K:
dp[i][k][0] = 0
if A[N - 1] % M != 0:
dp[N - 1][0][A[N - 1] % M] = A[N - 1]
for i in reversed(range(N - 1)):
elem = A[i]
# When k == 0
for r in range(1, M):
for j in range(K + 1):
dp[i][0][r] = max(dp[i][0][r], elem + dp[i + 1][j][(r - elem) % M])
# When k > 0
for k in range(1, K + 1):
for r in range(1, M):
dp[i][k][r] = dp[i + 1][k - 1][r]
ans = max([max(x) for x in dp[0]])
return ans if math.isfinite(ans) else -1
If you'd like to test this code and other answers, plus a slow brute force solution, here's an online code-runner

How to loop through Lua table using key, value, and index

I want to loop through a table that looks like this
function add(t, k, v, ...)
if k ~= nil then
t[k] = v
t[#t+1] = k
return add(t, ...)
end
return t
end
my_table = add({ }, "a", 5, "b", 4, "c", 3)
for i,k in ipairs(my_table) do
local v = my_table[k]
print(k, v)
end
Result:
a - 5
b - 4
c - 3
But I want to be able to loop through the table using the index, the key, and the value, so it looks like this:
1 - a - 5
2 - b - 4
3 - c - 3
Is this possible in Lua?
Iterator:
function triples(t)
local function next_triple(tbl, idx)
idx = idx + 1
local k = tbl[idx]
if k ~= nil then
return idx, k, tbl[k]
end
end
return next_triple, t, 0
end
Usage:
local a = {"q", "w", "e", q = 11, w = 22, e = 33}
for i, k, v in triples(a) do
print(i, k, v)
end
Output:
1 q 11
2 w 22
3 e 33
An alternate implementation of Egor's triples function using coroutines:
function triples(t)
return coroutine.wrap(function()
for i, k in ipairs(t) do
coroutine.yield(i, k, t[k])
end
end)
end

How to store submatrix in array

I have a square matrix B and I want to extract its submatrix which has consecutive row numbers and column numbered 1 through k, with k every natural number no more than n (size of my matrix). It also needs to have non-zero main diagonal entries.
Furthermore, I want to store the submatrices in an array form, (the next step is to check if their determinants are positive, but I won't include that in this question. Here is the code I have built:
for i = 1:n
for j = 1:n-i+1
submat2{i,j} = B([j:j+i-1],[1:i]);
for k = 1:i
maindiag{i,j,k} = prod((submat2{i,j}(i,i) ~= 0));
end
matmaindiag = []
for l = 1:size(maindiag(i,j,:),3)
matmaindiag = [matmaindiag cell2mat(maindiag(i,j,l))]
if prod(matmaindiag ~= 0)
boundsub{end+1} = submat2{i,j};
end
end
end
end
Is there any better way to do this?
For example, if I have:
B =
6 7 8 9
11 12 13 14
0 17 18 19
0 0 23 24
then the submatrices I would like to extract are:
B([1],[1]), B([1],[2]), B([1,2],[1,2]), B([2,3],[1,2]), B([1,2,3],[1,2,3]), B([2,3,4],[1,2,3]), and B itself
since they:
consisted of entries from columns of B that numbered consecutively from 1 through k (the size of the submatrix itself) and
From the consecutively numbered rows of B
Don't have any zero-valued diagonal entries
Thanks for any help and thoughts :)
I have come up with this
n = size(B,1)
for i = 1:n
for j = 1:n-i+1
submat{i,j} = B([j:j+i-1],[1:i]);
end
end
bousub = []
for i = 1:n
for j = 1:n-i+1
dia = diag(submat{i,j});
if (prod(dia) ~= 0)
bousub{end+1} = submat{i,j};
end
end
end

Randoming Numbers is Way too Long [duplicate]

How do you generate a Sudoku board with a unique solution? What I thought was to initialize a random board and then remove some numbers. But my question is how do I maintain the uniqueness of a solution?
Here is the way my own SuDoKu program does it:
Start with a complete, valid board (filled with 81 numbers).
Make a list of all 81 cell positions and shuffle it randomly.
As long as the list is not empty, take the next position from the list and remove the number from the related cell.
Test uniqueness using a fast backtracking solver. My solver is - in theory - able to count all solutions, but for testing uniqueness, it will stop immediately when it finds more than one solution.
If the current board has still just one solution, goto step 3) and repeat.
If the current board has more than one solution, undo the last removal (step 3), and continue step 3 with the next position from the list
Stop when you have tested all 81 positions.
This gives you not only unique boards, but boards where you cannot remove any more numbers without destroying the uniqueness of the solution.
Of course, this is only the second half of the algorithm. The first half is to find a complete valid board first (randomly filled!) It works very similar, but "in the other direction":
Start with an empty board.
Add a random number at one of the free cells (the cell is chosen randomly, and the number is chosen randomly from the list of numbers valid for this cell according to the SuDoKu rules).
Use the backtracking solver to check if the current board has at least one valid solution. If not, undo step 2 and repeat with another number and cell. Note that this step might produce full valid boards on its own, but those are in no way random.
Repeat until the board is completely filled with numbers.
Easy:
Find all solutions with an efficient backtracking algorithm.
If there is just one solution, you are done. Otherwise if you have more than one solution, find a position at which most of the solutions differ. Add the number at this position.
Go to 1.
I doubt you can find a solution that would be much faster than this.
You can cheat. Start with an existing Sudoku board that can be solved then fiddle with it.
You can swap any row of three 3x3 blocks with any other row. You can swap any column of three 3x3 blocks with another column. Within each block row or block column you can swap single rows and single columns. Finally you can permute the numbers so there are different numbers in the filled positions as long as the permutation is consistent across the whole board.
None of these changes will make a solvable board unsolvable.
Unless P = NP, there is no polynomial-time algorithm for generating general Sudoku problems with exactly one solution.
In his master's thesis, Takayuki Yato defined The Another Solution Problem (ASP), where the goal is, given a problem and some solution, to find a different solution to that problem or to show that none exists. Yato then defined ASP-completeness, problems for which it is difficult to find another solution, and showed that Sudoku is ASP-complete. Since he also proves that ASP-completeness implies NP-hardness, this means that if you allow for arbitrary-sized Sudoku boards, there is no polynomial-time algorithm to check if the puzzle you've generated has a unique solution (unless P = NP).
Sorry to spoil your hopes for a fast algorithm!
The solution is divide in to 2 parts:
A. Generating the number pattern 600 billion
B. Generating the masking pattern ~ 7e23 combinations
A ) For Number pattern the fastest way which can generate unique combinations with NO time spent on backtracing or testing
Step 1. Choose an already exiting matrix, I chose the below one as it can be made easily by human without any help from a computing device or solver:
First row is numbers in ascending order
Second row is also in ascending order but start from 4 & roll around
Third row is also in ascending order but start from 7 & roll around
Row 4,5,6: Replace the three cell column with the top right column - 2 5 8 and roll within the 3x3 cell for last column
Row 7,8,9: Replace the three cell column with the top right column - 3 6 9 and roll within the 3x3 cell for last column
1 2 3 4 5 6 7 8 9
4 5 6 7 8 9 1 2 3
7 8 9 1 2 3 4 5 6
2 3 1 5 6 4 8 9 7
5 6 4 8 9 7 2 3 1
8 9 7 2 3 1 5 6 4
3 1 2 6 4 5 9 7 8
6 4 5 9 7 8 3 1 2
9 7 8 3 1 2 6 4 5
Step 2. Shuffle the the digits and replace in all other cells
Step 3. Randomly rearrange columns 1,2 and 3 within themselves
Step 4. Randomly rearrange columns 4,5 and 6 within themselves
Step 5. Randomly rearrange columns 7,8 and 9 within themselves
Step 6. Randomly rearrange rows 1,2 and 3 within themselves
Step 7. Randomly rearrange rows 4,5 and 6 within themselves
Step 8. Randomly rearrange rows 7,8 and 9 within themselves
Step 9. Randomly rearrange in 3 column groups of size 9x3
Step 10. Randomly rearrange in 3 row groups of size 3x9
voila...
5 8 3 1 6 4 9 7 2
7 2 9 3 5 8 1 4 6
1 4 6 2 7 9 3 8 5
8 5 2 6 9 1 4 3 7
3 1 7 4 2 5 8 6 9
6 9 4 8 3 7 2 5 1
4 6 5 9 1 3 7 2 8
2 3 1 7 8 6 5 9 4
9 7 8 5 4 2 6 1 3
B ) For Masking Pattern we need to have a solver algorithm. As we already have a quite unique number grid (which is also solved!) this gives us faster performance for using solver
Step 1: Start with selecting 15 random locations out of the 81.
Step 2: Check with solver whether it has unique solution
Step 3: If solution not unique select additional location. iterate Steps 2 and 3 until unique solution found
This should give you the very unique and fast Sudoku board.
This way you can generate any possible sudoku board as well as any other nxn sudoku board
as for how efficient this algorithm is , it took 3.6 secs to generate a million boards in java & 3.5 sec in golang
Find any filled board of sudoku. (use trivial ones will not affect final result)
int[][] board = new int[][] {
{1,2,3, 4,5,6, 7,8,9},
{4,5,6, 7,8,9, 1,2,3},
{7,8,9, 1,2,3, 4,5,6},
{2,3,1, 5,6,4, 8,9,7},
{5,6,4, 8,9,7, 2,3,1},
{8,9,7, 2,3,1, 5,6,4},
{3,1,2, 6,4,5, 9,7,8},
{6,4,5, 9,7,8, 3,1,2},
{9,7,8, 3,1,2, 6,4,5}
};
for each number from 1 to 9 (say num), (i.e 1, 2, 3, 5, 6, 7, 8, 9) take a random number from range [1 to 9], traverse the board, swap num with your random number.
void shuffleNumbers() {
for (int i = 0; i < 9; i++) {
int ranNum = random.nextInt(9);
swapNumbers(i, ranNum);
}
}
private void swapNumbers(int n1, int n2) {
for (int y = 0; y<9; y++) {
for (int x = 0; x<9; x++) {
if (board[x][y] == n1) {
board[x][y] = n2;
} else if (board[x][y] == n2) {
board[x][y] = n1;
}
}
}
}
Now shuffle rows. Take the first group of 3 rows , shuffle them , and do it for all rows. (in 9 X 9 sudoku), do it for second group and as well as third.
void shuffleRows() {
int blockNumber;
for (int i = 0; i < 9; i++) {
int ranNum = random.nextInt(3);
blockNumber = i / 3;
swapRows(i, blockNumber * 3 + ranNum);
}
}
void swapRows(int r1, int r2) {
int[] row = board[r1];
board[r1] = board[r2];
board[r2] = row;
}
Swap columns, again take block of 3 columns , shuffle them, and do it for all 3 blocks
void shuffleCols() {
int blockNumber;
for (int i = 0; i < 9; i++) {
int ranNum = random.nextInt(3);
blockNumber = i / 3;
swapCols(i, blockNumber * 3 + ranNum);
}
}
void swapCols(int c1, int c2) {
int colVal;
for (int i = 0; i < 9; i++){
colVal = board[i][c1];
board[i][c1] = board[i][c2];
board[i][c2] = colVal;
}
}
swap the row blocks itself (ie 3X9 blocks)
void shuffle3X3Rows() {
for (int i = 0; i < 3; i++) {
int ranNum = random.nextInt(3);
swap3X3Rows(i, ranNum);
}
}
void swap3X3Rows(int r1, int r2) {
for (int i = 0; i < 3; i++) {
swapRows(r1 * 3 + i, r2 * 3 + i);
}
}
do the same for columns, swap blockwise
void shuffle3X3Cols() {
for (int i = 0; i < 3; i++) {
int ranNum = random.nextInt(3);
swap3X3Cols(i, ranNum);
}
}
private void swap3X3Cols(int c1, int c2) {
for (int i = 0; i < 3; i++) {
swapCols(c1 * 3 + i, c2 * 3 + i);
}
}
Now you are done, your board should be a valid sudoku board
To generate a board with hidden values, this can be done using backtracking sudoku algorithm with it try to remove one element from the board until you have a board that is solvable, remove until it will become unsolvable even if you remove only one more element.
if you want to categorised final generated board by difficulty, just count how many numbers are left in board while removing element one by one. The less the number harder it will be to solve
least possible hints in sudoku can be 17, but all possible sudoku board not necessarily reducible to 17 hint sudoku
SWIFT 5 version
The simply way, here my code:
First, create the function into [[Int]] array
func getNumberSudoku() -> [[Int]] {
// Original number
let originalNum = [1,2,3,4,5,6,7,8,9]
// Create line 1 to 9 and shuffle from original
let line1 = originalNum.shuffled()
let line2 = line1.shift(withDistance: 3)
let line3 = line2.shift(withDistance: 3)
let line4 = line3.shift(withDistance: 1)
let line5 = line4.shift(withDistance: 3)
let line6 = line5.shift(withDistance: 3)
let line7 = line6.shift(withDistance: 1)
let line8 = line7.shift(withDistance: 3)
let line9 = line8.shift(withDistance: 3)
// Final array
let renewRow = [line1,line2,line3,line4,line5,line6,line7,line8,line9]
// Pre-shuffle for column
let colSh1 = [0,1,2].shuffled()
let colSh2 = [3,4,5].shuffled()
let colSh3 = [6,7,8].shuffled()
let rowSh1 = [0,1,2].shuffled()
let rowSh2 = [3,4,5].shuffled()
let rowSh3 = [6,7,8].shuffled()
// Create the let and var
let colResult = colSh1 + colSh2 + colSh3
let rowResult = rowSh1 + rowSh2 + rowSh3
var preCol: [Int] = []
var finalCol: [[Int]] = []
var prerow: [Int] = []
var finalRow: [[Int]] = []
// Shuffle the columns
for x in 0...8 {
preCol.removeAll()
for i in 0...8 {
preCol.append(renewRow[x][colResult[i]])
}
finalCol.append(preCol)
}
// Shuffle the rows
for x in 0...8 {
prerow.removeAll()
for i in 0...8 {
prerow.append(finalCol[x][rowResult[i]])
}
finalRow.append(prerow)
}
// Final, create the array into the [[Int]].
return finalRow
}
Then usage:
var finalArray = [[Int]]
finalArray = getNumberSudoku()
It's not easy to give a generic solution. You need to know a few things to generate a specific kind of Sudoku... for example, you cannot build a Sudoku with more than nine empty 9-number groups (rows, 3x3 blocks or columns). Minimum given numbers (i.e. "clues") in a single-solution Sudoku is believed to be 17, but number positions for this Sudoku are very specific if I'm not wrong. The average number of clues for a Sudoku is about 26, and I'm not sure but if you quit numbers of a completed grid until having 26 and leave those in a symmetric way, you may have a valid Sudoku.
On the other hand, you can just randomly quit numbers from completed grids and testing them with CHECKER or other tools until it comes up with an OK.
Here is a way to make a classic sudoku puzzle (sudoku puzzle with one and only solution; pre-filled squares are symmetrical around the center square R5C5).
1) start with a complete grid (using group filling plus circular shift to get it easily)
2) remove number(s) from two symmetrical squares if the cleared squares can be inferred using the remaining clues.
3) repeat (2) until all the numbers are checked.
Using this method you can create a very easy sudoku puzzle with or without programming. You can also use this method to craft harder Sudoku puzzles. You may want to search "create classic sudoku" on YouTube to have a step by step example.
You can start with any valid (filled) puzzle and modify it to produce a completely different one (again, filled). Instead of permutating groups of numbers, you can swap single cells - there will be no similarity whatsoever between the seed puzzle and the resulting puzzle. I have written a simple program long ago in VB, you can find it here: https://www.charalampakis.com/blog/programming-vb-net/a-simple-algorithm-for-creating-sudoku-puzzles-using-visual-basic. It can be translated to any language easily.
Then, randomly and gradually remove cells and check if the puzzle is solvable and has a unique solution. You can also rate the puzzle in terms of difficulty depending on the rules needed for the solution. Continue until removing any known cell leads to an unsolvable puzzle.
HTH
I also think that you will have to explicitly check uniqueness. If you have less than 17 givens, a unique solution is very unlikely, though: None has yet been found, although it is not clear yet whether it might exist.)
But you can also use a SAT-solver, as opposed to writing an own backtracking algorithm. That way, you can to some extent regulate how difficult it will be to find a solution: If you restrict the inference rules that the SAT-solver uses, you can check whether you can solve the puzzle easily. Just google for "SAT solving sudoku".
One way to generate sudoku faster.
find an exist sudoku.
exchange the value with a random group.
exchange the cell or the column or the row-grid or the column-grid.
You exchange the value will make the value different, if not exchange the rows or the column, the sudoku isn't changed in the essential.
You can flag the sudoku with 9 grids, the rows and column exchanged must do in the same grid. Like you can exchange row1-3, row4-6, row7-9, don't exchange row1-4 or row1-7. You can also exchange the row-grid(exchange row1~3 with the row4~6 or row7~9).
Solve the sudoku: record the empty with all the possible value, then check the value from 1 to 9. If one value is unique, remove it from the loop.
You may need code like this:
#pz is a 9x9 numpy array
def PossibleValueAtPosition(pz:[], row:int, col:int):
r=row//3*3
c=col//3*3
return {1,2,3,4,5,6,7,8,9}.difference(set(pz[r:r+3,c:c+3].flat)).difference(set(pz[row,:])).difference(set(pz[:,col]))
def SolvePuzzle(pz:[], n:int, Nof_solution:int):# init Nof_solution = 0
if Nof_solution>1:
return Nof_solution # no need to further check
if n>=81:
Nof_solution+=1
return Nof_solution
(row,col) = divmod(n,9)
if pz[row][col]>0: # location filled, try next location
Nof_solution = SolvePuzzle(pz, n+1, Nof_solution)
else:
l = PossibleValueAtPosition(pz, row,col)
for v in l: # if l = empty set, bypass all
pz[row][col] = v # try to fill a possible value v
Nof_solution = SolvePuzzle(pz, n+1, Nof_solution)
pz[row][col] = 0
return Nof_solution # resume the value, blacktrack
Quick and Dirty, but works:
import numpy as np
import math
N = 3
# rewrite of https://www.tutorialspoint.com/valid-sudoku-in-python
def isValidSudoku(M):
'''
Check a sudoku matrix:
A 9x9 sudoku matrix is valid iff every:
row contains 1 - 9 and
col contains 1 - 9 and
3x3 contains 1 - 9
0 is used for blank entry
'''
for i in range(9):
row = {}
col = {}
block = {}
row_cube = N * (i//N)
col_cube = N * (i%N)
for j in range(N*N):
if M[i][j] != 0 and M[i][j] in row:
return False
row[M[i][j]] = 1
if M[j][i] != 0 and M[j][i] in col:
return False
col[M[j][i]] = 1
rc = row_cube + j//N
cc = col_cube + j%N
if M[rc][cc] in block and M[rc][cc] != 0:
return False
block[M[rc][cc]]=1
return True
def generate_sudoku_puzzles(run_size, seed):
order = int(math.sqrt(run_size))
count = 0
valid = 0
empty = []
np.random.seed(seed) # for reproducible results
for k in range(order):
for l in range(order):
A = np.fromfunction(lambda i, j: ((k*i + l+j) % (N*N)) + 1, (N*N, N*N), dtype=int)
B = np.random.randint(2, size=(N*N, N*N))
empty.append(np.count_nonzero(B))
C = A*B
count += 1
if isValidSudoku(C):
valid += 1
last = C
# print('C(',k,l,') is valid sudoku:')
# print(C) # Uncomment for puzzle
print('Tried', count, 'valid', valid, 'yield', round(valid/count, 3)*100, '%', 'Average Clues', round(sum(empty)/len(empty)))
return(last)
posTest = np.array([(0, 7, 0, 0, 4, 0, 0, 6, 0), \
(3, 0, 0, 5, 0, 7, 0, 0, 2), \
(0, 0, 5, 0, 0, 0, 3, 0, 0), \
(0, 4, 0, 3, 0, 6, 0, 5, 0), \
(6, 0, 0, 0, 0, 0, 0, 0, 8), \
(0, 1, 0, 2, 0, 8, 0, 3, 0), \
(0, 0, 7, 0, 0, 0, 4, 0, 0), \
(1, 0, 0, 8, 0, 2, 0, 0, 9), \
(0, 6, 0, 0, 9, 0, 0, 1, 0), \
])
negTest = np.array([(0, 7, 0, 0, 4, 0, 0, 6, 2), \
(3, 0, 0, 5, 0, 7, 0, 0, 2), \
(0, 0, 5, 0, 0, 0, 3, 0, 0), \
(0, 4, 0, 3, 0, 6, 0, 5, 0), \
(6, 0, 0, 0, 0, 0, 0, 0, 8), \
(0, 1, 0, 2, 0, 8, 0, 3, 0), \
(0, 0, 7, 0, 0, 0, 4, 0, 0), \
(1, 0, 0, 8, 0, 2, 0, 0, 9), \
(0, 6, 0, 0, 9, 0, 0, 1, 0), \
])
print('Positive Quality Control Test:', isValidSudoku(posTest))
print('Negative Quality Control Test:', isValidSudoku(negTest))
print(generate_sudoku_puzzles(10000, 0))
Output:
Positive Quality Control Test: True
Negative Quality Control Test: False
Tried 10000 valid 31 yield 0.3 % Average Clues 40
[[0 0 2 3 0 0 0 7 8]
[7 8 9 1 2 0 0 0 0]
[5 0 0 0 9 0 0 3 0]
[0 0 0 6 7 8 0 0 2]
[0 2 0 0 0 0 7 8 9]
[8 0 0 2 3 0 0 0 0]
[0 0 0 0 0 2 3 0 5]
[0 5 6 0 8 9 1 2 0]
[0 3 0 5 0 0 0 9 0]]
Uncomment the two lines for puzzle source.
Here is a SQL Server stored procedure to generate Sudoku puzzles. I still need to remove values from the completed grid.
CREATE PROC dbo.sp_sudoku
AS
DROP TABLE IF EXISTS #cg
;
WITH cg
AS ( SELECT 1 AS g, 1 AS c, RAND() AS rnd
UNION ALL SELECT 1 AS g, 2 AS c, RAND() AS rnd
UNION ALL SELECT 1 AS g, 3 AS c, RAND() AS rnd
UNION ALL SELECT 2 AS g, 1 AS c, RAND() AS rnd
UNION ALL SELECT 2 AS g, 2 AS c, RAND() AS rnd
UNION ALL SELECT 2 AS g, 3 AS c, RAND() AS rnd
UNION ALL SELECT 3 AS g, 1 AS c, RAND() AS rnd
UNION ALL SELECT 3 AS g, 2 AS c, RAND() AS rnd
UNION ALL SELECT 3 AS g, 3 AS c, RAND() AS rnd)
, cg_seq
AS (SELECT g
, c
, row_number() over (partition by g
order by rnd) AS n
FROM cg)
SELECT g
, c + ((g - 1) * 3) AS c
, n + ((g - 1) * 3) AS n
INTO #cg
FROM cg_seq
--SELECT *
-- FROM #cg
-- ORDER BY g, c
DROP TABLE IF EXISTS #rg
;
WITH rg
AS ( SELECT 1 AS g, 1 AS r, RAND() AS rnd
UNION ALL SELECT 1 AS g, 2 AS r, RAND() AS rnd
UNION ALL SELECT 1 AS g, 3 AS r, RAND() AS rnd
UNION ALL SELECT 2 AS g, 1 AS r, RAND() AS rnd
UNION ALL SELECT 2 AS g, 2 AS r, RAND() AS rnd
UNION ALL SELECT 2 AS g, 3 AS r, RAND() AS rnd
UNION ALL SELECT 3 AS g, 1 AS r, RAND() AS rnd
UNION ALL SELECT 3 AS g, 2 AS r, RAND() AS rnd
UNION ALL SELECT 3 AS g, 3 AS r, RAND() AS rnd)
, rg_seq
AS (SELECT g
, r
, row_number() over (partition by g
order by rnd) AS n
FROM rg)
SELECT g
, r + ((g - 1) * 3) AS r
, n + ((g - 1) * 3) AS n
INTO #rg
FROM rg_seq
--SELECT *
-- FROM #rg
-- ORDER BY g, r
DROP TABLE IF EXISTS #r1
;
WITH r1
AS ( SELECT 1 AS r, 1 AS c, RAND() AS rnd
UNION ALL SELECT 1 AS r, 2 AS c, RAND() AS rnd
UNION ALL SELECT 1 AS r, 3 AS c, RAND() AS rnd
UNION ALL SELECT 1 AS r, 4 AS c, RAND() AS rnd
UNION ALL SELECT 1 AS r, 5 AS c, RAND() AS rnd
UNION ALL SELECT 1 AS r, 6 AS c, RAND() AS rnd
UNION ALL SELECT 1 AS r, 7 AS c, RAND() AS rnd
UNION ALL SELECT 1 AS r, 8 AS c, RAND() AS rnd
UNION ALL SELECT 1 AS r, 9 AS c, RAND() AS rnd)
, r1_seq
AS (SELECT r
, c
, row_number() over (order by rnd) AS n
FROM r1)
SELECT *
INTO #r1
FROM r1_seq
DROP TABLE IF EXISTS #r_tot
;
WITH r2
AS (SELECT r + 1 AS r
, CASE WHEN c > 6 THEN c - 6
ELSE c + 3
END AS c
, n
FROM #r1)
, r3
AS (SELECT r + 1 AS r
, CASE WHEN c > 6 THEN c - 6
ELSE c + 3
END AS c
, n
FROM r2)
, r4
AS (SELECT r + 3 AS r
, CASE WHEN c = 1 THEN c + 8
ELSE c - 1
END AS c
, n
FROM #r1)
, r5
AS (SELECT r + 1 AS r
, CASE WHEN c > 6 THEN c - 6
ELSE c + 3
END AS c
, n
FROM r4)
, r6
AS (SELECT r + 1 AS r
, CASE WHEN c > 6 THEN c - 6
ELSE c + 3
END AS c
, n
FROM r5)
, r7
AS (SELECT r + 6 AS r
, CASE WHEN c = 1 THEN c + 7
WHEN c = 2 THEN c + 7
ELSE c - 2
END AS c
, n
FROM #r1)
, r8
AS (SELECT r + 1 AS r
, CASE WHEN c > 6 THEN c - 6
ELSE c + 3
END AS c
, n
FROM r7)
, r9
AS (SELECT r + 1 AS r
, CASE WHEN c > 6 THEN c - 6
ELSE c + 3
END AS c
, n
FROM r8)
, r_tot
AS (
SELECT * FROM #r1
UNION ALL SELECT * FROM r2
UNION ALL SELECT * FROM r3
UNION ALL SELECT * FROM r4
UNION ALL SELECT * FROM r5
UNION ALL SELECT * FROM r6
UNION ALL SELECT * FROM r7
UNION ALL SELECT * FROM r8
UNION ALL SELECT * FROM r9
)
SELECT *
INTO #r_tot
FROM r_tot
DROP TABLE IF EXISTS #r_tot2
;
SELECT g.n AS r
, r.c
, r.n
INTO #r_tot2
FROM #r_tot r
, #rg g
WHERE r.r = g.r
DROP TABLE IF EXISTS #c_tot2
;
SELECT r.r
, g.n AS c
, r.n
INTO #c_tot2
FROM #r_tot2 r
, #cg g
WHERE r.c = g.c
;
WITH c1 AS (SELECT r, n FROM #c_tot2 WHERE c = 1)
, c2 AS (SELECT r, n FROM #c_tot2 WHERE c = 2)
, c3 AS (SELECT r, n FROM #c_tot2 WHERE c = 3)
, c4 AS (SELECT r, n FROM #c_tot2 WHERE c = 4)
, c5 AS (SELECT r, n FROM #c_tot2 WHERE c = 5)
, c6 AS (SELECT r, n FROM #c_tot2 WHERE c = 6)
, c7 AS (SELECT r, n FROM #c_tot2 WHERE c = 7)
, c8 AS (SELECT r, n FROM #c_tot2 WHERE c = 8)
, c9 AS (SELECT r, n FROM #c_tot2 WHERE c = 9)
SELECT c1.r
, CAST(c1.n AS CHAR(2))
+ CAST(c2.n AS CHAR(2))
+ CAST(c3.n AS CHAR(2))
+ CAST(c4.n AS CHAR(2))
+ CAST(c5.n AS CHAR(2))
+ CAST(c6.n AS CHAR(2))
+ CAST(c7.n AS CHAR(2))
+ CAST(c8.n AS CHAR(2))
+ CAST(c9.n AS CHAR(2)) AS puzzle
FROM c1, c2, c3, c4, c5, c6, c7, c8, c9 WHERE c1.r = c2.r AND c3.r = c2.r AND c4.r = c3.r AND c5.r = c4.r AND c6.r = c5.r AND c7.r = c6.r AND c8.r = c7.r AND c9.r = c8.r
ORDER BY r

redim nested array

i have an array path... this array contains a list of nested array paths. it could look something like this:
path( 0 1 2 3 4 5 6 7 8 )
"1" | 1, 1, 1, 1, 1, 1, 1, 1, 1 |
"2" | 4, 3, 1, 4, 2, 3, 4, 3, 2 |
"3" | 1, 1, , 2, 1, 2, 3, 3, 2 |
"Val" A, B, C, D, E, F, G, H, I
now i have a small loop to get the maximum of the second row.
x = 1
For c = 0 To UBound(path)
If IsArray(path(c)) Then
If CInt(path(c)(x)) <= maxDimension1 Then
maxDimension1 = CInt(path(c)(x))
End If
End If
Next
redim preserve pathValues(maxDimension1 - 1)
i must now find the maximum number of elements for the elements in row "2" and redim the array-Element in pathValues to this.
i tried:
For Dimension2 = 1 To maxDimension1
For c = 0 To UBound(Path)
If IsArray(Path(c)) Then
If CInt(Path(c)(x)) = Dimension2 Then
If CInt(Path(c)(2)) >= maxDimension2 Then
maxDimension2 = CInt(Path(c)(2))
End If
End If
End If
redim PathValues(c)(maxDimension2) //Syntax Error
next
next
is there a way to avoid a workaround with multidimensional array?
for explanation: the pathValues would look like this in the end:
PathValues() = (C,(E, I),(B, F, H),(A, D, G))
I fixed it by recursively calling a function that uses x as "depth" and the full path to create one single array containing empty elements for the values that get written in later.
one just needs to add a statement sorting out the upper bounds you do not want because they belong to other arrays. for all else it works fine
Function iterate_Path(path As Variant, x As Integer, value_x As Variant) As Variant
Dim insideArray, returnPath
returnPath = Array()
For c = 0 To UBound(path)
If IsArray(path(c)) Then
If CInt(path(c)(x)) = value_x Then
If x <> UBound(path(c)) Then
If CInt(path(c)(x)) > UBound(returnPath) + 1 Then
ReDim Preserve returnPath(CInt(path(c)(x)) - 1)
End If
returnPath(path(c)(x) - 1) = iterate_Path(path, x + 1, path(c)(x))
ElseIf CInt(path(c)(x)) > UBound(returnPath) + 1 Then
ReDim Preserve returnPath(CInt(path(c)(x)) - 1)
End If
ElseIf CInt(path(c)(x)) > UBound(returnPath) + 1 Then
ReDim Preserve returnPath(CInt(path(c)(x)) - 1)
If x + 1 = UBound(path(c)) Then
returnPath(CInt(path(c)(x)) - 1) = iterate_Path(path, x + 1, path(c)(x))
End If
ElseIf x + 1 = UBound(path(c)) Then
If CInt(path(c)(x)) > UBound(returnPath) + 1 Then
ReDim Preserve returnPath(CInt(path(c)(x)) - 1)
End If
returnPath(CInt(path(c)(x)) - 1) = iterate_Path(path, x + 1, path(c)(x))
End If
Else
returnPath(CInt(path(c)(x)) - 1) = Empty
End If
Next
iterate_Path = returnPath
End Function

Resources