DAA - Unit 4 (PG)
DAA - Unit 4 (PG)
As we can observe in the above figure that F(20) is calculated as the sum of
F(19) and F(18). In the dynamic programming approach, we try to divide the
problem into the similar subproblems. We are following this approach in the above
case where F(20) into the similar subproblems, i.e., F(19) and F(18). If we recap the
definition of dynamic programming that it says the similar subproblem should not be
computed more than once. Still, in the above case, the subproblem is calculated
twice. In the above example, F(18) is calculated two times; similarly, F(17) is also
calculated twice. However, this technique is quite useful as it solves the similar
subproblems, but we need to be cautious while storing the results because we are not
particular about storing the result that we have computed once, then it can lead to a
wastage of resources.
In the above tree, all the nodes on the left subtree are smaller than the value of
the root node, and all the nodes on the right subtree are larger than the value of the
root node. The maximum time required to search a node is equal to the minimum
height of the tree, equal to logn. Now we will see how many binary search trees can
be made from the given number of keys. For example: 10, 20, 30 are the keys, and
the following are the binary search trees that can be made out from these keys.
When we use the above formula, then it is found that total 5 number of trees
can be created. The cost required for searching an element depends on the
comparisons to be made to search an element.
Dynamic Approach for Optimal Binary Search Tree
Consider the below table, which contains the keys and frequencies.
When i=1 and j=3, then keys 20 and 30. There are two possible trees that can be
made out from these two keys shown below:
In the first binary tree, cost would be: 1*2 + 2*6 = 14
In the second binary tree, cost would be: 1*6 + 2*2 = 10
The minimum cost is 10; therefore, c[1,3] = 10
When i=2 and j=4, we will consider the keys at 3 and 4, i.e., 30 and 40. There are
two possible trees that can be made out from these two keys shown as below:
In the first binary tree, cost would be: 1*6 + 2*3 = 12
In the second binary tree, cost would be: 1*3 + 2*6 = 15
The minimum cost is 12, therefore, c[2,4] = 12
In the above tree, 10 is the root node, 20 is the right child of node 10, and 30 is the
right child of node 20.
Cost would be: 1*4 + 2*2 + 3*6 = 26
In the above tree, 10 is the root node, 30 is the right child of node 10, and 20 is the
left child of node 20.
In the above tree, 20 is the root node, 30 is the right child of node 20, and 10 is the
left child of node 20.
Cost would be: 1*2 + 4*2 + 6*2 = 22
The following are the trees that can be created if 30 is considered as the root node.
In the above tree, 30 is the root node, 20 is the left child of node 30, and 10 is the left
child of node 20.
Cost would be: 1*6 + 2*2 + 3*4 = 22
In the above tree, 30 is the root node, 10 is the left child of node 30 and 20 is the right
child of node 10.
Cost would be: 1*6 + 2*4 + 3*2 = 20
Therefore, the minimum cost is 20 which is the 3rd root. So, c[0,3] is equal to 20.
When i=1 and j=4 then we will consider the keys 20, 30, 40
c[1,4] = min{ c[1,1] + c[2,4], c[1,2] + c[3,4], c[1,3] + c[4,4] } + 11
= min{0+12, 2+3, 10+0}+ 11
= min{12, 5, 10} + 11
The minimum value is 5; therefore, c[1,4] = 5+11 = 16
The fractional knapsack problem means that we can divide the item. For example, we
have an item of 3 kg then we can pick the item of 2 kg and leave the item of 1 kg. The
fractional knapsack problem is solved by the Greedy approach.
To consider all subsets of items, there can be two cases for every item:
(1) the item is included in the optimal subset,
(2) not included in the optimal set.
Therefore, the maximum value that can be obtained from n items is max of
following two values.
1) Maximum value obtained by n-1 items and W weight (excluding nth item).
2) Value of nth item plus maximum value obtained by n-1 items and W minus
weight of the nth item (including nth item).
If weight of nth item is greater than W, then the nth item cannot be included
and case 1 is the only possibility.
Example: Consider the problem having weights and profits are:
Weights: {3, 4, 6, 5}; Profits: {2, 3, 1, 4} ; The weight of the knapsack is 8 kg & the
number of items is 4
First, we create a matrix shown as below:
0 1 2 3 4 5 6 7 8
0
1
2
3
4
In the above matrix, columns represent the weight, i.e., 8. The rows represent
the profits and weights of items. Here we have not taken the weight 8 directly,
problem is divided into sub-problems, i.e., 0, 1, 2, 3, 4, 5, 6, 7, 8. The solution of the
sub-problems would be saved in the cells and answer to the problem would be stored
in the final cell. First, we write the weights in the ascending order and profits
according to their weights shown as below:
wi = {3, 4, 5, 6}
pi = {2, 3, 4, 1}
The first row and the first column would be 0 as there is no item for w=0
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0
2 0
3 0
4 0
When i = 1, W = 2
w1 = 3; Since we have only one item in the set having weight 3, but the capacity of
the knapsack is 2. We cannot fill the item of 3kg in the knapsack of capacity 2 kg so
add 0 at M[1][2] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0
2 0
3 0
4 0
When i=1, W=3 w1 = 3; Since we have only one item in the set having weight equal
to 3, and weight of the knapsack is also 3; therefore, we can fill the knapsack with an
item of weight equal to 3. We put profit corresponding to the weight 3, i.e., 2 at
M[1][3] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2
2 0
3 0
4 0
When i=1, W = 5
W1 = 3; Since we have only one item in the set having weight equal to 3, and weight
of the knapsack is 5; therefore, we can fill the knapsack with an item of weight equal
to 3. We put profit corresponding to the weight 3, i.e., 2 at M[1][5] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2
2 0
3 0
4 0
When i=1, W = 7
W1 = 3; Since we have only one item in the set having weight equal to 3, and weight
of the knapsack is 7; therefore, we can fill the knapsack with an item of weight equal
to 3. We put profit corresponding to the weight 3, i.e., 2 at M[1][7] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2
2 0
3 0
4 0
When i =1, W =8
W1 = 3; Since we have only one item in the set having weight equal to 3, and weight
of the knapsack is 8; therefore, we can fill the knapsack with an item of weight equal
to 3. We put profit corresponding to the weight 3, i.e., 2 at M[1][8] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0
3 0
4 0
Repeating the same process for all the iterations, the final table will be:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 2 3 3 3 5 5
4 0 0 0 2 3 3 4 5 5
As we can observe in the above table that 5 is the maximum profit among all
the entries. The pointer points to the last row and the last column having 5 value.
Now we will compare 5 value with the previous row; if the previous row, i.e., i = 3
contains the same value 5 then the pointer will shift upwards.
Again, we will compare the value 5 from the above row, i.e., i = 2. Since the
above row contains the value 5 so the pointer will again be shifted upwards.
The set of items to take can be deduced from the table, starting at c[n, w] and
tracing backwards where the optimal values came from.
If c[i, w] = c[i-1, w], then item i is not part of the solution, and we continue
tracing with c[i-1, w]. Otherwise, item i is part of the solution, and we continue
tracing with c[i-1, w-W].
We try to find the possible options that exist. This can be done by iterating over the
adjacency matrix of A or the other way of doing so can be iterating over other cities
and checking if they can be a possible option to travel from A. As we begin, we visit
city A, and the bit mask value is now changed to 0001.
This way we explore each available option from the starting node. We also
maintain a minimum cost variable that maintains the minimum cost spent to visit
each city and return to the original city.
The recursion tree would look like this:
The time complexity of the Travelling Salesman Problem comes out to be O(n-1!).
The space complexity of this code comes out to be O(n).
Solution:
The smallest job is D, which is on machine M1, so schedule this job last. And skip
that job from further consideration.
x x x x D