0% found this document useful (0 votes)
11 views10 pages

DAA (Algorithms Knowledge Capsule 2 by Dr. Choudhary Ravi Singh)

Algorithms Knowledge Capsule 2 by Dr. Choudhary Ravi Singh

Uploaded by

ravi singh
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)
11 views10 pages

DAA (Algorithms Knowledge Capsule 2 by Dr. Choudhary Ravi Singh)

Algorithms Knowledge Capsule 2 by Dr. Choudhary Ravi Singh

Uploaded by

ravi singh
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/ 10

DAA KNOWLEDGE CAPSULE - 2

Optimization Problems By: Dr. Choudhary Ravi Singh

Problems have more than one solution but one best solution known as optimal solution.

Approaches to solve optimization problems


Greedy
A greedy algorithm is a simple, intuitive algorithm that is used in optimization problems. The algorithm makes
the optimal choice at each step as it attempts to find the overall optimal way to solve the entire problem.

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.

Fractional Knapsack Problem:


The fractional knapsack problem is also one of the techniques which are used to solve the knapsack problem.
In fractional knapsack, the items are broken in order to maximize the profit. The problem in which we break
the item is known as a Fractional knapsack problem.

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.

here X = (A,B,C,B,D,A,B) and Y = (B,D,C,A,B,A)


m = length [X] and n = length [Y]
m = 7 and n = 6
Here x1= x [1] = A y1= y [1] = B
x 2 = B y2 = D
x 3 = C y3 = C
x 4 = B y4 = A
x5= D y5= B
x6= A y6= A
x7 = B

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.

We are given the sequence {4, 10, 3,


12, 20, and 7}. The matrices have
size 4 x 10, 10 x 3, 3 x 12, 12 x 20,
20 x 7.
Number of Multiplications: 1344
A1 (A2 (A3 (A4 A5)))

Ex: Solve Yourself

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.

Knapsack weight capacity = W


Number of items each having some weight and value = n
Draw a table say ‘T’ with (n+1) number of rows and (W+1) number of columns.
For the given set of items and knapsack capacity (W) = 5 kg, find the optimal solution for the 0/1 knapsack
problem making use of dynamic programming approach.
Or

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.

Travelling Salesman Problem:


You are given-
A set of some cities, Distance between every pair of cities
Travelling Salesman Problem states- A salesman has to visit every city exactly once. He has to come back to
the city from where he starts his journey. What is the shortest possible route that the salesman must follow to
complete his tour?

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

Travelling distance is: 10+25+30+15=80

You might also like