0% found this document useful (0 votes)
17 views38 pages

12 23ec60 Data Structures and Algorithms Kir Dynammic Programming

The document provides an overview of dynamic programming, emphasizing its use in optimization problems that require optimal substructures and overlapping subproblems. It includes examples such as the 0/1 Knapsack Problem and the Longest Common Subsequence, detailing the principles and algorithms involved in solving these problems. Additionally, it presents a C programming implementation for the 0/1 Knapsack Problem, demonstrating how to calculate maximum profit and identify included items.

Uploaded by

rivervaigai19
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)
17 views38 pages

12 23ec60 Data Structures and Algorithms Kir Dynammic Programming

The document provides an overview of dynamic programming, emphasizing its use in optimization problems that require optimal substructures and overlapping subproblems. It includes examples such as the 0/1 Knapsack Problem and the Longest Common Subsequence, detailing the principles and algorithms involved in solving these problems. Additionally, it presents a C programming implementation for the 0/1 Knapsack Problem, demonstrating how to calculate maximum profit and identify included items.

Uploaded by

rivervaigai19
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/ 38

23EC460

DATA STRUCTURES
AND
ALGORITHMS
DYNAMMIC PROGRAMMING

Prepared by,
Mrs. K. Iyshwarya Ratthi
Assistant Professor,
Department of ECE

K. Iyshwarya Ratthi, Image Processing Lab,


5/3/2025 TCE 1
Dynamic Programming
• A dynamic programming algorithm remembers past results and uses them to
find new results
• Dynamic programming is generally used for optimization problems
• Multiple solutions exist, need to find the best one.
• Requires optimal substructures and overlapping subproblems.
 Optimal substructure: Optimal solution contains optimal solutions
to subproblems
 Overlapping subproblems: Solutions to subproblems can be stored
and reused in a bottom-up fashion
• This differs from Divide and Conquer, where subproblems generally need not
overlap.
Principle of optimality
The principle of optimality in dynamic programming is a fundamental
concept that states:

"An optimal solution to a problem contains within it optimal


solutions to subproblems."

Explanation:
If you want to solve a complex problem optimally using dynamic
programming, you break it down into smaller overlapping
subproblems.
The principle of optimality ensures that:
• Solving each subproblem optimally is sufficient.
• The combination of these optimal subproblem solutions will give
the optimal solution to the overall problem.
Examples of dynamic programming:
1. Knapsack problem
2. Longest common subsequence
0/1 Knapsack Problem

In 0/1 Knapsack Problem,


• As the name suggests, items are indivisible here.
• We can not take the fraction of any item.
• We have to either take an item completely or leave it completely.
• It is solved using dynamic programming approach.

Maximum weight the knapsack can hold is 8


Find the maximum value and give its solution.
• The maximum profit we have is 8. Now, which objects have to be included in the
knapsack to achieve the maximum profit?

• Starting from the last object, we have a profit of 6 from object 4. We can include
object 4 and subtract the profit of object 4 from the maximum profit. So, 8-6 = 2 is
the profit remaining. We have to now check by including which object we can get a
profit of 2.

• Check for 2 in the above row (i=3). Is there a 2? Yes, it is. But is it solely achieved
from object 3? No. Why because there is one more 2 in the above row as well (i=2).
It can be understood that it is obtained by including another object, not 2. Now
check in the above row for 2. Yes, it exists. It is achieved from object 2. Hence,
include object 2. The remaining profit is 2-2=0. So, we got our solution.

• Including objects 2 and 4 gives a maximum of profit 8.


#include <stdio.h>
#include <stdlib.h>

int n, W, w[10], v[10], V[10][10], x[10];

int max(int a, int b)


{
if (a > b)
return a;
else
return b;
}

n: Number of items.
W: Maximum weight the knapsack can hold.
w[10]: Array to store weights of items.
v[10]: Array to store values/profits of items.
V[10][10]: 2D array (DP table) to store maximum profit at each subproblem.
x[10]: Array to mark whether an item is included in the optimal solution.
void Knapsack()
{
int i, j; Loops through each item (i) and weight capacity (j).
for (i = 0; i <= n; i++)
{
for (j = 0; j <= W; j++)
{
if (i == 0 || j == 0) Base case: If no items or weight capacity is 0, profit is 0.
V[i][j] = 0;
else if (j < w[i])
V[i][j] = V[i - 1][j];
else
V[i][j] = max(V[i - 1][j], V[i - 1][j - w[i]] + v[i]);

printf("%d\t", V[i][j]); // Print Dynammic Programming table value


}
printf("\n");
}
}
void Knapsack()
{
int i, j;
for (i = 0; i <= n; i++) Is the current item too heavy to fit in the knapsack?
{
for (j = 0; j <= W; j++) The current item's weight is w[i] = 4
{ And the current knapsack capacity is j = 3
if (i == 0 || j == 0)
V[i][j] = 0; Then, since 4 > 3, the item cannot be included.
else if (j < w[i]) We ignore the current item and copy the value (max profit)
V[i][j] = V[i - 1][j]; from the previous item:
else
V[i][j] = max(V[i - 1][j], V[i - 1][j - w[i]] + v[i]);

printf("%d\t", V[i][j]); // Print Dynammic Programming table value


}
printf("\n");
}
}
void printsolution()
{
int i, j;
i = n;
j = W; Start tracing back from the last cell of the DP table.

while (i != 0 && j != 0) If the value differs from the one above, it means the item
{ was included.
if (V[i][j] != V[i - 1][j])
{ Mark it as included and reduce the capacity.
x[i] = 1;
j = j - w[i];
}
i--;
}
}
int main()
{
int i;

printf("\n Read no. of objects: ");


scanf("%d", &n);

printf("\n Read knapsack capacity: ");


scanf("%d", &W);

printf("\n Read weights of the objects\n");


for (i = 1; i <= n; i++)
scanf("%d", &w[i]);

printf("\n Read profits of the objects\n");


for (i = 1; i <= n; i++)
scanf("%d", &v[i]);
Call functions to solve the knapsack and determine which
Knapsack();
items are selected.
printsolution();

printf("object\tweight\tprofit\n");
for (i = 1; i <= n; i++)
if (x[i] == 1)
printf("%d\t%d\t%d\n", i, w[i], v[i]);

printf("\n Max profit is %d\n", V[n][W]);

return 0;
} Print selected items and their weights/profits.

Print the maximum profit from the DP table and exit the
program.
Dynamic programming table V[i][j]
Longest Common Subsequence

• Subsequence:
• BDFH is a subsequence of ABCDEFGH
• If X and Y are sequences, a common subsequence
is a sequence which is a subsequence of both.
• BDFH is a common subsequence of ABCDEFGH
and of ABDFGHI
• A longest common subsequence…
• …is a common subsequence that is longest.
• The longest common subsequence of ABCDEFGH and
ABDFGHI is ABDFGH.

You might also like