Skip to main content

Sudoku using dancing link - Java implementation


Here is a algorithm which uses dancing link to solve algorithm X(exact cover) that solves upto 100*100 sudoku. Algorithm has been implemented using  JAVA and can be able to solve Sudoku of any difficulty level(easy,hard,very hard).
Algorithm works as follows:-
1.) Algorithm first find out the number of possible elements in each cell , algorithm for this can be seen in the code.
2.) After this apply rule1 which says finds  that cell which has a single possible element , this works recursively until we find single possible element.
3.) After this apply rule2 which says if there is a number which can go into only one cell, then assign that number to that cell.
4.) Rule2 and rule1 run recursively.
5.) Still if we find possible number of elements in some cell then create a dancing link for sudoku puzzle , reduce the puzzle in to exact cover form and solve it using algorithm X in a efficient way.To make it efficient,first we implemented rule1 and rule2 ,firstly algorithm try to solve sudoku puzzle using rule1 and rule2 recursively and filled some cells which helps us to reduce no of rows when we convert it in to exact cover form.Applying algorithm X will give us a solution.
























Input format :-First line of it take size of sudoku puzzle(say n).i.e Sudoku will be of n2*n2. For an example if we have input say 3 , then 9*9 sudoku will be there.
Next n2 lines will be space seperated sudoku input.
Here is a sample input format for sudoku:-
3
4 0 6 0 0 0 0 2 0
0 2 0 6 8 0 1 0 5
0 1 8 0 0 0 0 0 3
0 8 0 7 0 5 3 0 0
0 0 7 0 3 0 5 0 0
0 0 3 8 0 6 0 1 0
8 0 0 0 0 0 4 7 0
6 0 1 0 7 4 0 3 0
0 4 0 0 0 0 6 0 1

Here is a sample output format:-

----------------------
|4 7 6 |1 5 3 |9 2 8 |
|3 2 9 |6 8 7 |1 4 5 |
|5 1 8 |4 2 9 |7 6 3 |
----------------------
|2 8 4 |7 1 5 |3 9 6 |
|1 6 7 |9 3 2 |5 8 4 |
|9 5 3 |8 4 6 |2 1 7 |
----------------------
|8 3 5 |2 6 1 |4 7 9 |
|6 9 1 |5 7 4 |8 3 2 |
|7 4 2 |3 9 8 |6 5 1 |
----------------------
Time Taken:->0.001 seconds

Code has been uploaded in www.github.com , you can view and download the code from below link:-





Comments

Popular posts from this blog

MonoLithic Vs Microservice Architecture | which Architecture should i choose ?

From last few years ,microservices are an accelerating trend . Indeed, microservices approach offers tangible benefits including an increase in scalability, flexibility, agility, and other significant advantages. Netflix, Google, Amazon, and other tech leaders have successfully switched from monolithic architecture to microservices. Meanwhile, many companies consider following this example as the most efficient way for business growth. On the contrary, the monolithic approach is a default model for creating a software application. Still, its trend is going down because building a monolithic application poses a number of challenges associated with handling a huge code base, adopting a new technology, scaling, deployment, implementing new changes and others. So is the monolithic approach outdated and should be left in the past? And is it worth shifting the whole application from a monolith to microservices ? Will developing a microservices application help you reach you...

C Program to sort an array using insertion sort

I have seen couple of students who use swap in insertion sort to sort an array.Insertion sort is an example of exchange sort ,we don't swap two elements to sort an array ,instead of this we shift elements if ith element is greater than i+1th element to sort an array.It works on simple concept:- Store the ith element in temporary variable and shift element until you find the element smaller than this element towards 0 index position.Here is the C Program to find insertion sort:- _________________________________________________________________________________ #include <stdio.h> #include <string.h> main() {     int arrSize,i;    printf("Enter Size of array:");    scanf("%d",&arrSize);    int array[arrSize];    for(i=0;i<arrSize;i++){        scanf("%d",&array[i]);    }    int temp,j;    for(i=1;i<arrSize;i++){        temp...

Perfect Hashing and its implementation in C

Perfect hashing simply means hashing with no collision.But there is nothing like perfect hashing(no collision).These are just a work around to achieve perfect hashing.Using this we can able to search the large set of records in a constant time i.e O(1) time complexity.To achieve searching in strict O(1) time ,one needs to implement hash table and then search the given record in to hash table using hash function.         Here in perfect hashing ,one need to implement two level hash table , first level hash table consist a set of pointers that points to the second level hash table,we must select hash function that first maps the key in to first level hash table,so it simply means there must be collision in first level hash table ,thats  why i am saying its just a work around to achieve prefect  hashing.We must wisely choose hash function:                  h(x)=((a*x+b)mod p)mod m x is the key to be map, Here ...