Git Product home page Git Product logo

sudoku-solver's Introduction

Sudoku Solver using Backtracking Algorithm

This Python script provides a CLI Sudoku puzzle generator and solver using backtracking algorithm. It is designed to offer a hands-on experience in implementing and understanding the intricacies of backtracking while solving Sudoku puzzles.

An updated Sudoku Solver with GUI can be accessed here.


Sudoku

Sudoku is a classic number-placement puzzle where the objective is to fill a square grid with numbers so that each column, each row, and each of the subgrids (also known as boxes) contain all of the numbers from 1 to 9 (or 1 to 4 for smaller puzzles) without repetition.


image

(classic sudoku image from adobe stock)


Usage

  1. Save the script to your local machine.
  2. Run the script.
  3. When prompted, enter the desired difficulty level (1-9) and Sudoku board size (4 for 4x4, 9 for 9x9).
  4. The script will generate a Sudoku puzzle for you to solve and display both the initial puzzle and the solution.

Function Descriptions

  • SudokuSolver: The main function that utilizes backtracking to recursively fill in empty cells on the Sudoku board.
  • ValidMoveCheck: Checks if a move is valid by examining the row, column, and box constraints.
  • DisplayBoard: Creates a visual representation of the Sudoku board.
  • FindEmptyCell: Finds the first empty cell on the Sudoku board.
  • GenerateBoard: Generates a Sudoku board with a solution and removes numbers based on difficulty.

Performance Measurement

The script includes a function, measure_solver_performance, which measures the time taken by the SudokuSolver function to solve a Sudoku puzzle.


Demo

demo.mp4

Backtracking Algorithm

The heart of this Sudoku solver lies in the backtracking algorithm, a powerful technique for solving problems that involve making a sequence of decisions. In the context of Sudoku, the backtracking algorithm explores possible number placements, backtracking when it encounters an invalid move, until a valid solution is found.

I developed this script as a way to solidify my understanding of Data Structures and Algorithms (DSA), leveraging various sources to grasp the intricacies of backtracking.


Approach

  • The SudokuSolver function utilizes backtracking to recursively fill in empty cells on the Sudoku board.
  • It starts by finding an empty cell and attempting to place a number (1 to 9) in that cell.
  • If the move is valid, the algorithm continues to the next empty cell, repeating the process.
  • If a dead-end is reached (no valid moves for a particular cell), the algorithm backtracks to the previous cell and explores alternative options.
  • This process continues until a solution is found or all possibilities are exhausted.

The backtracking algorithm ensures an efficient and systematic approach to solving Sudoku puzzles, making it an ideal choice for this project. The script also includes functions for checking the validity of a move (ValidMoveCheck), displaying the Sudoku board (DisplayBoard), and finding an empty cell (FindEmptyCell).


Author

Acknowledgments

  • Thanks to various resources and tutorials on Sudoku that guided me at different stages of learning
  • An updated version with GUI can be accessed here.

sudoku-solver's People

Contributors

aswinpkumar01 avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.