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

Dsa Report

The document is a project report submitted by Arnav Kumar and Anamika Kumari for their Bachelor's degree in Computer Science from Chandigarh University. It details the development of a Sudoku solver using recursive backtracking. The report includes an introduction to Sudoku and the problem definition, a literature review of relevant data structures and algorithms, a description of the methodology and implementation of a recursive backtracking approach, an analysis of results, and references.

Uploaded by

welasi3444
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Dsa Report

The document is a project report submitted by Arnav Kumar and Anamika Kumari for their Bachelor's degree in Computer Science from Chandigarh University. It details the development of a Sudoku solver using recursive backtracking. The report includes an introduction to Sudoku and the problem definition, a literature review of relevant data structures and algorithms, a description of the methodology and implementation of a recursive backtracking approach, an analysis of results, and references.

Uploaded by

welasi3444
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Sudoku Solver

Using Recursive Backtracking

A PROJECT REPORT

Submitted by

ARNAV KUMAR (22BCS12122)


ANAMIKA KUMARI (22BCS12113)

in partial fulfillment for the award of the degree of

BACHEOLERS OF ENGINEERING
IN
COMPUTER SCIENCE

Chandigarh University

July, 2023

1
ACKNOWLEDGEMENT
I would like to extend my heartfelt gratitude to all those who have played a significant role in the
successful completion of this case study on data structures. Their support, guidance, and encouragement
have been invaluable throughout this journey. I am deeply thankful to Paramjot ma’am for her guidance and
mentorship. Her expertise and insights were instrumental in shaping the research and refining the report.
I would also like to express my appreciation to my fellow classmates for their valuable discussions and
feedback during the course of this study. Their input and constructive criticism have contributed to the
quality of this work. Furthermore, I extend my thanks to the Chandigarh University for providing the
necessary resources and a conducive environment for research and learning. I am grateful to the
individuals who participated in interviews, surveys, or discussions, sharing their experiences and
insights. Their willingness to contribute to this study is greatly appreciated.
Lastly, I want to acknowledge the unwavering support of my friends and family. Their patience,
encouragement, and understanding were vital in enabling me to devote time and effort to this research.
This case study would not have been possible without the collective support and collaboration of all
those mentioned above. Thank you for being a part of this journey.

Anamika Kumari(22BCS12113)

Vishal Kumar(22BCS12097)

2
TABLE OF CONTENTS
List of Figures 3

CHAPTER 1. INTRODUCTION....................................................................................................... 4

1.1 Introduction to Sudoku Solver……………………………………………………………………


1.2 Problem definition………………………………………………………………………………..

CHAPTER 2. Literature review……………………………………………………………,,,........5

2.1 Data Structure Used………………………………………………………………………………

CHAPTER 3. Methodology & implementation 6

3.1 Hypothesis………………………………………...……………………………………………...

3.1.1 Hypothesis of Recursive Backtracking…………………………………………………………

3.2 Methodology……………………………………………………………………………………...

3.3 Implementation……………………………………………...........................................................

CHAPTER 4.Result Analysis............................................................................................................ 7

4.1 Result Analysis ………………………………………………………….……………………….

4.2 Key Area Analysis…………………………………………………………………….………….

4.3 Complexity Analysis …………………………………………………………………………….

REFERENCES................................................................................................................................ 8

3
CHAPTER 1 INTRODUCTION

1.1 Introduction to Sudoku Solver


A Sudoku solver is a program or algorithm that uses logical rules, recursion, and backtracking
to fill a 9x9 grid with digits, ensuring each row, column, and 3x3 subgrid contains unique
numbers from 1 to 9.
A Sudoku solver using recursive backtracking explores possible digit placements,
backtracking when conflicts arise, until a valid solution is found or all options are exhausted,
ensuring each row, column, and subgrid has unique digits.
Every Sudoku player has ever got stuck on the same puzzle for hours. If you are tired of
looking at an unfinished Sudoku puzzle without ideas how to proceed, our Sudoku Solver can
help you! The Sudoku Solver tool lets you find a solution for every, even the most challenging
puzzle with ease.
In this essay we present a Sudoku Solver named as pencil-and-paper algorithm using simple
rules to solve the puzzles. The pencil-and-paper algorithm is formulated based on human
techniques. This means that the algorithm is implemented based on human perceptions.
Therefore the name of the solver is pencil-and-paper algorithm. The Brute force algorithm is
then used to compare with this algorithm in order to evaluate the efficiency of the proposed
algorithm. The brute force is a general algorithm than can be applied to any possible problem.

