Backtracking
Terms related to backtracking— Nodes
Difference between backtracking and recursion
Applications of backtracking
sum of subsets problem
Recursive method of solving sum of subsets problem
We have two options while solving it at every step:
1. Include : here we include means we are selecting the element from our next line
2. Reject(exclude) : We’re rejecting the elements from the array
sum of subsets algo
def subset_sum(arr, res, sum)
if sum ==0
return true
if sum < 0
return false
if len(arr) == 0 and sum!= 0
return false
arr.pop(0);
if len(arr) > 0
res.append(arr[0])
select = subset_sum(arr, sum-arr[0], res)
reject = subset_sum(arr, res, sum)
return reject or sum
n-queens problem
The possible solution for 4-queens problem is:
(2, 4 1, 3) or
(3, 1, 4, 2)
The possible solution for 8- queens problem is:
(4,6,8,2,7,6,5)
Graph colour