0% found this document useful (0 votes)
109 views

Fundamentals of Algorithm

Backtracking is an algorithmic technique for solving problems recursively by trying to build solutions incrementally and removing those that fail to satisfy constraints. It considers all possible combinations to solve computational problems. For Sudoku, it assigns numbers to empty cells, checking safety by verifying the number doesn't exist in the same row, column or subgrid. It recursively tries assignments and returns false if none lead to a solution, trying the next number until a solution is found or all numbers are exhausted. The algorithm finds unassigned cells, tries digits 1-9, recursively fills the grid if no conflicts exist, else tries another digit, returning false if all fail.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views

Fundamentals of Algorithm

Backtracking is an algorithmic technique for solving problems recursively by trying to build solutions incrementally and removing those that fail to satisfy constraints. It considers all possible combinations to solve computational problems. For Sudoku, it assigns numbers to empty cells, checking safety by verifying the number doesn't exist in the same row, column or subgrid. It recursively tries assignments and returns false if none lead to a solution, trying the next number until a solution is found or all numbers are exhausted. The algorithm finds unassigned cells, tries digits 1-9, recursively fills the grid if no conflicts exist, else tries another digit, returning false if all fail.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Fundamentals of Algorithm

Backtracking Algorithm
6830 Samruddhi Naik
Backtracking
Backtracking is an algorithmic-technique for solving
problems recursively by trying to build a solution
incrementally, one piece at a time, removing those
solutions that fail to satisfy the constraints of the problem
at any point of time

Backtracking can be defined as a general algorithmic


technique that considers searching every possible
combination in order to solve a computational problem
When to use BackTracking
Consider a situation that you have three boxes in front of you
and only one of them has a gold coin in it but you do not know
which one. So, in order to get the coin, you will have to open
all of the boxes one by one. You will first check the first box, if
it does not contain the coin, you will have to close it and check
the second box and so on until you find the coin. This is what
backtracking is, that is solving all sub-problems one by one in
order to reach the best possible solution
Sudoku
 Given a partially filled 9×9 2D array grid[9][9], the goal is to assign digits
from 1 to 9 to the empty cells so that every row, column and sub grid of
size 3×3 contains exactly one instance of the digits from 1 to 9
Naive Algorithm
The Naive Algorithm is to generate all possible
configurations of numbers from 1 to 9 to fill the empty
cells

Try every configuration one by one until the correct


configuration is found

Tedious process
Backtracking
Assign numbers one by one to empty cells

Before assigning a number, we check whether it is safe to assign. We


basically check that the same number is not present in the current
row, current column and current 3X3 sub grid

After checking for safety, we assign the number and recursively check
whether this assignment leads to a solution or not. If the assignment
doesn’t lead to a solution, then we try next number for the current
empty cell. And if none of the number (1 to 9) leads to a solution, we
return false
Algorithm
Find row, col of an unassigned cell

If there is none,


return true

 For digits from 1 to 9

If there is no conflict for digit at row, col assign digit to row, col and recursively try fill
in rest of grid

If recursion successful, return true

Else, remove digit and try another If all digits have been tried and nothing worked,
return false
Python Implementation
Output
Analysis of Algorithm
Backtracking algorithms are generally exponential in both
time and space

However, a few problems still remain, that only have


backtracking algorithms to solve them until now
Thankyou !

You might also like