This algorithm generates any possible solutions until the right answer is
found.
1.2. Problem Definition
Solving Sudoku has been a challenging problem in the last decade. The purpose has been to
develop more effective algorithm in order to reduce the computing time and utilize lower
memory space. This essay develops an algorithm for solving Sudoku puzzle by using a
method, called pencil-and-paper algorithm. This algorithm resembles human methods, i.e. it
describes how a person tries to solve the puzzle by using certain techniques. Our ambition is
to implement the pencil-and-paper algorithm by using these techniques. There are currently
different variants of Sudoku such as 4X4 grids, 9X9 grids and 16X16 grids. This work is
focused on classic and regular Sudoku of 9X9 board, and then a comparison is performed
between the paper-and-pencil method and Brute force algorithm.

4
CHAPTER 2 LITERATURE REVIEW
2.1. Data Structure Used
2D Arrays
A 2D array is a data structure with rows and columns, forming a grid. Elements are accessed
using row and column indices. Common in various applications, from representing images to
solving matrices. Accessing elements is O(1), and iteration takes O(m * n) time, where m and n
are row and column counts, respectively. Managing boundaries and memory allocation are
essential considerations.

2.2. Algorithm
Recursive backtracking is an algorithmic technique used to systematically explore and solve
problems by making choices, exploring them, and reverting when needed, often used for puzzles
and combinatorial problems.
Kovacs describe some of the brute force methods used for solving Sudoku puzzles [9]. The
simplest method randomly produces a solution to the puzzle called “unconstrained grid”, after
that the program checks whether it is a valid solution. If not, the process is repeated until a
solution is found. This algorithm can be applied simply and will find a valid solution for any
problems because it will go through all possibility solutions. However, this method can be time
consuming but according to Kovacs the algorithm can be optimized. Generally, the brute force
algorithm goes through the empty squares, filling in numbers from the existing choices, or
removing failed choices if a “dead-end” is reached. For example, Brute force solve a puzzle by
inserting the digit “1” in the first square. If the digit is allowed to be there by checking row,
column and box then the program go to the next square, and put the digit “1” in that square. The
program discovers that the “1” is not allowed, then the digit increments by one i.e. it has
become 2. When a square is noticed where none of the digits (1 to 9) is permitted, then the
program backtracks and comes back to the prior square. The value in that square increases by 1.
The process is repeated until the correct digits fill all 81 squares

5
CHAPTER 3 METHODOLIZATION
3.1. Hypothesis
3.1.1. Recursive Backtracking
Decide on the next option, often based on heuristics or predefined rules and constraints.
Progress by applying the chosen option, working toward a solution or goal.
When a dead-end or conflict is reached, reverse decisions and try different options until
success. The backtracking method, which is similar to the human strategy (guessing), is used
as a help method to the pencil-and-paper algorithm. In other words, if the puzzle cannot be
filled when using the unique missing method and the naked single method, the backtracking
method will take the puzzle and fill the rest of empty squares. Generally, the backtracking
method find empty square and assign the lowest valid number in the square once the content
of other squares in the same row, column and box are considered. However, if none of the
numbers from 1 to 9 are valid in a certain square, the algorithm backtracks to the previous
square, which was filled recently.

3.2. Methodology

3.3. Implementation

Recursive backtracking is an algorithmic technique used to systematically explore and solve


problems by making choices, exploring them, and reverting when needed, often used for
puzzles and combinatorial problems.

6
Code Block 1: Header and Constants

#include <iostream>

#include <vector>

const int N = 9; // Size of the Sudoku grid

Explanation: In this code block, we include necessary header files for input/output and data structures. The
constant N is set to 9 to represent the size of a standard 9x9 Sudoku grid.

Code Block 2: Printing the Sudoku Grid

// Function to print the Sudoku grid

