Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views22 pages

Daa Unit 4

DAA UNIT 4

Uploaded by

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

Daa Unit 4

DAA UNIT 4

Uploaded by

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

UNIT – 4: DYNAMIC PROGRAMMING

PRINCIPALS OF DYNAMIC PROGRAMMING


Dynamic programming: Dynamic programming is a technique that breaks the problems into sub-
problems, and saves the result for future purposes so that we do not need to compute the result again.
The subproblems are optimized to optimize the overall solution is known as optimal substructure
property.

The main use of dynamic programming is to solve optimization problems. Here, optimization
problems mean that when we are trying to find out the minimum or the maximum solution of a
problem. The dynamic programming guarantees to find the optimal solution of a problem if the
solution exists.

The definition of dynamic programming says that it is a technique for solving a complex problem by
first breaking into a collection of simpler subproblems, solving each subproblem just once, and then
storing their solutions to avoid repetitive computations.

Example: Consider an example of the Fibonacci series. The following series is the Fibonacci series:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …
The numbers in the above series are not randomly calculated. Mathematically, we could write each of
the terms using the below formula:
F(n) = F(n-1) + F(n-2),

With the base values F(0) = 0, and F(1) = 1. To calculate the other numbers, we follow the above
relationship. For example, F(2) is the sum f(0) and f(1), which is equal to 1.

How can we calculate F(20)? The F(20) term will be calculated using the nth formula of the
Fibonacci series. The below figure shows that how F(20) is calculated.

Working of dynamic programming approach: The following are the steps that the dynamic
programming follows
➢ It breaks down the complex problem into simpler subproblems.
➢ It finds the optimal solution to these sub-problems.
➢ It stores the results of subproblems (memoization). The process of storing the results of
subproblems is known as memorization.
➢ It reuses them so that same sub-problem is calculated more than once.
➢ Finally, calculate the result of the complex problem.
Approaches of dynamic programming: There are two approaches to dynamic programming:
1. Top-down approach
2. Bottom-up approach
1. Top-down approach: The top-down approach follows the memorization technique, while bottom-
up approach follows the tabulation method. Here memorization is equal to the sum of recursion and
caching. Recursion means calling the function itself, while caching means storing the intermediate
results.

Advantages
➢ It is very easy to understand and implement.
➢ It solves the subproblems only when it is required.
➢ It is easy to debug.

Disadvantages
➢ It uses the recursion technique that occupies more memory in the call stack. Sometimes when
the recursion is too deep, the stack overflow condition will occur.
➢ It occupies more memory that degrades the overall performance.

2. Bottom-Up approach: The bottom-up approach is also one of the techniques which can be used to
implement the dynamic programming. It uses the tabulation technique to implement the dynamic
programming approach. It solves the same kind of problems but it removes the recursion. If we
remove the recursion, there is no stack overflow issue and no overhead of the recursive functions. In
this tabulation technique, we solve the problems and store the results in a matrix.

ROD CUTTING PROBLEM


Problem statement: Given a rod of length n, and an array that contains the prices of all the pieces
smaller than n, determine the maximum profit you could obtain from cutting up the rod and selling its
pieces. Suppose that we have a rod of length 5, and an array containing the length (1,2,3 and 4) and
price (2,5,7 and 8) of the pieces.

There are various ways to cut the rod into sub-rods, each way results in a certain profit.

The answer should be 12 (selling the sub-rods of length 1+2+2 gives a 2+5+5 profit).
Using dynamic programming: Since dynamic programming is mainly an optimization-over-plain
recursion, we can use it to optimize the above exponential time algorithm. The idea is to store the
results of subproblems so that we do not have to re-compute them when they are needed.
➢ The possible range of length, of any sub-rod, is from 0 to 5. A sub-rod of a length within this
range can be made.
➢ We have 4 different prices of different-length sub-rods (i.e., cut marks, the place where cuts
can be made on a sub-rod).

The grid above represents our sub-problems. Let:


➢ i be the number of rows
➢ j be the number of columns
The cell at [i,j] represents the maximum amount of profit that can be earned from selling a rod of
initial length j. One is able to choose the cuts from an array of 0 to i.

