Coder Social home page Coder Social logo

sheikhumar93 / solving-sudoku-using-dfs Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 1.56 MB

Solving Sudoku (diagonally as well) using Depth First Search and Constraint Propagation

Python 100.00%
depth-first-search constraint-propagation sudoku-solver

solving-sudoku-using-dfs's Introduction

Artificial Intelligence Nanodegree

Introductory Project: Diagonal Sudoku Solver

Question 1 (Naked Twins)

Q: How do we use constraint propagation to solve the naked twins problem?
A: As Constraint Propagation is all about introducing local constraints in a space, in the case of Sudoku, thereby reducing the search space for a given problem. Naked Twins further helped with introducing these constraints along with Elimination and Only Choice. If in a unit there are only 2 boxes available that have the same values, we can safely say that only these two boxes can have these values (a constraint) and we can remove these digits from all the other boxes inside this unit reducing the search space and helping the agorithm solve the problem faster e.g. if in row 1 from A1-A9 if we have two boxes which can only take 2 values e.g. 2,3 we can say that only these boxes can have either 2 or 3 in which case we remove any instances of 2 or 3 from any other boxes in the same unit. In this way we reduce our search space in the next iteration of Depth First Search, and we can converge towards the solution one bit faster reducing the overall number of possibilities in our unsolved grid which is the main purpose of using DFS so that we do not have to brute force our way to finding a solution by using lots of computational power which is very expensive. Techniques like naked twins make our model more efficient.

Question 2 (Diagonal Sudoku)

Q: How do we use constraint propagation to solve the diagonal sudoku problem?
A: Likewise, for solving the diagonal sudokus we have a constraint that no two boxes in the main forward and backward diagonals across the board can have the same values. We can then use elimination, only choice, naked twins and only square technique again to quickly solve this problem as well giving us numbers in each box in the diagonal that are unique in their respective units. We use constraints to say that we can only have one digit in each box along each diagonal. We narrow down the search space by using elimination, only choice, naked twins and only square one at a time again saving us on computing power which is very expensive to make our model more efficient and step by step eliminating obvious digits that cannot be part of some box by the end of which we have a solved sudoku that only has one digit in each box and does not overlap with any other peer in any other unit.

Install

This project requires Python 3.

We recommend students install Anaconda, a pre-packaged Python distribution that contains all of the necessary libraries and software for this project. Please try using the environment we provided in the Anaconda lesson of the Nanodegree.

Optional: Pygame

Optionally, you can also install pygame if you want to see your visualization. If you've followed our instructions for setting up our conda environment, you should be all set.

If not, please see how to download pygame here.

Code

  • solution.py - You'll fill this in as part of your solution.
  • solution_test.py - Do not modify this. You can test your solution by running python solution_test.py.
  • PySudoku.py - Do not modify this. This is code for visualizing your solution.
  • visualize.py - Do not modify this. This is code for visualizing your solution.

Visualizing

To visualize your solution, please only assign values to the values_dict using the assign_values function provided in solution.py

solving-sudoku-using-dfs's People

Contributors

sheikhumar93 avatar

Watchers

 avatar  avatar  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.