0% found this document useful (0 votes)
14 views61 pages

Backtracking

The document discusses backtracking techniques, including their historical development and algorithmic descriptions, focusing on applications like the N-Queen Problem and Hamiltonian Cycle. It explains the state space tree concept, constraints for problem-solving, and outlines algorithms for finding solutions. Additionally, it touches on branch and bound methods for optimization problems such as the Traveling Salesman Problem and the Assignment Problem.

Uploaded by

iamavp1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views61 pages

Backtracking

The document discusses backtracking techniques, including their historical development and algorithmic descriptions, focusing on applications like the N-Queen Problem and Hamiltonian Cycle. It explains the state space tree concept, constraints for problem-solving, and outlines algorithms for finding solutions. Additionally, it touches on branch and bound methods for optimization problems such as the Traveling Salesman Problem and the Assignment Problem.

Uploaded by

iamavp1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 61

Backtracking

Backtrack’ the Word was first introduced by Dr. D.H. Lehmer in


1950s.
• R.J Walker Was the First man who gave algorithmic
description in 1960.
• Later developed by S. Golamb and L. Baumert.

Backtracking technique resembles a depth-first – search


in a directed graph. The graph concerned here is usually
a tree, the aim of backtracking is to search the state
space tree systematically. The aim of the search is to
find solutions to some problems.
What is Backtracking?

When the search begins, solution to the problem is unknown.


Each move along an edge of the tree corresponds to adding a
new element to a partial solution , that is to narrowing down the
remaining possibilities for a complex solution.

The search is successful if, a solution can be completely


defined. At this stage an algorithm may terminate or it may
continue for an alternative solution.
The search is unsuccessful if at some stage the partial solution
constructed so far cannot be completed. In this case the search
backtracks like a depth first search, removing elements that
were added at each stage.
State Space Tree

In state space tree, root represents an initial state before the


search for a solution begins. The nodes of the first level in the
tree represent the choice made for the first component of a
solution, the nodes of the second level represent the choices for
the second components, and so on. A node in a state space tree
is said to be promising if it corresponds to a partially
constructed solution that may lead to a complete solution;
otherwise a node is said to be non promising.
N-Queen Problem
N Queen Problem
Constraints
Explicit Constraints: All ‘n’ queens must be placed on the
chessboard in the columns 1,2,3, …. N. Xi belongs to S where S =
{1,2,3, …. N }

Implicit Constraints: In this all Xi Values must be distinct


No two queens can be on the same row
No two queens can be on the same column
No two queens can be on the same diagonal
Horizontal Attack:
Row wise attacking is avoided by placing 1st queen in
1st row, 2nd queen in 2nd row and so on.
By placing ith queen in ith row, horizontal attacking can
be avoided
Vertical Attack:
(i, x[i]) means the position of ith queen in row i and
column x[i]
(k, x[k]) means the position of kth queen in row k
and column x[k]
If ith & kth queen are in same column
then X[i] == x[k]---------(1)
Hence indicate that queens attack vertically
(1,1) &Q1(4,1) x[i] == x[k] to be avoided

Q4
Diagonal Attack:
Top left corner to bottom right corner: The difference
between row value and column value is same.
(1,3) &1,1(2,4)
1,2 1,3 1,4 |i - x[i]| = |k - x[k]|-------(2) to
be 2,1
avoided
2,2 2,3 2,4
3,1 3,2 3,3 3,4
4,1 4,2 4,3 4,4
Diagonal Attack:
Top right corner to bottom left corner: The difference
between row value and column value is same.
(1,3) &1,1(3,1)
1,2 1,3 1,4 i + x[i] = k + x[k] ------(3) to be
avoided2,1 2,2 2,3 2,4
3,1 3,2 3,3 3,4 Using eqn. (2) and (3)
4,1 4,2 4,3 4,4 i – k = x[i] - x[k]------------------(4)
i – k = - x[i] + x[k]-----------------(5)