For example, the cell at [2,3] represents the maximum profit that can be earned from a rod with length
3, and an array of different prices of different-length sub-rods (0 to 2).

Now, let’s start filling in the table/array. Start with the simple calculations first. If a rod has a length
of 0, what would the maximum profit be? It would be 0.

Let’s start to fill in the rows


➢ In the first row, we only have the price 2 at a length of 1 in our array.
➢ At index [0,1], putting a cut at 1 would give a profit of 2. No cut would give a profit of 2.
➢ At index of [0,2], putting a cut at 1 would give a profit of 4 (2 rods with a length of 1, each
with a price of 2). No cut would give a profit of 4 (not 5 because the price of 2 does not exist
in the array yet!). Hence, 4 is the better option.
➢ Do this for the whole first row.
Your grid should look like this now:

You may have observed that we’re deciding, at every step, whether the newly added cut mark ( i+1)
increases the profit of the given rod j, or not. We decide this by taking the maximum of 2 things:
➢ The maximum profit of the length before the cut mark was made (i.e., the value at [ i-1,j] ).
This is a case where we don’t include the newly added cut mark.
➢ price [i] + the maximum profit at length j-i-1 for the current array of prices (i.e., the value at
[i, j-i-1]) would have already been computed before this computation. This is a case where we
use the new cut mark for its effect on the profit.

So, the general expression used for filling the grid is:

T is the grid, and price is the array of prices. In the cases where j <= i, j-i-1 would be negative, simply
use the value at [i-1,j]

Once the grid is filled, which cell contains our answer? Let’s recall that the problem asked us to
maximize the profit of a rod, of length n, with an array of prices from 0 to n-1. This is the cell in the
extreme bottom-right:
FLOYD-WARSHALL ALGORITHM
The Floyd-Warshall algorithm is a dynamic programming algorithm used to discover the shortest
paths in a weighted graph, which includes negative weight cycles. The algorithm works with the aid
of computing the shortest direction between every pair of vertices within the graph, the usage of a
matrix of intermediate vertices to keep music of the exceptional-recognized route thus far.

History of Floyd-Warshall algorithm:


The Floyd-Warshall set of rules was advanced independently via Robert Floyd and Stephen Warshall
in 1962. Robert Floyd turned into a mathematician and computer scientist at IBM's Thomas J. Watson
Research Center, whilst Stephen Warshall became a computer scientist at the University of
California, Berkeley.

Floyd first presented the set of rules in a technical record titled "Algorithm 97: Shortest Path" in
1962. Warshall independently discovered the set of rules quickly afterwards and posted it in his
personal technical document, "A Theorem on Boolean Matrices".

Working of Floyd-Warshall Algorithm:


1. Initialize a distance matrix D wherein D[i][j] represents the shortest distance between vertex i
and vertex j.
2. Set the diagonal entries of the matrix to 0, and all other entries to infinity.
3. For every area (u,v) inside the graph, replace the gap matrix to mirror the weight of the brink:
D[u][v] = weight(u,v).
4. For every vertex okay in the graph, bear in mind all pairs of vertices (i,j) and check if the path
from i to j through k is shorter than the current best path. If it is, update the gap matrix:
D[i][j] = min(D[i][j], D[i][k] D[k][j])
5. After all iterations, the matrix D will contain the shortest course distances between all pairs of
vertices.

Example: It works by means of keeping a matrix of distances between each pair of vertices and
updating this matrix iteratively till the shortest paths are discovered.

In this graph, the vertices are represented by letters (A, B, C, D), and the numbers on the edges
represent the weights of those edges.

To follow the Floyd-Warshall algorithm to this graph, we start by way of initializing a matrix of
distances among every pair of vertices. If two vertices are immediately related by using a side, their
distance is the load of that edge. If there may be no direct edge among vertices, their distance is
infinite.
In the first iteration of the set of rules, we keep in mind the possibility of the usage of vertex 1 (A) as
an intermediate vertex in paths among all pairs of vertices. If the space from vertex 1 to vertex 2 plus
the space from vertex 2 to vertex three is much less than the present-day distance from vertex 1 to
vertex three, then we replace the matrix with this new distance. We try this for each possible pair of
vertices.

