0% found this document useful (0 votes)
25 views11 pages

Sodu Ku

Sukudu project

Uploaded by

reemadollvlog
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views11 pages

Sodu Ku

Sukudu project

Uploaded by

reemadollvlog
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Algorithm Design Report

By

Muhammad Hamza Sadiq (Sp-2023-BSCS-141)


Haseeba Rani (Sp-2023-BSCS-142)
Hafiz Muneeb Ul Hassan (Sp-2023-BSCS-143)

Submitted To: Ma`am Zainab Zafar


Subject: Data Structure & Algorithm
Date: June 5, 2023

DEPARTMENT OF COMPUTER SCIENCE


LAHORE GARRISON UNIVERSITY
“Algorithm Design For Sudoku”

Abstract

This paper presents the design and implementation of a Sudoku game algorithm. The objective is to
create a playable and solvable Sudoku puzzle using algorithmic techniques. The foundation lies in
backtracking and constraint satisfaction principles, providing an effective method to generate and solve
Sudoku puzzles. This document outlines the game's rules, objectives, elements, and the proposed
algorithm for generating and solving the puzzles.

Keywords: Sudoku, Numbers, Grid, Logic, Backtracking, Puzzles.

Introduction

Sudoku is a popular logic-based number placement puzzle that originated in Japan. The game involves a
9x9 grid divided into nine 3x3 sub-grids, where the objective is to fill the grid with digits from 1 to 9. Each
digit must appear exactly once in each row, column, and sub-grids. The puzzle starts with some cells pre-
filled, and the player must complete the grid following the rules.

The goal of the algorithm is to generate a valid Sudoku puzzle and provide a method to solve it. The
inputs are the initial state of the Sudoku grid, and the outputs are the completed grid or a confirmation
of its solvability. The constraints include maintaining the unique placement of digits within rows,
columns, and sub-grids.

The desired performance is an efficient generation and solving mechanism that can handle different
difficulty levels. Test cases will include various initial grid configurations to ensure the algorithm's
robustness.
About the Game

1. Objective: Players need to ensure that each row, each column, and each of the nine 3x3 sub-
grids, known as regions, contains every digit exactly once. Successful completion requires
strategically placing numbers based on deductive reasoning, without violating the rules,
resulting in a fully filled grid.

2. Rules:
 Numbers 1 to 9: Fill a 9x9 grid with numbers 1 through 9.
 No Repeats: Each row, column, and 3x3 box must have every number 1-9, with no repeats.
 Start with Clues: Some cells come filled; these are hints to help you.

3. Game Elements: The Key Elements are:


 Sudoku board: A 9x9 grid.
 Digits: Numbers from 1 to 9.

4. Initial State: In the starting configuration of a Sudoku puzzle:


 Grid: You have a 9x9 grid, like a big square split into 81 smaller squares.
 Numbers: Some of the smaller squares (tiles) already have numbers in them. These
are the initial clues given to you.
 Empty Spaces: Most of the smaller squares are empty at the beginning. This is
where you’ll place numbers to solve the puzzle.
5. Objective State: Successfully completing a Sudoku puzzle means:
 Every Row: Each horizontal row (left to right) has numbers 1 through 9, with no
repeats.
 Every Column: Each vertical column (top to bottom) also has numbers 1 through 9,
without repetition.
 Every 3x3 Box: Each of the 9 smaller squares (3x3 boxes) contains every number 1-
9, and no number is repeated.
6. Actions:
Baby steps

When eight digits in some row or column or box are known, one can find the Last missing digit.

Singles
When there is only one place for a given digit in a given row or column or box, write it there. If there is
only one digit that can go in a given square, write it there.
Hinge

The previous subsection used that each cell contains at least one digit. Conversely, each digit is in at least
one cell in any given row, column or box. For example,

7. Move Validation:
 Number Placement: Put numbers 1 through 9 in empty spots, following the no-
repeat rule.
 Logical Deduction: Use logic to figure out where a number goes based on existing
clues and the rules.
 Elimination: Cross out options for empty spots by considering the numbers already
in the row, column, and 3x3 box.
8. Transition Function: When you put a number in an empty spot, the game changes. This
shift happens as you use logic to figure out where numbers go. With each move, the grid
reflects your choices, and as you eliminate possibilities, the puzzle evolves. The game
transitions from one state to another based on your strategic placements, always making
sure numbers follow the rules. Each move takes you closer to completing the puzzle!
9. Game Loop: This iterative process continues as players strategically fill in more numbers,
eliminating possibilities and adjusting the state. The game progresses step by step until all
empty spots are filled, and every row, column, and 3x3 box adheres to the rules.
10. Scoring (Optional): Scoring can be based on the number of moves or time taken to
complete the puzzle.
11. End Conditions: Game of Sudoku ends when the player successfully fills every empty spot
in the grid with numbers 1 through 9, adhering to the rules of no repetition in rows,
columns, or 3x3 boxes. There is no predefined limit on moves or time; completion of the
puzzle by achieving the correct configuration is the clear signal that the game has concluded
successfully.
12. User Interface (Optional):

a) Grid Display

b) Input Methods

c) Feedback

d) Navigation

e) Timer (Optional)
Literature Review

Origins and Early Development:


 Leonhard Euler's Latin Squares (18th Century): The concept of Sudoku can be traced back
to the Swiss mathematician Leonhard Euler, who developed "Latin Squares" a grid-based puzzle
where numbers appear only once in each row and column.
 Number Place (1979): Modern Sudoku was first published as "Number Place" in Dell Pencil
Puzzles and Word Games magazine in the U.S. by Howard Garns, a retired architect. The puzzle
involved filling a 9x9 grid so that each column, each row, and each of the nine 3x3 subgrids
contain all of the digits from 1 to 9.

Popularization in Japan:
 Introduction to Japan (1984): The puzzle was introduced to Japan by Nikoli a puzzle publishing
company. It was renamed "Sudoku" a short form of the Japanese phrase "Sūji wa dokushin ni
kagiru" which means "the digits are limited to one occurrence."
 Sudoku's Popularity (1980s-1990s): In Japan, Sudoku gained significant popularity in the
1980s and 1990s. Nikoli made slight modifications to the puzzle, such as limiting the number of
givens to no more than 30, to increase difficulty and appeal.

Legacy and Impact:


 Educational Value: Sudoku is praised for its educational benefits, such as improving logical
thinking, problem-solving skills, and memory. It is used in classrooms and brain training
programs.
 Cultural Impact: Sudoku has become a cultural icon, appearing in various forms of media,
including TV shows, films, and merchandise. Its simplicity and universal appeal have made it a
beloved pastime for millions worldwide.
Problem Statement

Sudoku is a logic-based, combinatorial number-placement puzzle. The objective is to fill a 9x9 grid
with digits from 1 to 9, ensuring that each column, each row, and each of the nine 3x3 sub-grids
(also known as "regions" or "boxes") contain all of the digits from 1 to 9 without any repetition. The
puzzle begins with some of the cells already filled with numbers (called "givens" or "clues") and
The challenge is to design an efficient algorithm for generating and solving Sudoku puzzles. The
algorithm must ensure the puzzles are unique and solvable while allowing for different difficulty
levels.

Algorithm Design Objectives

The objectives of this algorithm design are:


 To develop a method for generating valid Sudoku puzzles.
 To implement a solver using backtracking techniques.
 To ensure the generated puzzles have a unique solution.
 To provide different levels of puzzle difficulty.
 To optimize the algorithm for efficient performance.
The algorithm aims to guide users with simple steps to place numbers correctly in the grid,
following the game rules. It prioritizes user-friendliness, ensuring that players can solve puzzles
without confusion. The goal is to create an algorithm that is adaptable to different puzzle
complexities, allowing users to enjoy a straightforward and satisfying experience. Ultimately, the
objectives are to simplify Sudoku solving, making it accessible and enjoyable for players of various
skill levels.

Proposed Solution

The proposed solution involves a backtracking algorithm to generate and solve Sudoku puzzles. The
generation algorithm ensures a unique solution by strategically placing initial digits and validating
the grid. The solving algorithm uses backtracking to fill the grid while adhering to the game's rules.
Proposed System Algorithm

Algorithm for Generating Sudoku Puzzle:

1. Start.
2. Initialize an empty 9x9 grid.
3. Randomly fill the diagonal 3x3 sub-grids to ensure a baseline of filled cells.
4. Use backtracking to fill the remaining cells while ensuring the uniqueness of digits in rows,
columns, and sub-grids.
5. Remove a set number of digits to create the puzzle, ensuring the solution remains unique.

Algorithm for Solving Sudoku Puzzle:


a. Find the next empty cell in the grid.
b. Try placing digits from 1 to 9 in the cell.
c. For each digit, check if it follows the rules.
d. If a digit is valid, recursively attempt to fill the rest of the grid.
e. If no digits lead to a solution, backtrack and try the next digit.
f. Repeat until the grid is completely and correctly filled.
Testing and Iteration

To ensure the Sudoku generation and solving algorithms are robust and efficient, comprehensive
testing and iteration are essential.

1. Unit Testing
Validity Check: Test the function that verifies if a digit can be placed in a cell without violating Sudoku
rules.
 Input: Partially filled grid and candidate number.
 Expected Output: True if valid, otherwise false.

Next Empty Cell: Test the function that finds the next empty cell.

 Input: Partially filled grid.


 Expected Output: Coordinates of the next empty cell or indication that the grid is
complete.

Backtracking Function: Test the backtracking function that fills the grid.

 Input: Partially filled grid.


 Expected Output: Fully solved grid or indication of no solution.
2. Iteration
 Identify Issues: Use test results and user feedback to identify issues.
 Implement Improvements: Make necessary changes and retest.

Optimization

Potential optimizations include using advanced data structures to keep track of filled cells and
constraints more efficiently. Parallel processing can be explored for generating puzzles more
quickly.

Conclusion

In conclusion, the algorithm presented not only meets the technical challenges of Sudoku puzzle
generation and solving but also aims to provide an engaging and satisfying experience for
enthusiasts of all skill levels. Future iterations may focus on adaptive difficulty settings and real-
time feedback mechanisms to further enhance usability and enjoyment.

References

i. This website likely provided information on the historical origins of Sudoku, tracing its roots
back to Leonhard Euler's Latin Squares and its evolution into the modern puzzle known as
Sudoku. [ https://sudoku.com/how-to-play/the-history-of-sudoku/ ]
ii. This source might have been used to explore how Sudoku gained popularity in Japan
through Nikoli and its subsequent modifications to increase the puzzle's appeal and
difficulty. [ https://www.sudokudragon.com/index.html/ ]
iii. This website could have provided insights into the educational benefits of Sudoku, including
its impact on logical thinking, problem-solving skills, and memory enhancement.[
https://www.bkkkids.com/blog/the-benefits-of-sudoku-for-kids/ ]

You might also like