Dsa Report
Dsa Report
A PROJECT REPORT
Submitted by
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
3.1 Hypothesis………………………………………...……………………………………………...
3.2 Methodology……………………………………………………………………………………...
3.3 Implementation……………………………………………...........................................................
REFERENCES................................................................................................................................ 8
3
CHAPTER 1 INTRODUCTION
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
6
Code Block 1: Header and Constants
#include <iostream>
#include <vector>
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.
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
// Check if 'num' is not already present in the current row and column
return false;
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.
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:
Backtracking
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.
12
CHAPTER 5 REFERENCES
13
THANK YOU
10