In the second iteration, we recollect the possibility to use of vertex 2 (B) as an intermediate vertex in
paths among all pairs of vertices. We replace the matrix in the same manner as earlier before.

In the third iteration, we consider the possibility of using vertex 3 (C) as an intermediate vertex in
paths between all pairs of vertices.
Finally, in the fourth and final iteration, we consider the possibility of using vertex 4 (D) as an
intermediate ve

After the fourth iteration, we have got the shortest path between every pair of vertices in the graph.
For example, the shortest path from vertex A to vertex D is 4, which is the value in the matrix at row
A and column D.

MATRIX MULTIPLICATION
In mathematics, matrix multiplication is different from the multiplication that we perform, generally.
It is a binary operation that performs between two matrices and produces a new matrix. In this
section, we will learn matrix multiplication, its properties, along with its examples.

While we do addition or subtraction of matrices, we add or subtract the elements matching with the
positions. But in matrix multiplication, we do not so. Instead of it, we perform the dot product of the
rows and columns.

Dot Product: It is the sum of the product of the matching entries of the two sequences of the
numbers.

In matrix multiplication, it is not necessary that both matrices must be a square matrix, as in addition
and subtraction. Suppose we have a matrix A of m×n dimensions and a matrix B of n×k dimensions,
then the resultant matrix will be of m×k dimensions.

Example:
Suppose we have two matrices A and B of dimensions 2×3 and 3×2, respectively. The resultant
matrix will be a 2×2 matrix.

To find the first element of the resultant matrix, multiply the first row of matrix A by the first column
of matrix B and sum up the product.
(a,b,c).(p,q,r)=a×p+b×q+c×r
To find the second element of the resultant matrix, multiply the first row of matrix A by the second
column of matrix B and sum up the product.
(a,b,c).(x,y,z)=a×x+b×y+c×z
To find the third element of the resultant matrix, multiply the second row of matrix A by the first
column of matrix B and sum up the product.
(d,e,f).(p,q,r)=d×p+e×q+f×r
To find the fourth element of the resultant matrix, multiply the second row of matrix A by the second
column of matrix B and sum up the product.
(d,e,f).(x,y,z)=d×x+e×y+f×z
The resultant matrix is:

Multiplication of a 2×2 matrix and 2×1 matrix

Multiplication of the two 2×2 matrix

Multiplication of 3×3 matrix

Properties of Multiplication
➢ Non-commutative: AB ≠ BA
➢ Associative: A(BC) = (AB)C
➢ Left Distributive: A(B + C) = AB + AC
➢ Right Distributive: (A + B)C = AC + BC
➢ Scalar: k(AB)=(kA)B (where k is scalar)
➢ Identity: IA=AI=A
➢ Transpose: (AB)T=ATBT
Example 1: Multiply the following matrices.

TRAVELLING SALESMAN PROBLEM


Traveling salesman problem is stated as, “Given a set of n cities and distance between each pair of
cities, find the minimum length path such that it covers each city exactly once and terminates the tour
at starting city.”

Each edge (u, v) in TSP graph is assigned some non-negative weight, which represents the distance
between city u and v. This problem can be solved by finding the Hamiltonian cycle of the graph.

The distance between cities is best described by the weighted graph, where edge (u, v) indicates the
path from city u to v and w(u, v) represents the distance between cities u and v.

Algorithm
Step 1: Let d[i, j] indicates the distance between cities i and j. Function C[x, V – { x }]is the cost of
the path starting from city x. V is the set of cities/vertices in given graph. The aim of TSP is to
minimize the cost function.
Step 2: Assume that graph contains n vertices V1, V2, ..., Vn. TSP finds a path covering all vertices
exactly once, and the same time it tries to minimize the overall traveling distance.

Step 3: Mathematical formula to find minimum distance is stated below:


C(i, V) = min { d[i, j] + C(j, V – { j }) }, j ∈ V and i ∉ V.

Problem: Solve the traveling salesman problem with the associated cost adjacency matrix using
dynamic programming.
– 24 11 10 9
8 – 2 5 11
26 12 – 8 7
11 23 24 – 6
5 4 8 11 –

