DAA (Algorithms Knowledge Capsule 2 by Dr. Choudhary Ravi Singh)
DAA (Algorithms Knowledge Capsule 2 by Dr. Choudhary Ravi Singh)
Problems have more than one solution but one best solution known as optimal solution.
Knapsack:
Given a set of items, each with a weight and a value, determine the number of each item to include in a
knapsack (bag) so that the total weight is less than or equal to a given limit of knapsack and the total value is
as large as possible.
Problem Statement: You are given with ‘n’ objects with their weights and profit. You are also given a
bag or knapsack of certain size. Your objective is to fill that knapsack with maximum profit.
For the given set of items and knapsack capacity = 60 kg, find the optimal solution for the fractional
knapsack problem making use of greedy approach.
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.
Divide the large problem into smaller independent subproblems. Solve the smaller subproblems and by
combining results of independent subproblems we get the solution to the large problem.
LCS Problem:
Given two sequences X and Y, we say that the sequence Z is a common sequence of X and Y if Z is a
subsequence of both X and Y.
In the longest common subsequence problem, we are given two sequences X = (x1 x2....xm) and Y = (y1 y2 yn)
and wish to find a maximum length common subsequence of X and Y. LCS Problem can be solved using
dynamic programming.
LCS is BCBA
Solution 2:
From the table we can deduct that LCS = 6. There are several such sequences, for instance (1,0,0,1,1,0).
Matrix Chain Multiplication:
Given a set of matrices and we have applied brackets (parenthesis) in such a way that number of
multiplications should be minimum.
matrix dimension
A1 30 × 35
A2 35 × 15
A3 15 × 5
A4 5 × 10
A5 10 × 20
A6 20 × 25
Dynamic Knapsack (0/1 Knapsack):
In the case of 0/1 knapsack problem, items are indivisible. Here, indivisible means that we cannot break an
item. In this, we can either take an item or not. We either take the item completely and keep in the knapsack
or we leave the item completely. There is no possibility that we keep some fraction of the item in the knapsack.
Here, '0' means that we are not taking that item and '1' means that we are taking the item completely. This type
of problem is solved by using the dynamic programming approach.
Find the optimal solution for the 0/1 knapsack problem making use of dynamic programming approach.
Consider-
n=4
W = 5 kg
(w1, w2, w3, w4) = (2, 3, 4, 5)
(b1, b2, b3, b4) = (3, 4, 5, 6)
So, maximum possible value that can be put into the knapsack = 7.
We mark the rows labelled “1” and “2”.
Thus, items that must be put into the knapsack to obtain the maximum value 7 are-
Item-1 and Item-2
Branch and Bound:
Branch and bound algorithms are used to find the optimal solution for combinatory, discrete, and general
mathematical optimization problems.
1 2 3 4
1 0 10 15 20
2 10 0 35 25
3 15 35 0 30
4 20 25 30 0
1 2 3 4
1 0 10 15 20
2 10 0 35 25
3 15 35 0 30
4 20 25 30 0
1 2 3 4
1 0 10 15 20
2 10 0 35 25
3 15 35 0 30
4 20 25 30 0
1 2 3 4
1 0 10 15 20
2 10 0 35 25
3 15 35 0 30
4 20 25 30 0