ADA_Unit-7
ADA_Unit-7
Back Tracking
Backtracking
• Backtracking can be defined as a general algorithmic technique that
considers searching every possible combination in order to solve an
optimization problem.
• It is a recursive technique.
• It generates state space tree for all possible solutions.
• It traverse state space tree in the depth first order.
• So, in a backtracking we attempt solving a sub-problem, and if we don't
reach the desired solution, then undo whatever we did for solving that
sub-problem, and try solving another sub-problem.
• All the solutions require a set of constraints divided into two categories:
explicit and implicit constraints.
The N - Queens Problem
• The 𝑁 - queen is the problem of placing N chess queens on an 𝑁 × 𝑁
chessboard so that no two queens attack each other.
• Two queens of same row, same column or the same diagonal can
attack each other.
• K-Promising solution: A solution is called k-promising if it arranges
the k - queens such a way that, they can not threat each other.
Q Q Q
Q Q
1 - Promising 0 - Promising 0 - Promising 0 - Promising
Solution Solution Solution Solution
The 4 - queen Problem
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
Q1 Q1 Q1 Q1
Q2 Q3 can’t Q2 Q4 can’t Q2
place so place so
backtrack Q3 backtrack Q3
to Q2 to Q1
Q4
4 – Promising <2, 4, 1, 3>
Another possible solution is 1 2 3 4
<3, 1, 4, 2>
1 Q1
2 Q2
3 Q3
4 Q4
N - Queen Problem
Number of Possible
Queens Solutions
1 1
2 0
3 0
4 2
5 10
6 4
7 40
8 92
9 352
10 724
4 - Queens State Space Tree
Knapsack Problem
• We are given a certain number of objects and a knapsack.
• Instead of supposing that we have n objects available, we shall
suppose that we have n types of object, and that an adequate number
of objects of each type are available.
• Our aim is to fill the knapsack in a way that maximizes the value of the
included objects.
• We may take an object or leave behind,
but we may not take fraction of an object.
Knapsack Problem
Initially solution is empty. Weight of
Left of the semicolon are weights of selected objects. objects w = (2,3,4,5)
Right of the semicolon is the current total value of load. Corresponding v = (3,5,6,10)
value of each
object
W=8
apply depth first
;0 Capacity of
search & find
Knapsack
max profit.
2;3 3;5 4;6 5 ; 10
MAX (player) 3
MIN
(opponent) 3 2 2
3 12 8 2 4 6 14 5 2
Minimax Principle
• It is a decision rule that minimize the maximum loss and
maximize the minimum profit(gain).
Minimax Principle
• The key to the Minimax algorithm is a back and forth between the
two players, where the player whose "turn it is" desires to select the
move with the maximum score.
• In turn, the scores for each of the available moves are determined by
the opposing player deciding which of its available moves has the
minimum score.
• It uses a simple recursive computation of the minimax values of each
successor state.
• The recursion proceeds all the way down to the leaves of the tree,
and then the minimax values are backed up through the tree as the
recursion unwinds.
X X O
Human Max X O
O
WIN
X X O X X O X X O
Computer Min X O X X O X O
O X O O X
WIN WIN
X X O X X O X X O X X O
Human Max X O X X O X X O X O O
O O O O O O X O X
WIN WIN
X X O X X O
Computer Min X O X X O O
X O O X O X
Minimax Principle
23-11-2022
B C
1 5
7
23-11-2022
8
23-11-2022
4
5
6
2
9
23-11-2022
0 0 1 0
4
5
6
2
0 0 1 0
10
23-11-2022
-
13
0
0
5 - 0 0
Cost(2) = Cost(1) + Sum of reduction elements + M[A,B]
= 18 + (13 + 5) + 0
= 36
11
23-11-2022
-
0
0
0
0 0 - 0
Cost(3) = Cost(1) + Sum of reduction elements + M[A,C]
= 18 + 0 + 7
= 25
12
23-11-2022
-
0
5
0
0 0 0 -
Cost(4) = Cost(1) + Sum of reduction elements + M[A,D]
= 18 + 5 + 3
= 26
13
23-11-2022
A
Thus, we have-
Cost(2) = 36 (for Path A → B)
Cost(3) = 25 (for Path A → C)
Cost(4) = 26 (for Path A → D) B C D
36 25 26
We choose the node with the lowest cost.
Since cost for node-3 is lowest, so we prefer to visit node-3.
Thus, we choose node-3 i.e. path A → C.
Cost(3) = 25
14
23-11-2022
-
13
-
8
0 - - 0
Cost(5) = cost(3) + Sum of reduction elements + M[C,B]
= 25 + (13 + 8) + ∞
=∞
Cost(3) = 25
15
23-11-2022
-
0
-
0
0 0 - -
Cost(6) = cost(3) + Sum of reduction elements + M[C,D]
= 25 + 0 + 0
= 25
16
23-11-2022
Cost(6) = 25
-
-
-
-
- - - -
Cost(7) = cost(6) + Sum of reduction elements + M[D,B]
= 25 + 0 + 0
= 25
17