|i – k| = |x[i] - x[k]| indicates queens attack diagonally.

X[i] == x[k] || abs(i –k) = abs(x[i] – x[k])  two queens


attack each other and cannot be placed.
Algorithm
State Space Tree for 4
Queens
271
Two Solutions of 4 Queen Problem

BMS Institute of Technology and


Mgmt
Hamiltonian Cycle
Hamiltonian Path in an undirected graph is a path that visits each vertex exactly
once. A Hamiltonian cycle (or Hamiltonian circuit) is a Hamiltonian Path such that
there is an edge (in graph) from the last vertex to the first vertex of the
Hamiltonian Path. Determine whether a given graph contains Hamiltonian Cycle
or not. If it contains, then print the path. Following are the input and output of
the required function.
Input:
A 2D array graph[V][V] where V is the number of vertices in graph and graph[V]
[V] is adjacency matrix representation of the graph. A value graph[i][j] is 1 if
there is a direct edge from i to j, otherwise graph[i][j] is 0.
Output:
An array path[V] that should contain the Hamiltonian Path. path[i] should
represent the ith vertex in the Hamiltonian Path. The code should also return
false if there is no Hamiltonian Cycle in the graph.

BMS Institute of Technology and


Mgmt
Example 2

BMS Institute of Technology and


Mgmt
BMS Institute of Technology and
Mgmt
Example

BMS Institute of Technology and


Mgmt
Hamiltonian Cycle
X[1] =1 X[2] = 0 X[3]=0 X[4]=0 1

2 3 4

1 2
3 2 4 3

Dead end

4 3 4 2

1 1

Solution Solution

BMS Institute of Technology and


Mgmt
BMS Institute of Technology and
Mgmt
for (int i = 1; i <= n; i++)
x[i] = 0; 1
x[1] = 1;
void HamiltonianMethod(int k) { 2 3 4
while (true) {
NextValue(k, G, x, n);
if (x[k] == 0) 3 2 4 3
return;
if (k == n) { Dead end
for (int i = 1; i <= k; i++) 4 2
System.out.print(x[i] + " ");
System.out.println(x[1]);
System.out.println();
found = true;
return;
} else
HamiltonianMethod(k + 1);
}
} 1 1

Solution
void NextValue(int k, int G[][], int x[], int n) Hamiltonian Cycle
{ while (true) {
x[k] = (x[k] + 1) % (n + 1); Enter the number of the vertices: 4
if (x[k] == 0)
return; If edge between the following vertices
if (G[x[k - 1]][x[k]] != 0) { enter 1 else 0:
int j; 1 and 2: 1
for (j = 1; j < k; j++) 1 and 3: 1
if (x[k] == x[j]) 1 and 4: 1
break; 2 and 3:
if (j == k) 1
if ((k < n) || ((k == n) && G[x[n]][x[1]] != 0)) 2 and 4: 0
return; 3 and 4: 1
}
} Solution:
} 12341

14321

Department of BMS Institute of Technology and 280


ISE Mgmt
1

2 3 4

3 2 4 3

Dead end
4 2

1 1

Solution Solution

Department of BMS Institute of Technology and 281


ISE Mgmt
Department of BMS Institute of Technology and 282
ISE Mgmt
Sum of subsets

Department of BMS Institute of Technology and 283


ISE Mgmt
Sum of subsets

Department of BMS Institute of Technology and 284


ISE Mgmt
Sum of Subsets
Find a subset of a given set S= {S1, S2, S3, S4,-------Sn}
Of n +ve integers whose sum is equal to given +ve integer d subject
to the constrains

1. Implicit: All Xi values should be distinct and should belong

2. Explicit: optimal solution be ∑𝑘𝑖= 𝑆𝑖 = d


to the set S

1
Xi of the solution vector is either 1 or 0 depending on weather the
weight Wi is included or not.

Department of BMS Institute of Technology and 285


ISE Mgmt
For a node at level i, the left child corresponds to Xi = 1
and the right child to Xi = 0