Solution: Let us start our tour from city 1.


Step 1: Initially, we will find the distance between city 1 and city {2, 3, 4, 5} without visiting any
intermediate city.
Cost(x, y, z) represents the distance from x to z and y as an intermediate city.
Cost(2, Φ, 1) = d[2, 1] = 24
Cost(3, Φ, 1) = d[3, 1] = 11
Cost(4, Φ , 1) = d[4, 1] = 10
Cost(5, Φ , 1) = d[5, 1] = 9

Step 2: In this step, we will find the minimum distance by visiting 1 city as intermediate city.
Cost{2, {3}, 1} = d[2, 3] + Cost(3, f, 1) = 2 + 11 = 13
Cost{2, {4}, 1} = d[2, 4] + Cost(4, f, 1) = 5 + 10 = 15
Cost{2, {5}, 1} = d[2, 5] + Cost(5, f, 1) = 11 + 9 = 20
Cost{3, {2}, 1} = d[3, 2] + Cost(2, f, 1) = 12 + 24 = 36
Cost{3, {4}, 1} = d[3, 4] + Cost(4, f, 1) = 8 + 10 = 18
Cost{3, {5}, 1} = d[3, 5] + Cost(5, f, 1) = 7 + 9 = 16
Cost{4, {2}, 1} = d[4, 2] + Cost(2, f, 1) = 23 + 24 = 47
Cost{4, {3}, 1} = d[4, 3] + Cost(3, f, 1) = 24 + 11 = 35
Cost{4, {5}, 1} = d[4, 5] + Cost(5, f, 1) = 6 + 9 = 15
Cost{5, {2}, 1} = d[5, 2] + Cost(2, f, 1) = 4 + 24 = 28
Cost{5, {3}, 1} = d[5, 3] + Cost(3, f, 1) = 8 + 11 = 19
Cost{5, {4}, 1} = d[5, 4] + Cost(4, f, 1) = 11 + 10 = 21

Step 3: In this step, we will find the minimum distance by visiting 2 cities as intermediate city.
Cost(2, {3, 4}, 1) = min { d[2, 3] + Cost(3, {4}, 1), d[2, 4] + Cost(4, {3}, 1)]}
= min { [2 + 18], [5 + 35] }
= min{20, 40} = 20

Cost(2, {4, 5}, 1) = min { d[2, 4] + Cost(4, {5}, 1), d[2, 5] + Cost(5, {4}, 1)]}
= min { [5 + 15], [11 + 21] }
= min{20, 32} = 20
Cost(2, {3, 5}, 1) = min { d[2, 3] + Cost(3, {4}, 1), d[2, 4] + Cost(4, {3}, 1)]}
= min { [2 + 18], [5 + 35] }
= min{20, 40} = 20

Cost(3, {2, 4}, 1) = min { d[3, 2] + Cost(2, {4}, 1), d[3, 4] + Cost(4, {2}, 1)]}
= min { [12 + 15], [8 + 47] }
= min{27, 55} = 27

Cost(3, {4, 5}, 1) = min { d[3, 4] + Cost(4, {5}, 1), d[3, 5] + Cost(5, {4}, 1)]}
= min { [8 + 15], [7 + 21] }
= min{23, 28} = 23

Cost(3, {2, 5}, 1) = min { d[3, 2] + Cost(2, {5}, 1), d[3, 5] + Cost(5, {2}, 1)]}
= min { [12 + 20], [7 + 28] }
= min{32, 35} = 32

Cost(4, {2, 3}, 1) = min{ d[4, 2] + Cost(2, {3}, 1), d[4, 3] + Cost(3, {2}, 1)]}
= min { [23 + 13], [24 + 36] }
= min{36, 60} = 36

Cost(4, {3, 5}, 1) = min{ d[4, 3] + Cost(3, {5}, 1), d[4, 5] + Cost(5, {3}, 1)]}
= min { [24 + 16], [6 + 19] }
= min{40, 25} = 25

Cost(4, {2, 5}, 1) = min{ d[4, 2] + Cost(2, {5}, 1), d[4, 5] + Cost(5, {2}, 1)]}
= min { [23 + 20], [6 + 28] }
= min{43, 34} = 34

