Backtracking
Backtracking
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)
2 3 4
1 2
3 2 4 3
Dead end
4 3 4 2
1 1
Solution Solution
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
2 3 4
3 2 4 3
Dead end
4 2
1 1
Solution Solution
1
Xi of the solution vector is either 1 or 0 depending on weather the
weight Wi is included or not.
∑𝑘 𝑊𝑖 + 𝑖=𝑘+ 𝑊 ≥ 𝑑
𝑛
The bounding function X[X1, X2, X3, -----Xn] = true iff
𝑖=
1 𝑋𝑖 ∑ 1 𝑖
∑ 𝑊𝑖 + 𝑊 𝑘 + 1 𝑑
𝑋𝑖
≤
𝑖=1
1 𝑋𝑖 ∑ 1 𝑖
𝑎𝑛𝑑 ∑ +𝑊 𝑘 + 1 𝑑
𝑊𝑖 𝑋𝑖
≤
𝑖=1
X1=1 X1=0
3, 2, 18 0, 2, 18
X2=1 X2=0
8, 3, 13 3, 3, 13
14, 4, 7 8, 4, 7 9, 4, 7 3, 4, 7
X4=1
15, 5, 0
Solution
++) { if (soln[j] == 1)
] + " ");
J1 J2 j3 j4
A 9 2 7 8
B 6 4 3 7
C 5 8 1 8
D 7 6 9 4
c –> J3 c –> J4
Lb=13 Lb=25
d –> J4
Lb=13
J1 J2 j3 j4
A 10 3 8 9
B 7 5 4 8
C 6 9 2 9
D 8 7 10 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
Start
Lb=7
Start
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