void printGrid(std::vector<std::vector<int>>& grid) {

for (int i = 0; i < N; i++) {

for (int j = 0; j < N; j++) {

std::cout << grid[i][j] << " ";

std::cout << std::endl;

Explanation: This function, printGrid, takes a 2D vector grid as input and prints the Sudoku grid to the
console. It uses nested loops to iterate through the grid and std::cout to display each cell's value.

7
Code Block 3: Checking if a Number Can Be Placed

// Function to check if a number can be placed in a specific cell

bool isSafe(std::vector<std::vector<int>>& grid, int row, int col, int num) {

// Check if 'num' is not already present in the current row and column

for (int i = 0; i < N; i++) {

if (grid[row][i] == num || grid[i][col] == num) {

return false;

// Check if 'num' is not already present in the current 3x3 subgrid

int startRow = row - row % 3;

int startCol = col - col % 3;

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

if (grid[i + startRow][j + startCol] == num) {

return false;

8
return true;

Explanation: The isSafe function is responsible for checking if it's safe to place a number 'num' in a specific
cell defined by its row and column. It examines both the current row and column to ensure 'num' is not already
present and also checks the 3x3 subgrid the cell belongs to. If any of these checks fail, it returns false,
indicating that it's not safe to place 'num' in the cell.

9
CHAPTER 4 RESULT
4.1. Result Analysis
In this section we see the explanation of how recursive backtracking can be used to solve a
sudoku.

4.1.1. Pencil-And-Paper Solver

There are several methods that are used by human players when playing Sudoku. However, it
may be impossible to implement all these methods. It is found that the hidden single method
or pair method are difficult to be applied in computer programming, since a human player has
a better overview over the whole Sudoku board than the computer programming does. This is
due to the fact that a human player is able to scan two rows or two columns in order to check
whether a certain digit is allowed to be in an empty square in the box that is supposed to be
filled up. Implementing the above task in computer programming causes significant time
consumption. The methods that are used in this algorithm are the following:

 Unique missing candidate

 Naked single method

 Backtracking

4.1.2 Naked single method

The second method that is used in the pencil-and-paper algorithm is the Naked single method.
This method checks every empty square in the Sudoku board and finds the square that can
only take one single digits and the missing digit then is assigned to that square. Note that once
the squares are filled by naked single digits other naked singles will appear. This process is
repeated until the method has found all empty squares with the needed corresponding one
single value and complete the board [7]. This method is a useful method when a human player
solves the game. However if the corresponding method is combined with the unique missing
10
candidate

11
method then both the methods can solve the puzzles both in easy and medium levels quickly
and more efficiently.

4.2. Key Area Analysis

 Scanning in one direction.


 Scanning in two direction.
 Searching for single candidates.
 Eliminating numbers from rows, columns and boxes.
 Searching for missing numbers in rows and columns.
 Eliminating squares using Naked pairs in a box.

4.3. Complexity Analysis

In the worst-case scenario, it explores all possible combinations, resulting in an exponential


time complexity of O(9^(n^2)), where 'n' is the grid size
In practice, heuristics and early pruning reduce time, making backtracking efficient.Thus, the
average-case time complexity is often much lower than the worst-case scenario, making
backtracking a practical approach for solving Sudoku puzzles.

12
CHAPTER 5 REFERENCES

1) Wikipedia [cited 2013 February 21], Web site: http://en.wikipedia.org/wiki/Sudoku


2) Home Of Logic Puzzles [cited 2013 February 22], Web Page:
http://www.conceptispuzzles.com/index.aspx?uri=puzzle/sudoku/classic
3) J.F. Crook, A pencil and paper algorithm for solving Sudoku Puzzles, [Cited 2013
February 24], Winthrop University, Webpage:
http://www.ams.org/notices/200904/tx090400460p.pdf
4) A.S. Showdhury, S. Skher Solving Sudoku with Boolean Algebra [Cited 2013 February
24], International Journal of Computer Applications, Peer-reviewed Research, Webpage:
http://research.ijcaonline.org/volume52/number21/pxc3879024.pdf
5) N. Pillay, Finding Solutions to Sudoku Puzzles Using Human Intuitive Heuristics, South
African Research Articles, Webpage:
http://sacj.cs.uct.ac.za/index.php/sacj/article/viewArticle/111
6) Ch. Xu, W. Xu, The model and Algorithm to Estimate the Difficulty Levels of Sudoku
Puzzles, Journal of Mathematics Research, 2009 Webpage:
http://journal.ccsenet.org/index.php/jmr/article/viewFile/3732/3336
7) T. Davis, The Mathematics of Sudoku (http://www.geometer.org/index.html), Research
Article, Webpage: http://share.dschola.it/castigliano/ips/Documentazione%20Progetto/Materiale
%20Didattico/M atematica/1E/sudoku.pdf
8) The figures are taken from webpage:
http://www.conceptispuzzles.com/index.aspx?uri=puzzle/sudoku/techniques
9) T. Kovacs, Artificial Intelligence through Search: Solving Sudoku Puzzles, Journal
Papers, Webpage: http://www.cs.bris.ac.uk/Publications/Papers/2000948.pdf
10) Sudoku solver using brute force, visited in Mars
2013, https://github.com/olav/JavaSudokuSolver
11) The puzzle generator, visited in Mars 2013, websudoku.com/

13
THANK YOU

10

You might also like