Cost(5, {2, 3}, 1) = min{ d[5, 2] + Cost(2, {3}, 1), d[5, 3] + Cost(3, {2}, 1)]}
= min { [4 + 13], [8 + 36] }
= min{17, 44} = 17

Cost(5, {3, 4}, 1) = min{ d[5, 3] + Cost(3, {4}, 1), d[5, 4] + Cost(4, {3}, 1)]}
= min { [8 + 18], [11 + 35] }
= min{26, 46} = 26

Cost(5, {2, 4}, 1) = min{ d[5, 2] + Cost(2, {4}, 1), d[5, 4] + Cost(4, {2}, 1)]}
= min { [4 + 15], [11 + 47] }
= min{19, 58} = 19

Step 4: In this step, we will find the minimum distance by visiting 3 cities as intermediate city.

Cost(2, {3, 4, 5}, 1) = min { d[2, 3] + Cost(3, {4, 5}, 1), d[2, 4] + Cost(4, {3, 5}, 1), d[2, 5] +
Cost(5, {3, 4}, 1)}
= min { 2 + 23, 5 + 25, 11 + 36}
= min{25, 30, 47} = 25
Cost(3, {2, 4, 5}, 1) = min { d[3, 2] + Cost(2, {4, 5}, 1), d[3, 4] + Cost(4, {2, 5}, 1), d[3, 5] +
Cost(5, {2, 4}, 1)}
= min { 12 + 20, 8 + 34, 7 + 19}
= min{32, 42, 26} = 26

Cost(4, {2, 3, 5}, 1) = min { d[4, 2] + Cost(2, {3, 5}, 1), d[4, 3] + Cost(3, {2, 5}, 1), d[4, 5] +
Cost(5, {2, 3}, 1)}
= min {23 + 30, 24 + 32, 6 + 17}
= min{53, 56, 23} = 23

Cost(5, {2, 3, 4}, 1) = min { d[5, 2] + Cost(2, {3, 4}, 1), d[5, 3] + Cost(3, {2, 4}, 1), d[5, 4] +
Cost(4, {2, 3}, 1)}
= min {4 + 30, 8 + 27, 11 + 36}
= min{34, 35, 47} = 34

Step 5: In this step, we will find the minimum distance by visiting 4 cities as an intermediate city.
Cost(1, {2, 3, 4, 5}, 1) = min { d[1, 2] + Cost(2, {3, 4, 5}, 1), d[1, 3] + Cost(3, {2, 4, 5}, 1), d[1, 4]
+ Cost(4, {2, 3, 5}, 1) , d[1, 5] + Cost(5, {2, 3, 4}, 1)}
= min { 24 + 25, 11 + 26, 10 + 23, 9 + 34 }
= min{49, 37, 33, 43} = 33

Thus, minimum length tour would be of 33.

Trace the path: Let us find the path that gives the distance of 33.
Cost (1, {2, 3, 4, 5}, 1) is minimum due to d [1, 4], so move from 1 to 4. Path = {1, 4}.
Cost (4, {2, 3, 5}, 1) is minimum due to d [4, 5], so move from 4 to 5. Path = {1, 4, 5}.
Cost (5, {2, 3}, 1) is minimum due to d [5, 2], so move from 5 to 2. Path = {1, 4, 5, 2}.
Cost (2, {3}, 1) is minimum due to d [2, 3], so move from 2 to 3. Path = {1, 4, 5, 2, 3}.
All cities are visited so come back to 1. Hence the optimum tour would be 1 – 4 – 5 – 2 – 3 – 1.

LONGEST COMMON SUBSEQUENCE


Here longest means that the subsequence should be the biggest one. The common means that some of
the characters are common between the two strings. The subsequence means that some of the
characters are taken from the string that is written in increasing order to form a subsequence. Suppose
we have a string 'w'.
W1 = abcd
The following are the subsequences that can be created from the above string:
ab, bd, ac, ad, acd, bcd

The above are the subsequences as all the characters in a sub-string are written in increasing order
with respect to their position. If we write ca or da then it would be a wrong subsequence as characters
are not appearing in the increasing order.