∑𝑘 𝑊𝑖 + 𝑖=𝑘+ 𝑊 ≥ 𝑑
𝑛
The bounding function X[X1, X2, X3, -----Xn] = true iff
𝑖=
1 𝑋𝑖 ∑ 1 𝑖

X1, X2, -----Xk cannot lead to an promising node if this


condition is not satisfied.

Department of BMS Institute of Technology and 286


ISE Mgmt
The bounding function can be strengthened if we
assume that Wi’s are initially in increasing order.

In this case X1 – Xk can not lead to promising node if


X[X1, X2, X3, -----Xn] = true iff
𝑘

∑ 𝑊𝑖 + 𝑊 𝑘 + 1 𝑑
𝑋𝑖

𝑖=1

X1, X2, -----Xk cannot lead to an promising node if this


condition is not satisfied.
Department of BMS Institute of Technology and 287
ISE Mgmt
∑𝑖= 𝑊𝑖 + 𝑖=𝑘+ 𝑊 ≥ 𝑑
𝑘 𝑛
Therefore the bounding function will be

1 𝑋𝑖 ∑ 1 𝑖

𝑎𝑛𝑑 ∑ +𝑊 𝑘 + 1 𝑑
𝑊𝑖 𝑋𝑖

𝑖=1

Department of BMS Institute of Technology and 288


ISE Mgmt
State space tree
0, 1, 21

X1=1 X1=0

3, 2, 18 0, 2, 18

X2=1 X2=0

8, 3, 13 3, 3, 13

X3=1 X3=0 X3=1 X3=0

14, 4, 7 8, 4, 7 9, 4, 7 3, 4, 7

X4=1
15, 5, 0
Solution

Department of BMS Institute of Technology and 289


ISE Mgmt
Sum of subsets

Department of BMS Institute of Technology and 290


ISE Mgmt
Sum of subsets

Department of BMS Institute of Technology and 291


ISE Mgmt
Sum of subsets

Department of BMS Institute of Technology and 292


ISE Mgmt
Coding
for (int i = 1; i <= n; i++)
sum = sum + S[i];
if (sum < d || S[1] > d)
System.out.println("No Subset possible");
else
SumofSub(0, 0, sum);

Department of BMS Institute of Technology and 293


ISE Mgmt
b(int i, int weight, int total) { if (promising(i, weight,
total) == true)

++) { if (soln[j] == 1)
] + " ");

ght + S[i + 1], total - S[i + 1]); soln[i + 1] = 0;


