Fundamentals of Algorithm
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
Tedious process
Backtracking
Assign numbers one by one to empty cells
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 no conflict for digit at row, col assign digit to row, col and recursively try fill
in rest of grid
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