The total number of subsequences that would be possible is 2n, where n is the number of characters in
a string. In the above string, the value of 'n' is 4 so the total number of subsequences would be 16.
Algorithm: Suppose X and Y are the two given sequences
Initialize a table of LCS having a dimension of X.length * Y.length
XX.label = X
YY.label = Y
LCS[0][] = 0
LCS[][0] = 0
Loop starts from the LCS[1][1]
Now we will compare X[i] and Y[j]
if X[i] is equal to Y[j] then
LCS[i][j] = 1 + LCS[i-1][j-1]
Point an arrow LCS[i][j]
Else
LCS[i][j] = max(LCS[i-1][j], LCS[i][j-1])

Example: Consider two strings:


X= a b a a b a
Y= b a b b a b

(a, b): For index i=1, j=1

Since both the characters are different so we consider the maximum value. Both contain the same
value, i.e., 0 so put 0 in (a,b). Suppose we are taking the 0 value from 'X' string, so we put arrow
towards 'a' as shown in the above table.

(a, a): For index i=1, j=2

Both the characters are the same, so the value would be calculated by adding 1 and upper diagonal
value. Here, upper diagonal value is 0, so the value of this entry would be (1+0) equal to 1. Here, we
are considering the upper diagonal value, so the arrow will point diagonally.
(a, b): For index i=1, j=3

Since both the characters are different so we consider the maximum value. The character 'a' has the
maximum value, i.e., 1. The new entry, i.e., (a, b) will contain the value 1 pointing to the 1 value.

(a, b): For index i=1, j=4

Since both the characters are different so we consider the maximum value. The character 'a' has the
maximum value, i.e., 1. The new entry, i.e., (a, b) will contain the value 1 pointing to the 1 value.

(a, a): For index i=1, j=5

Both the characters are same so the value would be calculated by adding 1 and upper diagonal value.
Here, upper diagonal value is 0 so the value of this entry would be (1+0) equal to 1. Here, we are
considering the upper diagonal value so arrow will point diagonally.

(a, b): For index i=1, j=6

Since both the characters are different so we consider the maximum value. The character 'a' has the
maximum value, i.e., 1. The new entry, i.e., (a, b) will contain the value 1 pointing to the 1 value.
(b, b): For index i=2, j=1

Both the characters are same so the value would be calculated by adding 1 and upper diagonal value.
Here, upper diagonal value is 0 so the value of this entry would be (1+0) equal to 1. Here, we are
considering the upper diagonal value so arrow will point diagonally.

(b, a): For index i=2, j=2

Since both the characters are different so we consider the maximum value. The character 'a' has the
maximum value, i.e., 1. The new entry, i.e., (a, b) will contain the value 1 pointing to the 1 value.

In this way, we will find the complete table. The final table would be:

In the above table, we can observe that all the entries are filled. Now we are at the last cell having 4
values. This cell moves at the left which contains 4 values.; therefore, the first character of the LCS is
'a'.

The left cell moves upwards diagonally whose value is 3; therefore, the next character is 'b' and it
becomes 'ba'. Now the cell has 2 value that moves on the left. The next cell also has 2 value which is
moving upwards; therefore, the next character is 'a' and it becomes 'aba'.

The next cell is having a value 1 that moves upwards. Now we reach the cell (b, b) having value
which is moving diagonally upwards; therefore, the next character is 'b'. The final string of longest
common subsequence is 'baba'.
BACKTRAKING
Overview: Backtracking is one of the techniques that can be used to solve the problem. It uses the
Brute force search to solve the problem, and the brute force search says that for the given problem,
we try to make all the possible solutions and pick out the best solution from all the desired solutions.

Backtracking name itself suggests that we are going back and coming forward; if it satisfies the
condition, then return success, else we go back again. It is used to solve a problem in which a
sequence of objects is chosen from a specified set so that the sequence satisfies some criteria.

