You are on page 1of 7

Mini Project Assignment

CSF102-Data Structures
Submitted By: -
Name: Aastha Shree, Mayank Singh
Roll No.: 2301021032, 2301021038
SAP ID:1000021358, 1000021367
Section: F/P2

Submitted To: -
Mrs. Preeti Gupta
Assistant Professor
School of Computing
Sudoku Puzzle
Description:
The Sudoku Solver project aims to create a program that solves Sudoku puzzles using the
backtracking algorithm. Sudoku is a popular number puzzle game consisting of a 9x9 grid
divided into nine 3x3 subgrids. The goal is to fill in the grid with digits from 1 to 9 in such a
way that each row, column, and subgrid contains all the digits from 1 to 9 without repetition.
The program takes a partially filled 9x9 Sudoku grid as input and recursively tries to fill in
the empty cells with digits from 1 to 9, ensuring that no digit is repeated in any row, column,
or subgrid. If a solution exists, the program outputs the completed Sudoku grid; otherwise, it
indicates that no solution exists.

Key Features:
 Input: Accepts a partially filled 9x9 Sudoku grid as input.
 Solving Algorithm: Utilizes the backtracking algorithm to systematically explore possible
solutions.
 Validation: Verifies each digit placement to ensure it does not violate Sudoku rules.
 Output: Displays the completed Sudoku grid if a solution is found; otherwise, indicates
that no solution exists.
 User Interface: Provides a simple text-based interface for inputting Sudoku puzzles and
viewing the solution.
Algorithm: -
Find an empty cell in the grid.
If there are no empty cells, return true (puzzle solved).
If there is an empty cell, try assigning a digit from 1 to 9.
For each digit, check if it's valid to place it in the current cell (not violating Sudoku
rules).
If valid, place the digit in the cell and recursively solve the rest of the puzzle.
If the recursive call returns true (puzzle solved), return true.
If the recursive call returns false, backtrack and try the next digit.
If no digit works, return false.
Code: -
#include <stdio.h>
#include <stdbool.h>
#define N 9
bool find_empty_cell(int grid[N][N], int *row, int *col) {
for (*row = 0; *row < N; (*row)++)
for (*col = 0; *col < N; (*col)++)
if (grid[*row][*col] == 0)
return true;
return false;
}
bool is_valid_move(int grid[N][N], int row, int col, int num)
{
for (int i = 0; i < N; i++)
if (grid[row][i] == num || grid[i][col] == num)
return false;
int startRow = row - row % 3, 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;
return true;
}
bool solve_sudoku(int grid[N][N]) {
int row, col;
if (!find_empty_cell(grid, &row, &col))
return true;
for (int num = 1; num <= 9; num++) {
if (is_valid_move(grid, row, col, num)) {
grid[row][col] = num;
if (solve_sudoku(grid))
return true;
grid[row][col] = 0;
}
}
return false;
}
void print_grid(int grid[N][N]) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
printf("%2d", grid[i][j]);
printf("\n");
}
}
int main() {
int grid[N][N] = {
{5, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9}
};
if (solve_sudoku(grid))
print_grid(grid);
else
printf("No solution exists\n");
return 0;
}
OUTPUT: -
This output represents the completed Sudoku grid after solving the provided puzzle. Each
row corresponds to a row in the Sudoku grid, with digits separated by spaces.
The time complexity of the provided Sudoku solving algorithm can
be analyzed as follows:
 Finding an Empty Cell: This operation involves iterating over the entire Sudoku grid,
which has a constant size of 9x9 cells. Therefore, the time complexity of finding an
empty cell is O(1).
 Validating a Move: Validating a move requires checking if the proposed digit
placement violates any Sudoku rules in the current row, column, and 3x3 subgrid. For
each digit placement, we need to check the entire row, column, and subgrid. Since
each row, column, and subgrid has a constant size of 9, the time complexity of this
validation operation is O(1).
 Solving the Sudoku Puzzle: The backtracking algorithm is used to recursively solve
the Sudoku puzzle. In the worst-case scenario, the algorithm will try all possible digit
placements for each empty cell until a solution is found. Since each empty cell can
have up to 9 possible digit placements, and there can be at most 81 empty cells in a
9x9 Sudoku grid, the worst-case time complexity of the backtracking algorithm can
be expressed as O(9^81), which is an astronomical number.

However, in practice, the backtracking algorithm tends to terminate much earlier due to the
constraints imposed by Sudoku rules. The number of recursive calls and the number of
branches explored in the search tree depend on the complexity of the Sudoku puzzle. Puzzles
with fewer empty cells or with more pre-filled cells will generally require fewer recursive
calls and less exploration of the search space.
Therefore, while the worst-case time complexity of the backtracking algorithm is
exponential, the actual time complexity for solving practical Sudoku puzzles is typically
much lower and depends on the specific puzzle instance. Additionally, optimizations such as
constraint propagation techniques or heuristic-based approaches can further reduce the time
complexity in practice.

Conclusion: -
Overall, the Sudoku Solver project provides a robust and functional solution for solving
Sudoku puzzles, catering to both enthusiasts and those seeking to improve their problem-
solving skills. With further development and enhancements, the project can offer additional
features and capabilities, enhancing its utility and appeal to a wider audience.

You might also like