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...

Long-Polling vs WebSockets vs Server-Sent Events

Long-Polling vs WebSockets vs Server-Sent Events  Long-Polling, WebSockets, and Server-Sent Events are popular communication protocols between a client like a web browser and a web server. First, let’s start with understanding what a standard HTTP web request looks like. Following are a sequence of events for regular HTTP request: Client opens a connection and requests data from the server. The server calculates the response. The server sends the response back to the client on the opened request. HTTP Protocol Ajax Polling Polling is a standard technique used by the vast majority of AJAX applications. The basic idea is that the client repeatedly polls (or requests) a server for data. The client makes a request and waits for the server to respond with data. If no data is available, an empty response is returned. Client opens a connection and requests data from the server using regular HTTP. The requested webpage sends requests to...

Installation of OpenCV2 / OpenCV3 with Python and Anaconda

This is first tutorial of the series beginning with installation instruction of opencv2 / opencv3 in python anaconda virtual Environment . We will come with lot of exciting blog like face detection and recognition in video/image/livestream , object or people tracking etc , So stay tuned and subscribe for more updates . We are installing it on MAC OS , you need below tools to setup OpenCV: Xcode Homebrew Anaconda OS X Step 1: Install Xcode Go to App Store , Search for Xcode Install it . Step 2: Install HomeBrew Open Terminal(Application->Utilites->terminal) Write this following in terminal: ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install )" brew install python Step 3: Install Anaconda Python Package Follow the Installation instructions, should be pretty standard, however Continuum has a guide here . Type Conda Info and check Installation . click here for Anaconda cheat sheet . Step 4: Create Conda Virtual Env and setup OpenCV You...