Determine given Sudoku is a magic square? - matrix

A magic square is a square in which:
1.There is one in the left upper cell.
2. There are no repeating numbers in any column.
3. There are no repeating numbers in any row.
4. There are no repeating numbers in any of the smaller squares.
5.If we swap two smaller squares having a common side, then we obtain square satisfying properties 2 to 4.
Ram has already written several numbers. Determine if it is possible to fill the remaining cells and obtain a magic square.
Example:
Given Matrix:
2 1 3 4
4 3 1 2
1 2 4 3
3 4 2 1
Step 1:
Swapping adjacent smaller squares
3 4 1 2
1 2 3 4
2 1 4 3
4 3 2 1
Step 2:
Swapping adjacent smaller squares(Finally obtaining the magic square)
1 2 3 4
3 4 1 2
2 1 4 3
4 3 2 1
Can anyone explain the logic behind this? Please do tell the easiest way to check whether the magic sudoku formation is possible or not for a given matrix?

Related

How to generate symmetric matrices in Julia?

Functions like rand(m,n) generate a random matrix of m rows and n columns. Is there any function in Julia that can generate a Symmetric matrix of arbitrary dimensions?
You can create a symmetric matrix as you do in rand(m,n), but you will not be able to assign to non-diagonal elements as that might break its symmetry. So, you should create the general matrix first then convert to symmetric.
Symmetric(rand(0:9,5,5))
5×5 Symmetric{Int64, Matrix{Int64}}:
5 2 1 4 1
2 1 6 8 0
1 6 2 0 6
4 8 0 7 1
1 0 6 1 4

Quotient in R to hold position in a matrix

Let's assume these are our numbers and we are looking for mod for them
which we can find them using library(pracma)
> mod(c(1,4,23,13,8,9,11,27,32,2),7)
> [1] 1 4 2 6 1 2 4 6 4 2
I want to get a number to see where each number is coming from when it is a matrix?
1,1,4,2,2,2,2,4,5,1
For example; if this is an m by 7 matrix;
We know that it is on 2nd column but what row? 9 is 2nd row (2,2) but not quotient is 1, then 23 is 4th row (4,2) but quotient is 3. Finally, last element 2 is on (1,2).
I am looking for row position since I can use the mod as a column position.
I came out with this
b=c(1,4,23,13,7,9,11,27,32,2)
floor(b/7+1)
[1] 1 1 4 2 2 2 2 4 5 1

Efficiently construct a square matrix with unique numbers in each row

A matrix of size nxn needs to be constructed with the desired properties.
n is even. (given as input to the algorithm)
Matrix should contain integers from 0 to n-1
Main diagonal should contain only zeroes and matrix should be symmetric.
All numbers in each row should be different.
For various n , any one of the possible output is required.
input
2
output
0 1
1 0
input
4
output
0 1 3 2
1 0 2 3
3 2 0 1
2 3 1 0
Now the only idea that comes to my mind is to brute-force build combinations recursively and prune.
How can this be done in a iterative way perhaps efficiently?
IMO, You can handle your answer by an algorithm to handle this:
If 8x8 result is:
0 1 2 3 4 5 6 7
1 0 3 2 5 4 7 6
2 3 0 1 6 7 4 5
3 2 1 0 7 6 5 4
4 5 6 7 0 1 2 3
5 4 7 6 1 0 3 2
6 7 4 5 2 3 0 1
7 6 5 4 3 2 1 0
You have actually a matrix of two 4x4 matrices in below pattern:
m0 => 0 1 2 3 m1 => 4 5 6 7 pattern => m0 m1
1 0 3 2 5 4 7 6 m1 m0
2 3 0 1 6 7 4 5
3 2 1 0 7 6 5 4
And also each 4x4 is a matrix of two 2x2 matrices with a relation to a power of 2:
m0 => 0 1 m1 => 2 3 pattern => m0 m1
1 0 3 2 m1 m0
In other explanation I should say you have a 2x2 matrix of 0 and 1 then you expand it to a 4x4 matrix by replacing each cell with a new 2x2 matrix:
0 => 0+2*0 1+2*0 1=> 0+2*1 1+2*1
1+2*0 0+2*0 1+2*1 0+2*1
result => 0 1 2 3
1 0 3 2
2 3 0 1
3 2 1 0
Now expand it again:
0,1=> as above 2=> 0+2*2 1+2*2 3=> 0+2*3 1+2*3
1+2*2 0+2*2 1+2*3 0+2*3
I can calculate value of each cell by this C# sample code:
// i: row, j: column, n: matrix dimension
var v = 0;
var m = 2;
do
{
var p = m/2;
v = v*2 + (i%(n/p) < n/m == j%(n/p) < n/m ? 0 : 1);
m *= 2;
} while (m <= n);
We know each row must contain each number. Likewise, each row contains each number.
Let us take CS convention of indices starting from 0.
First, consider how to place the 1's in the matrix. Choose a random number k0, from 1 to n-1. Place the 1 in row 0 at position (0,k0). In row 1, if k0 = 1 in which case there is already a one placed. Otherwise, there are n-2 free positions and place the 1 at position (1,k1). Continue in this way until all the 1 are placed. In the final row there is exactly one free position.
Next, repeat with the 2 which have to fit in the remaining places.
Now the problem is that we might not be able to actually complete the square. We may find there are some constraints which make it impossible to fill in the last digits. The problem is that checking a partially filled latin square is NP-complete.(wikipedia) This basically means pretty compute intensive and there no know short-cut algorithm. So I think the best you can do is generate squares and test if they work or not.
If you only want one particular square for each n then there might be simpler ways of generating them.
The link Ted Hopp gave in his comment Latin Squares. Simple Construction does provide a method for generating a square starting with the addition of integers mod n.
I might be wrong, but if you just look for printing a symmetric table - a special case of latin squares isomorphic to the symmetric difference operation table over a powerset({0,1,..,n}) mapped to a ring {0,1,2,..,2^n-1}.
One can also produce such a table, using XOR(i,j) where i and j are n*n table indexes.
For example:
def latin_powerset(n):
for i in range(n):
for j in range(n):
yield (i, j, i^j)
Printing tuples coming from previously defined special-case generator of symmetric latin squares declared above:
def print_latin_square(sq, n=None):
cells = [c for c in sq]
if n is None:
# find the length of the square side
n = 1; n2 = len(cells)
while n2 != n*n:
n += 1
rows = list()
for i in range(n):
rows.append(" ".join("{0}".format(cells[i*n + j][2]) for j in range(n)))
print("\n".join(rows))
square = latin_powerset(8)
print(print_latin_square(square))
outputs:
0 1 2 3 4 5 6 7
1 0 3 2 5 4 7 6
2 3 0 1 6 7 4 5
3 2 1 0 7 6 5 4
4 5 6 7 0 1 2 3
5 4 7 6 1 0 3 2
6 7 4 5 2 3 0 1
7 6 5 4 3 2 1 0
See also
This covers more generic cases of latin squares, rather than that super symmetrical case with the trivial code above:
https://www.cut-the-knot.org/arithmetic/latin2.shtml (also pointed in the comments above for symmetric latin square construction)
https://doc.sagemath.org/html/en/reference/combinat/sage/combinat/matrices/latin.html

