UNIT-6
UNIT-6
• Introduction to Backtracking
• Introduction to Brach & Bound
• 0/1 Knapsack Problem
• N-Queens Problem
• Travelling Salesman Problem
Introduction to Backtracking
Backtracking Overview:
• Backtracking is a general algorithmic technique that searches through
all possible combinations to solve a problem.
• It uses recursion and is faster than brute force by eliminating invalid
candidates with a single test.
• Brute force tries all possible solutions,
• backtracking discards invalid partial solutions early, improving performance.
Backtracking Problem Types:
• Else
• 2) take the item but subtract the item from the capacity
Backtracking Example
• We take item3 vs we do not take
• X3-1 and x3=0
• And so on
• Given:
Weighted Graph
Cost Adjacency matrix
Goal:
Strat from any point and reach back
there in in minimum cost
State Space tree
• Backtracking and Branch and bound both use state space tree
• But there approach is different
• In backtracking every leaf node is a solution
• But We want minimum cost tour
• So find out cost of each branch, the moment we find a more
minimum option discard the previous one.
• But backtracking is more time consuming for solving optimization i.e
minimization or maximization problem, we avoid backtracking.
• Backtracking is best used for permutation problems
Reduce the matrix (Row reduction, subtract
Column reduction and add total
Cost of reduction is 25
The min cost of tour may be 25
Draw a state space
tree
• From 1, I can visit 2,3,4,5
• Cost of 1st node =25
• Cost from 1 to 2=
• C(1,2) 1st row 2nd col make
all infinity
• (2,1) also as infinity
• Then find min in each row
and column if min is already
0 then new reduction=0
• C(1,2)=C(1,2)+R=NewR=25
The least cost node is 4
Therefore it is further
explored
Now 4 is connected to 2,3,
5
(6,7,8 are node no.)
Find the cost from (4,2)
• Find the cost from (4,2)
• (4,2)=(1,4) & (4,2)
fill 1st row 4th col & 4th
row 2nd Col with ∞ and
(2,1) also ∞
• Since we cannot go back
from
• 2 to 4 make that also ∞
• Check for reduction
(4,3)=(1,4) & (4,3)
fill 1st row 4th col & 4th row 3rd Col with ∞ and (3,1) also ∞