When to use a Backtracking algorithm? When we have multiple choices, then we make the
decisions from the available choices. In the following cases, we need to use the backtracking
algorithm:
➢ A piece of sufficient information is not available to make the best choice, so we use the
backtracking strategy to try out all the possible solutions.
➢ Each decision leads to a new set of choices. Then again, we backtrack to make new decisions.
In this case, we need to use the backtracking strategy.

Working of Backtracking: Backtracking is a systematic method of trying out various sequences of


decisions until you find out that works. We start with a start node. First, we move to node A. Since it
is not a feasible solution so we move to the next node, i.e., B. B is also not a feasible solution, and it
is a dead-end so we backtrack from node B to node A.

Suppose another path exists from node A to node C. So, we move from node A to node C. It is also a
dead-end, so again backtrack from node C to node A. We move from node A to the starting node.

Now we will check any other path exists from the starting node. So, we move from start node to the
node D. Since it is not a feasible solution so we move from node D to node E. The node E is also not
a feasible solution. It is a dead end so we backtrack from node E to node D.
Suppose another path exists from node D to node F. So, we move from node D to node F. Since it is
not a feasible solution and it's a dead-end, we check for another path from node F.

Suppose there is another path exists from the node F to node G so move from node F to node G. The
node G is a success node.

Terminology of backtracking
1. Live node: The nodes that can be further generated are known as live nodes.
2. E node: The nodes whose children are being generated and become a success node.
3. Success node: The node is said to be a success node if it provides a feasible solution.
4. Dead node: The node which cannot be further generated and also does not provide a feasible
solution is known as a dead node.

Many problems can be solved by backtracking strategy, and that problems satisfy complex set of
constraints, and these constraints are of two types:
1. Implicit constraint: It is a rule in which how each element in a tuple is related.
2. Explicit constraint: The rules that restrict each element to be chosen from the given set.

Applications of Backtracking
➢ N-queen problem
➢ Sum of subset problem
➢ Graph coloring
➢ Hamiliton cycle
N-QUEENS PROBLEM
N - Queens problem is to place n - queens in such a manner on an n x n chessboard that no queens
attack each other by being in the same row, column or diagonal.

It can be seen that for n =1, the problem has a trivial solution, and no solution exists for n =2 and n
=3. So first we will consider the 4 queens’ problem and then generate it to n - queens problem. Given
a 4 x 4 chessboard and number the rows and column of the chessboard 1 through 4.

Since, we have to place 4 queens such as q1 q2 q3 and q4 on the chessboard, such that no two queens
attack each other. In such a conditional each queen must be placed on a different row, i.e., we put
queen "i" on row "i."

Now, we place queen q1 in the very first acceptable position (1, 1). Next, we put queen q2 so that
both these queens do not attack each other. We find that if we place q2 in column 1 and 2, then the
dead end is encountered.

Thus, the first acceptable position for q2 in column 3, i.e. (2, 3) but then no position is left for placing
queen 'q3' safely. So, we backtrack one step and place the queen 'q2' in (2, 4), the next best possible
solution. Then we obtain the position for placing 'q3' which is (3, 2). But later this position also leads
to a dead end, and no place is found where 'q4' can be placed safely.

Then we have to backtrack till 'q1' and place it to (1, 2) and then all other queens are placed safely by
moving q2 to (2, 4), q3 to (3, 1) and q4 to (4, 3). That is, we get the solution (2, 4, 1, 3). This is one
possible solution for the 4-queens problem. For another possible solution, the whole method is
repeated for all partial solutions. The other solutions for 4 - queens’ problems is (3, 1, 4, 2) i.e.
The implicit tree for 4 - queen problem for a solution (2, 4, 1, 3) is as follows:

Fig shows the complete state space for 4 - queens problem. But we can use backtracking method to
generate the necessary node and stop if the next node violates the rule, i.e., if two queens are
attacking.

It can be seen that all the solutions to the 4 queens’ problem can be represented as 4 - tuples (x1, x2,
x3, x4) where xi represents the column on which queen "qi" is placed.
One possible solution for 8 queens’ problem is shown in fig:

Thus, the solution for 8 -queen problem for (4, 6, 8, 2, 7, 1, 3, 5).