Move square inside large matrix, find minimum number in overlapping

I have a sqaure matrix and a smaller square which moves inside the matrix at all possible positions (does not go out of the matrix). I need to find the smallest number in all such possible overlappings.
The problem is that the sizes of both can go upto thousands. Any fast way to do that?
I know one way - if there's an array instead of a matrix and a window instead of a square, we can do that in linear time using a deque.
Thanks in advance.
EDIT: Examples
Matrix:
1 3 6 2 5
8 2 3 4 5
3 8 6 1 5
7 4 8 2 1
8 0 9 0 5
For a square of size 3, total 9 overlappings are possible. For each overlapping the minimum numbers in matrix form are:
1 1 1
2 1 1
0 0 0
It is possible in O(k * n^2) with your deque idea:
If your smaller square is k x k, iterate the first row of elements from 1 to k in your matrix and treat it as an array by precomputing the minimum of the elements from 1 to k, from 2 to k + 1 etc in each column of the matrix (this precomputation will take O(k * n^2)). This is what your first row will be:
*********
1 3 6 2 5
8 2 3 4 5
3 8 6 1 5
*********
7 4 8 2 1
8 0 9 0 5
The precomputation I mentioned will give you the minimum in each of its columns, so you will have reduced the problem to your 1d array problem.
Then continue with the row of elements from 2 to k + 1:
1 3 6 2 5
*********
8 2 3 4 5
3 8 6 1 5
7 4 8 2 1
*********
8 0 9 0 5
There will be O(n) rows and you will be able to solve each one in O(n) because our precomputation allows us to reduce them to basic arrays.

Permuting rows in an array to eliminate increasing subsequences

The following problem is taken from Problems on Algorithms (Problem 653):
You are given a n x 2 matrix of numbers. Find an O(n log n) algorithm that permutes the rows in the array such that that neither column of the array contains an increasing subsequence (that may not consist of contiguous array elements) longer than ⌈√n.⌉
I'm not sure how to solve this. I think that it might use some sort of divide-and-conquer recurrence, but I can't seem to find one.
Does anyone have any ideas how to solve this?
Heres's my solution.
1) Sort rows according to the first element from greatest to lowest.
1 6 5 1
3 3 -\ 3 3
2 4 -/ 2 4
5 1 1 6
2) Divide it into groups of ⌈√n⌉, and what is left(no more then ⌈√n⌉ groups)
5 1 5 1
3 3 -\ 3 3
2 4 -/
1 6 2 4
1 6
3) Sort rows in each group according to the second element from greatest to lowest
5 1 3 3
3 3 5 1
->
2 4 1 6
1 6 2 4
Proof of correctness:
Increasing subsequences in column 1 can happen only in single group(size is <= ⌈√n⌉),
No 2 elements of increasing subsequences in column 2 are in the same group(no more than ⌈√n⌉ groups)

Resources