ght, total - S[i + 1]);
static boolean promising(int i, int weight, int
total) {
return ((weight + total >= d) && (weight == d ||
weight + S[i + 1] <= d));

BMS Institute of Technology and 295


Mgmt
Graph Coloring

BMS Institute of Technology and 296


Mgmt
BMS Institute of Technology and 297
Mgmt
BMS Institute of Technology and 298
Mgmt
Graph Coloring

BMS Institute of Technology and 299


Mgmt
Graph Coloring

BMS Institute of Technology and 300


Mgmt
Branch and Bound
The term Branch means the way in which we search the
state space tree and Bound means assigning bounding
function at each node. This bounding function is used to
prevent the expansion of nodes that cannot possibly
lead to an answer node.
Basically there are two methods used in branch and
bound technique.

1. FIFO based Branch & Bound


2. In this method, the live node form a queue (FIFO
Structure) & each live node will be taken from the
queue and next live node is selected.
BMS Institute of Technology and 301
Mgmt
Branch and Bound

Least Cost Branch and Bound


At each node, an intelligent ranking function is used to
assign a value to that node. The next live node is
selected on the basis of the least cost.
Travelling sales man problem, a sales man must visit n
cities. The sales man visits each city exactly once and
comes back to the starting city.

The travelling sales man problem is minimization


problem and hence we require to find the lower bound.

BMS Institute of Technology and 302


Mgmt
Example 1
Assignment Problem : given n jobs <j1,j2,--- jn> and n persons
<p1,p2,p3 ---pn>, it is required to assign all n jobs to all n persons
with the constraint that one job has to be assigned to one person
and the cost involved in completing all the jobs should be
minimum.

J1 J2 j3 j4
A 9 2 7 8
B 6 4 3 7
C 5 8 1 8
D 7 6 9 4

BMS Institute of Technology and 303


Mgmt
Example 1
J1 J2 j3 j4 Take minimum in each row
A 9 2 7 8 2
B 6 4 3 7 3
C 5 8 1 8 1
D 7 6 9 4 4
10
a  J1 a  J2 a  J3 a  J4
a 9 2 7 8
b 3 3 4 3
c 8 5 5 5
d 4 4 4 6
24 14 20 22

BMS Institute of Technology and 304


Mgmt
Example 1
J1 J2 J3 J4 Take minimum in each row
A 9 2 7 8 2
B 6 4 3 7 3
C 5 8 1 8 1
D 7 6 9 4 4
10
b  J1 b  J3 b  J4
a 2 2 2
b 6 3 7
c 1 5 1
d 4 4 7
13 14 17

BMS Institute of Technology and 305


Mgmt
Example 1
J1 J2 J3 J4 Take minimum in each row
A 9 2 7 8 2
B 6 4 3 7 3
C 5 8 1 8 1
D 7 6 9 4 4
10
C  J3 C  J4 C  J4
a 2 2 a 2
b 6 6 b 6
c 1 8 c 1
d 4 9 d 4
13 25 13

BMS Institute of Technology and 306


Mgmt
Start
Lb=10

a –> J1 a – >J2 a –> J3 a –> J4


Lb=24 Lb=14 Lb=20 Lb=22

b –> J1 b –> J3 c –> J4


Lb=13 Lb=14 Lb=17

c –> J3 c –> J4
Lb=13 Lb=25

d –> J4
Lb=13

BMS Institute of Technology and 307


Mgmt
Example 2

J1 J2 j3 j4
A 10 3 8 9
B 7 5 4 8
C 6 9 2 9
D 8 7 10 5

BMS Institute of Technology and 308


Mgmt
Knapsack Problem
Knapsack Problem: Given n items of known weights wi and values vi, i=1, 2, . . . ,
n,and a knapsack of capacity W, find the most valuable subset of the items that
fit in the knapsack. It is convenient to order the items of a given instance in
descending order by their value-to-weight ratios. Then the first item gives the
best payoff per weight unit and the last one gives the worst payoff per weight
unit

BMS Institute of Technology and 309


Mgmt
First arrange in V/W in decreasing order
Since it is a maximization problem. The upper bound is calculated
using the function

i =0 , v=0, w=0 v i+1/wi+1 = 10


Ub = 0 + (10) 10 = 100

BMS Institute of Technology and 310


Mgmt
w=0. v=0
Ub = 100

With item 1 Without item 1


i =1, w=4. v=40, v i+1/wi+1 = 6 i =1, w=0. v=0, v i+1/wi+1 = 6
Ub = v + (W-w)(v i+1/wi+1 ) Ub = v + (W-w)(v i+1/wi+1 )
= 40 + 6. 6 = 0 + 10 . 6
= 76 = 60

With item 2 With out item 2


i =2, w=7. v=42, v i+1/wi+1 = 5 i =2, w=0+4. v=40+0, v i+1/wi+1 =
5
Ub = v + (W-w)(v i+1/wi+1 )
= 42 + (10 – 11) . 5 Ub = v + (W-w)(v i+1/wi+1 )
= Not Feasible = 40 + 6. 5
= 70

BMS Institute of Technology and 311


Mgmt
w=0. v=0
Ub = 100

With item 3 Without item 3


i =3, w=5+4. v=40+25, v i+1/wi+1 = 4 i =3, w=4+0. v=40+0, v i+1/wi+1 = 4
Ub = v + (W-w)(v i+1/wi+1 ) Ub = v + (W-w)(v i+1/wi+1 )
= 65 + 1. 4 = 40 + 6 . 4
= 69 = 64

With item 4 With out item 4


i =4, w=9+3 =12. i =4, w=0+9. v=65+0, v i+1/wi+1 = 1
Ub = v + (W-w)(v i+1/wi+1 ) Ub = v + (W-w)(v i+1/wi+1 )
= Not Feasible = 65 + 1. 1
= 66

BMS Institute of Technology and 312


Mgmt
w=0. v=0
Ub = 100

With item 1 Without item 2


i =1, w=4. v=40, v i+1/wi+1 = 6 i =1, w=0. v=0, v i+1/wi+1 = 6
= 76 = 60

With item 2 With out item 2


i =2, w=7. v=42, v i+1/wi+1 = 5 i =2, w=0+4. v=40+0, v i+1/wi+1 = 5
= Not Feasible = 70

With item 3 Without item 3


i =3, w=5+4. v=40+25, v i+1/wi+1 = 4 i =3, w=4+0. v=40+0, v i+1/wi+1 = 4
= 69 = 64

With item 4 With out item 4


i =4, w=9+3 =12. i =4, w=0+9. v=65+0, v i+1/wi+1 = 0
Not Feasible = 65

BMS Institute of Technology and 313


Mgmt
Travelling Sales Person
Problem
4
a d In the travelling sales man problem, a sales man must visit n
1 cities. The sales man visits each city exactly once and comes
3 2 back to the starting city.
1 The travelling sales man problem is minimization problem
b c and hence we require to find the lower bound.
5

Lower bound = lb = S / 2;
Where S = [Va+ Vb+Vc+Vd]
Va = sum of distances from vertex a to the nearest
vertices 1 + 3 = 4
Vb = 1+3 = 4
Vc = 1+2= 3
Vd = 1+2 = 3
Lb = [4 +4 +3+3] / 2 = 14 / 2 = 7
BMS Institute of Technology and 314
Mgmt
Now find,
a  b = (3+1)+(3+1)+(1+2)+(1+2) = 14/ 2 = 7
a  c = (1+3)+(3+1)+(1+2)+(1+2) = 14/ 2 = 7 a
 d = (4+1)+(1+3)+(1+2)+(4+1) = 17/2 = 8

Start
Lb=7

a –> b a–>c a –> d

Lb=7 Lb=7 Lb=8

BMS Institute of Technology and 315


Mgmt
Now find,
b  c = (3+1)+(5+1)+(5+1)+(1+2) = 19/ 2 = 9
b  d = (1+3)+(1+3)+(1+2)+(1+2) = 14/ 2 = 7
c  b = (1+3)+(5+1)+(5+1)+(1+2) = 19/2 = 9
c  d = (1+3)+(3+1)+(2+1)+(2+1) = 14/ 2 = 7

Start
Lb=7

a –> b a–>c a –> d

Lb=7 Lb=7 Lb=8

b –> c c –> b c –> d


b–>d
Lb=9 Lb=9 Lb=7
Lb=7

BMS Institute of Technology and 316


Mgmt
Now find,
d  c = (1+3)+(1+3)+(2+1)+(1+2) = 14/2 = 7 d
 b = (1+3)+(1+3)+(1+2)+(1+2) = 14/2 = 7 c
 a = (1+3)+(1+3)+(1+2)+(1+2) = 14/2 = 7 b
 a = (3+1)+(3+1)+(1+2)+(1+2) = 14/2 = 7

Start
Lb=7

a –> b a–>c a –> d


Lb=7 Lb=7 Lb=8

a b –> c a b –> d a c –> b a –> c  d


Lb=9 Lb=7 Lb=9 Lb=7

a b – > d  c a c  d – > b
Lb=7 Lb=7
a b – > d  c a c  d – > b

Lb=7 Lb=7

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy