Unit 3 Vips
Unit 3 Vips
Dynamic Programming
int fib(int n)
{
if(n==0)
return 0;
if(n==1)
return 1;
sum = fib(n-1) + fib(n-2);
}
Without Dynamic Programming
Recurrence Relation
T(n)=2T(n-1)+4
Time Complexity=O(2^n)
Dynamic Programming
1.int fib(int n)
2.{
3. int A[];
4. A[0] = 0, A[1] = 1;
5. for( i=2; i<=n; i++)
6. {
7. A[i] = A[i-1] + A[i-2]
8. }
9. return A[n];
10.}
0/1 Knapsack Problem
The 0/1 knapsack problem means that the items are either completely
or no items are filled in a knapsack.
Weights: {3, 4, 6, 5} Profits: {2, 3, 1, 4}
The weight of the knapsack is 8 kg
The number of items is 4
xi = {1, 0, 0, 1}
= {0, 0, 0, 1}
= {0, 1, 0, 1}
Possible combinations= 2^4=16
. 9
0/1 Knapsack Problem
w[i] P[i] w 0 1 2 3 4 5 6 7 8
i
0 0
3 2
4 3
5 4
6 1
w[i] P[i] w 0 1 2 3 4 5 6 7 8
i
0 0 0 0 0 0 0 0 0 0 0 0
3 2 1 0 0 0 2 2 2 2 2 2
4 3 2 0 0 0 2 3 3 3 5 5
5 4 3 0 0 0 2 3 4 4 5 6
6 1 4 0 0 0 2 3 4 4 5 6
}
int knapsack(int W, int wt[], int val[], else {
int n){ K[i][w] = K[i-1][w];
int K[n+1][W+1]; }
for(int i = 0; i<=n; i++) { }
for(int w = 0; w<=W; w++) { }
if(i == 0 || w == 0) { return K[n][W];
K[i][w] = 0; }
} else if(wt[i-1] <= w) { Time Complexity=O(n^2)
K[i][w] = find_Max(val[i-1] + Space Complexity=O(n^2)
K[i-1][w-wt[i-1]], K[i-1][w]);
12
0/1 Knapsack Problem
Item A B C D
Profit 2 4 7 10
Weight 1 3 5 7
13
Floyd Warshall Algorithm
14
Floyd Warshall Algorithm
14
Floyd Warshall Algorithm
1 2 3 4
A0 1 0 3 INF 5
2 2 0 INF 4
3 INF 1 0 INF
4 INF INF 2 0
1 2 3 4
A1 1 0 3 INF 5
2 2 0 INF 4
Ak[i, j] = min (Ak-1[i, j], Ak-1[i, k] + Ak-1[k, j])
3 INF 1 0 INF
A1 [2,3]=min(A0 [2,3],A0 [2,1]+A0 [1,3])
4 INF INF 2 0
14
Floyd Warshall Algorithm
A1 1 2 3 4
1 0 3 INF 5
2 2 0 INF 4
3 INF 1 0 INF
4 INF INF 2 0
1 2 3
A2 1 0 3 INF
2 2 0 INF
Ak[i, j] = min (Ak-1[i, j], Ak-1[i, k] + Ak-1[k, j])
A2 [1,4]=min(A1 [1,4],A1 [1,2]+A1 [2,4]) 3 3 1 0
4 INF INF 2
14
Floyd Warshall Algorithm
1 2 3 4
2
A
1 0 3 INF 5
2 2 0 INF 4
3 3 1 0 5
4 INF INF 2 0
1 2 3 4
A3 1 0 3 INF 5
1 2 3 4
A3 1 0 3 INF 5
2 2 0 INF 4
3 3 1 0 5
4 5 3 2 0
1 2 3 4
A4 1 0 3 7 5
15
.
Longest Common Subsequence
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.
Example 1
W1 = abcd
W2= bcd
Longest common subsequence=bcd
16
Longest Common Subsequence
Example 2
S1 = {BCDAACD}
S2 = {ACDBAC}
Then, common subsequences are {BC}, {CDAC}, {DAC}, {AAC},
{AC}, {CD}. CDAC is the longest common subsequence.
17
.
Longest Common Subsequence
C B D A
j→ 0 1 2 3 4
i↓
0
A 1
C 2
A 3
D 4
B 5
18
Longest Common Subsequence
j→ 0 1 2 3 4
i↓
0
1
2
3
4
5
18
Longest Common Subsequence
j→ 0 1 2 3 4
i↓
0
1
2
3
18
Longest Common Subsequence
In order to find the longest common subsequence, start from the last element and follow the
direction of the arrow. 18
Longest Common Subsequence
20
Backtracking
20
Backtracking
Backtracking Algorithm
Based on depth-first recursive search
Approach
1. Tests whether solution has been found
2. If found solution, return it
3. Else for each choice that can be made
a) Make that choice
b) Recursive
c) If recursion returns a solution, return it
4. If no choices remain, return failure
Some times called “search tree”
20
Backtracking
20
Brute force method
We want to arrange the three letters a, b, c in such a way that c cannot be beside a.
we’ll build a
state-space tree.
(a,b,c) and
(c,b,a) in the
final solution set
20
Backtracking
We want to arrange the three letters x, y, z in such a way that z cannot be beside x.
we’ll build a
state-space tree.
(x,y,z) and
(z,y,x) in the
final solution set
20
Difference between the Backtracking and Dynamic Programming
Difference bwtween Backtracking and Branch and Bound
Advantage, Disadvantage and Application of Backtracking and Branch and Bound
Applications of Backtracking
N-queen problem
Sum of subset problem
Graph coloring
Hamiliton cycle
21
N-Queens Problem
Given a 4 x 4 chessboard and number the rows and column of the chessboard 1 through 4.
. 22
N-Queens Problem
. 22
N-Queens Problem
. 22
N-Queens Problem
. 22
N-Queens Problem
. 22
N-Queens Problem
. 22
Branch and bound
Branch and bound is one of the techniques used for problem solving. It is similar to the backtracking since
it also uses the state space tree. It is used for solving the optimization problems and minimization
problems. If we have given a maximization problem then we can convert it using the Branch and bound
technique by simply converting the problem into a maximization problem.We want to arrange the three
letters x, y, z in such a way that z cannot be beside x.
we’ll build a state-space tree.
23
.
Travelling Salesman Problem
You are given-
A set of some cities
Distance between every pair of cities
23
.
Travelling Salesman Problem
23
.
Travelling Salesman Problem
Rules
To reduce a matrix, perform the row
reduction and column reduction of the
matrix separately.
A row or a column is said to be
reduced if it contains at least one entry
‘0’ in it.
23
.
Travelling Salesman Problem
Step I
Row Reduction-
Reduce the elements of row-1 by 4.
Reduce the elements of row-2 by 5.
Reduce the elements of row-3 by 6.
Reduce the elements of row-4 by 2.
23
.
Travelling Salesman Problem
Column Reduction-
There is no need to reduce column-1.
There is no need to reduce column-2.
Reduce the elements of column-3 by 1.
There is no need to reduce column-4.
23
.
Travelling Salesman Problem
=4+5+6+2+1
= 18
23
.
Travelling Salesman Problem
Step 2
Choosing To Go To Vertex-B: Node-2
(Path A → B)
From the reduced matrix of step-01,
M[A,B] = 0
Set row-A and column-B to ∞
Set M[B,A] = ∞
23
.
Travelling Salesman Problem
Now,
Row Reduction
We reduce this matrix.
Then, we find out the cost of node-02.
Row Reduction-
We can not reduce row-1 as all its
elements are ∞.
Reduce all the elements of row-2 by
13.
There is no need to reduce row-3.
There is no need to reduce row-4.
23
.
Travelling Salesman Problem
Column Reduction-
Column Reduction
23
.
Travelling Salesman Problem
Cost(2)
= 18 + (13 + 5) + 0
= 36
23
.
Travelling Salesman Problem
23
.
Travelling Salesman Problem
Row Reduction-
23
.
Travelling Salesman Problem
Column Reduction-
23
.
Travelling Salesman Problem
Cost(2)
= 18 + (13 + 5) + 0
= 36
23
.
Travelling Salesman Problem
23
.
Travelling Salesman Problem
Column Reduction-
Row Reduction-
There is no need to reduce column-1.
We can not reduce row-1 as all its There is no need to reduce column-2.
elements are ∞. We can not reduce column-3 as all its
There is no need to reduce row-2. elements are ∞.
There is no need to reduce row-3. There is no need to reduce column-4.
There is no need to reduce row-4.
Thus, the matrix is already column
reduced.
Thus, the matrix is already
row-reduced.
23
.
Travelling Salesman Problem
Cost(3)
= 18 + 0 + 7
= 25
.
23
.
Travelling Salesman Problem
23
.
Travelling Salesman Problem
23
.
Travelling Salesman Problem
23
.
Travelling Salesman Problem
Step 3
Go To Vertex-B: Node-5 (Path A → C →
B)
23
.
Travelling Salesman Problem
Row Reduction-
23
.
Travelling Salesman Problem
Column Reduction-
we calculate the cost of node-5.
23
.
Travelling Salesman Problem
23
.
Travelling Salesman Problem
Row Reduction-
Column Reduction-
We can not reduce row-1 as all its
elements are ∞.
There is no need to reduce
There is no need to reduce row-2. column-1.
We can not reduce row-3 as all its We can not reduce column-2 as all
its elements are ∞.
elements are ∞. We can not reduce column-3 as all
We can not reduce row-4 as all its its elements are ∞.
elements are ∞. We can not reduce column-4 as all
its elements are ∞.
23
.
Travelling Salesman Problem
Row Reduction-
Column Reduction-
We can not reduce row-1 as
all its elements are ∞. We can not reduce column-1 as
We can not reduce row-2 as all its elements are ∞.
We can not reduce column-2 as
all its elements are ∞. all its elements are ∞.
We can not reduce row-3 as We can not reduce column-3 as
all its elements are ∞. all its elements are ∞.
We can not reduce column-4 as
We can not reduce row-4 as all its elements are ∞
all its elements are ∞.
23
.
Travelling Salesman Problem
Cost(7)
= 25 + 0 + 0
= 25
Thus,
23
.