If two queens are placed at position (i, j) and (k, l).
Then they are on same diagonal only if (i - j) = k - l or i + j = k + l.
The first equation implies that j - l = i - k.
The second equation implies that j - l = k - i.
Therefore, two queens lie on the duplicate diagonal if and only if |j-l|=|i-k|

KNAPSACK PROBLEM
➢ The knapsack problem is useful in solving resource allocation problems.
➢ Let X = <x1, x2, x3, . . .., xn> be the set of n items,
W = <w1, w2, w3, . . . , wn> and V = <v1, v2, v3, . . . , vn> be the set of weight and value
associated with each item in X, respectively.
➢ Let M be the total capacity of the knapsack, i.e. knapsack cannot hold items having a
collective weight greater than M.
➢ Select items from X such that it maximizes the profit and the collective weight of selected
items does not exceed the knapsack capacity.
➢ The knapsack problem has two variants. 0/1 knapsack does not allow breaking up the item,
whereas a fractional knapsack does. 0/1 knapsack is also known as a binary knapsack.
➢ The Knapsack problem can be formulated as,

Maximize
sumni=1 vixi
subjected to
sumni=1 wixileM
xi in (0,1)
for binary knapsack
xi in 0,1
for fractional knapsack
Algorithm:
BK_KNAPSACK (M, W, V, fw, fp, X)
// Description: Solve knapsack problem using backtracking
// Input:
M: Knapsack capacity
W(1...n): Set of weight of the items
V(1...n): Set of profits associated with items
Fw: Final knapsack weight
Fp: Final earned profit
X(1...n): Solution vector
N: Total number of items
// Output: Solution tuple X, earned profit fp
// Initialization
cw ← 0 // Current weight
cp ← 0 // Current profit
fp ← – 1
k←1 // Index of item being processed

Example: Consider knapsack problem: n = 8. (W1, W2, W3, W4, W5, W6, W2, W8) = (1, 11, 21,
23, 33, 43, 45, 55), P = (11, 21, 31, 33, 43, 53, 55, 65), m = 110. Solve the problem using
backtracking approach.
Solution: Let arrange all items in non-decreasing order of p[i] / w[i].
i 1 2 3 4 5 6 7 8
p[i] 11 21 31 33 43 54 55 65
w[i] 1 11 21 23 33 43 45 55
p[i]/ w[i] 11 1.90 1.47 1.43 1.30 1.23 1.22 1.18
Here M = 110 and n = 8.
Initially, cp = cw = 0, fp = – 1, k = 0

For k = 1: For k=1:


cp = cp + p1 = 0 + 11 = 11 cp = cp + p2 = 11 + 21 = 32
cw = cw + w1 = 0 + 1 = 1 cw = cw + w2 = 1 + 11 = 12
cw < M, so select item 1 cw < M, so select item 2
k = k+1=2 k = k+1=2

For k = 2: For k = 3:
cp = cp + p3 = 32 + 31 = 63 cp = cp + p4 = 63 + 33 = 96
cw = cw + w3 = 12 + 21 = 33 cw = cw + w4 = 33 + 23 = 56
cw < M, so select item 3 cw < M, so select item 4
k = k+1=3 k = k+1=4

For k = 4:
cp = cp + p5 = 96 + 43 = 139
cw = cw + w5 = 56 + 33 = 89
cw < M, so select item 5
k = k+1=5
For k = 5:
cp = cp + p6 = 139 + 53 = 192
cw = cw + w6 = 89 + 43 = 132
cw > M, so reject item 6 and find upper bound
cp = cp – p6 = 192 – 53 = 139
cw = cw – w6 = 132 – 43 = 89
ub = cp + ((M – cw ) / w i+1) * pi+1
b = cp + [(110 – 89) / 43] * 53 = 164.88
Inclusion of any item from {I6, I7, I8} will exceed the capacity. So, let’s backtrack to item 4.

Upper bound at node 1:


ub = cp + ((M – cw ) / w i+1) * pi+1
= 139 + [(110 – 89)] / 43 * 53 = 164.88

Upper bound at node 2:


= 96 + [(110 – 56) / 33] * 43 = 166.09

Upper bound at node 3:


= 63 + [(110 – 33) / 33] * 43 = 163.33

You might also like