CHAPTER FOUR
Dynamic Programming
1
SOEng3021- DDU - 2024 G.C
Dynamic Programming
▪ Dynamic programming is a technique for solving a
complex problem by first breaking into a collection of
simpler subproblems, solving each subproblem just once,
and then storing their solutions to avoid repetitive
computations.
▪ It is used to solve optimization problems.
▪ The dynamic programming guarantees to find the
optimal solution of a problem if the solution exists.
SOEng3021- DDU - 2024 G.C 2
Dynamic Programming
▪ Dynamic Programming helps to efficiently solve a class
of problems that have overlapping subproblems and
optimal substructure property.
▪ Overlapping Subproblems:
▪ When the solutions to the same subproblems are needed
repetitively for solving the actual problem.
▪ Optimal Substructure Property:
▪ If the optimal solution of the given problem can be
obtained by using optimal solutions of its subproblems.
SOEng3021- DDU - 2024 G.C 3
Dynamic Programming
▪ Recursive program of Fibonacci series:
int fib(int n) ▪ If we want to calculate fib(5), then
{ the diagrammatic representation of
if(n<=1) f(5) is shown as below:
return n;
return fib(n-1) + fib(n-2);
}
▪ F(3) is calculated twice
▪ F(2) is calculated three times
▪ This is Overlapping subproblems
property
SOEng3021- DDU - 2024 G.C 4
How it works?
▪ The following are the steps that the dynamic programming
follows:
1. It breaks down the complex problem into simpler
subproblems.
2. It finds the optimal solution to these sub-problems.
3. It stores the results of subproblems (memorization).
4. It reuses them so that same sub-problem is calculated
more than once.
5. Finally, calculate the result of the complex problem.
SOEng3021- DDU - 2024 G.C 5
Approaches of Dynamic Programming
▪ There are two approaches to dynamic programming:
Top-down and Bottom-up approaches.
1. Top-down approach:
▪ It uses the memorization technique,
▪ Memorization is equal to the sum of recursion and
caching.
▪ Recursion means calling the function itself, while caching
means storing the intermediate results.
▪ It uses the recursion technique that occupies more
memory in the call stack. (disadvantage)
SOEng3021- DDU - 2024 G.C 6
Approaches of Dynamic Programming
1. Top-down approach: Algorithm for Fibonacci sequence
if (n < 2) • Fibonacci sequence is the sequence of
then return n numbers in which every next item is
the total of the previous two items.
if (F[n] is undefined)
then F[n] = MEMOFIB (n - 1) + MEMOFIB (n - 2)
return F[n]
▪ We have used the memorization technique in which we store
the results in an array to reuse the values.
▪ This is also known as a top-down approach in which we move
from the top and break the problem into sub-problems.
SOEng3021- DDU - 2024 G.C 7
Approaches of Dynamic Programming
2. Bottom-up approach:
▪ It uses the tabulation technique
▪ It used to avoid the recursion, thus saving the memory
space.
▪ It starts from the beginning, whereas the recursive
algorithm starts from the end and works backward.
▪ If we remove the recursion, there is no stack overflow
issue and no overhead of the recursive functions.
▪ In this tabulation technique, we solve the problems and
store the results in a matrix.
SOEng3021- DDU - 2024 G.C 8
Approaches of Dynamic Programming
2. Bottom-up approach: Algorithm for Fibonacci sequence
Fib [0] = 0 • We can replace recursion with a
Fib [1] = 1 simple for-loop that just fills up
for i = 2 to n the array Fib [] to iterate over
the sub-problems.
do
Fib[i] = Fib [i - 1] + Fib [i - 2]
return F[n]
• For example, the value of a[5] will be calculated by adding the
values of a[4] and a[3], and it becomes 5 shown as below:
SOEng3021- DDU - 2024 G.C 9
Dynamic Programming Application
• The following computer problems can be solved using
dynamic programming approach:
– Fibonacci number series
– All pair shortest path by Floyd-Warshall
– Knapsack problem
– Project scheduling
– Matrix Chain Multiplication
SOEng3021- DDU - 2024 G.C 10
All Pairs Shortest Path Problem
• The problem is to find the shortest path between all the
pairs of vertices in a weighted graph.
– It computes the shortest path between every pair of
vertices of the given weighted graph.
– A weighted graph is a graph in which each edge has a
numerical value associated with it.
• Floyd-Warshall algorithm is used to solve All Pairs
Shortest Path Problem.
• As a result of this algorithm, it will generate a matrix,
which will represent the minimum distance from any
vertex to all other vertices in the graph.
SOEng3021- DDU - 2024 G.C 11
How Floyd-Warshall Algorithm Works?
▪ Consider a graph, G = {V, E} where
– V is the set of all vertices present in the graph and
– E is the set of all the edges in the graph.
▪ The graph, G, is represented in the form of an adjacency
matrix, A, that contains all the weights of every edge
connecting two vertices.
✓ Step 1: Construct an adjacency matrix A with all the
costs of edges present in the graph.
‾ If there is no path between two vertices, mark the value as
∞.
SOEng3021- DDU - 2024 G.C 12
How Floyd-Warshall Algorithm Works?
✓ Step 2: Derive another adjacency matrix A1 from A
keeping the first row and first column of the original
adjacency matrix intact in A1.
‾ And for the remaining values, say A1[i,j], if
A[i,j]>A[i,k]+A[k,j] then replace A1[i,j] with A[i,k]+A[k,j].
Otherwise, do not change the values.
‾ Here, in this step, k = 1 (first vertex acting as pivot).
✓ Step 3: Repeat Step 2 for all the vertices in the graph by
changing the k value for every pivot vertex until the final
matrix is achieved.
✓ Step 4: The final adjacency matrix obtained is the final
solution with all the shortest paths.
SOEng3021- DDU - 2024 G.C 13
Floyd-Warshall Algorithm: Pseudocode
Floyd-Warshall(w, n) // w: weights, n: number of vertices
for i = 1 to n do // initialize,
for j = 1 to n do
if (i = = j)
A[i, j] = 0 // For all diagonal elements, value = 0
if (i , j) is an edge in E
A[i, j] = w[i, j]; // If there exists a direct edge between the vertices,
value = weight of edge
else
A[i, j] = infinity // If there is no direct edge between the vertices, value = ∞
for k = 1 to n do // Compute A (k) from A (k-1)
for i = 1 to n do
for j = 1 to n do
if (A[i, k] + A[k, j] < A[i, j])
A[i, j] = A[i, k] + A[k, j];
return A[1..n, 1..n];
SOEng3021- DDU - 2024 G.C 14
Floyd-Warshall Algorithm: Example
▪ Consider the following directed weighted graph G = {V, E}.
▪ Find the shortest paths between all the vertices of the graphs
using the Floyd-Warshall algorithm.
SOEng3021- DDU - 2024 G.C 15
Floyd-Warshall Algorithm: Example
Solution:
▪ Step 1: Construct an adjacency matrix A with all the
distances as values.
• Create a matrix A of dimension n*n
where n is the number of vertices.
• The row and the column are indexed
as i and j respectively.
• i and j are the vertices of the graph.
• Each cell A[i][j] is filled with the distance from the ith vertex
to the jth vertex.
• If there is no path from ith vertex to jth vertex, the cell is left as
infinity.
SOEng3021- DDU - 2024 G.C 16
Floyd-Warshall Algorithm: Example
Solution:
▪ Step 2: Considering the above adjacency matrix as the
input, derive another matrix A1 by keeping only first
rows and columns intact. Take k = 1, and replace all the
other values by A[i, k] + A[k, j].
▪ While considering kth vertex as intermediate vertex,
there are two possibilities :
1. If k is not part of shortest path from i to j, we keep the
distance A[i, j] as it is.
2. If k is part of shortest path from i to j, update distance
A[i, j] as A[i, k] + A[k, j].
SOEng3021- DDU - 2024 G.C 17
Floyd-Warshall Algorithm: Example
Solution:
▪ We can use the following optimal substructure formula
for Floyd’s algorithm,
Ak [i, j] = min { Ak – 1 [i, j], Ak – 1 [i, k] + Ak – 1 [k, j] }
– Ak = Distance matrix after kth iteration
SOEng3021- DDU - 2024 G.C 18
Floyd-Warshall Algorithm: Example
Solution: Iteration 1 ( k = 1)
for i = 1
▪ A1[1, 2] = min{A[1, 2], A[1, 1] + A[1, 2]} = min { 5, 0 + 5} = 5
▪ A1[1, 3] = min{A[1, 3], A[1, 1] + A[1, 3]} = min {∞, 0 + ∞} = ∞
▪ A1[1, 4] = min{A[1, 4], A[1, 1] + A[1, 4]} = min {6, 0 + 6} = 6
▪ A1[1, 5] = min{A[1, 5], A[1, 1] + A[1, 5]} = min {∞, 0 + ∞} = ∞
for i = 2
▪ A1[2, 1] = min{A[2, 1], A[2, 1] + A[1, 1]} = min {∞, ∞ + 0} = ∞
▪ A1[2, 2] = min{A[2, 2], A[2, 1] + A[1, 2]} = min {0 , ∞ + 0} = 0
▪ A1[2, 3] = min{A[2, 4], A[2, 1] + A[1, 3]} = min {1, ∞ + ∞} = 1
▪ A1[2, 4] = min{A[2, 5], A[2, 1] + A[1, 4]} = min {∞, ∞ + 6} = ∞
▪ A1[2, 5] = min{A[2, 5], A[2, 1] + A[1, 5]} = min {7, ∞ + ∞} = 7
SOEng3021- DDU - 2024 G.C 19
Floyd-Warshall Algorithm: Example
Solution:
▪ Step 3: Considering the above adjacency matrix as the
input, derive another matrix A2 by keeping only first
rows and columns intact. Take k = 2, and replace all the
other values by A1[i, k]+A1[k, j].
SOEng3021- DDU - 2024 G.C 20
Floyd-Warshall Algorithm: Example
Solution:
▪ Step 4: Considering the above adjacency matrix as the
input, derive another matrix A3 by keeping only first
rows and columns intact. Take k = 3, and replace all the
other values by A2[i, k]+A2[k, j].
SOEng3021- DDU - 2024 G.C 21
Floyd-Warshall Algorithm: Example
Solution:
▪ Step 5: Considering the above adjacency matrix as the
input, derive another matrix A4 by keeping only first
rows and columns intact. Take k = 4, and replace all the
other values by A3[i, k] + A3[k, j].
SOEng3021- DDU - 2024 G.C 22
Floyd-Warshall Algorithm: Example
Solution:
▪ Step 6: Considering the above adjacency matrix as the
input, derive another matrix A5 by keeping only first
rows and columns intact. Take k = 5, and replace all the
other values by A4[i, k] + A4[k, j].
▪ The last matrix A5 represents the shortest path distance
between every pair of vertices.
SOEng3021- DDU - 2024 G.C 23
Floyd-Warshall Algorithm: Analysis
▪ The Floyd-Warshall algorithm operates using three for
loops to find the shortest distance between all pairs of
vertices within a graph.
▪ Therefore, the time complexity of the Floyd-Warshall
algorithm is O(n3), where ‘n’ is the number of vertices in
the graph.
▪ The space complexity of the algorithm is O(n2).
SOEng3021- DDU - 2024 G.C 24
Floyd-Warshall Algorithm: Exercise
▪ Consider the following directed weighted graph G = {V, E}.
▪ Find the shortest paths between all the vertices of the graphs
using the Floyd-Warshall algorithm.
SOEng3021- DDU - 2024 G.C 25
Knapsack problem
▪ The knapsack problem states that − given a set of items
(n), holding weights (wi) and profit values (vi), one must
determine the subset of the items to be added in a knapsack
(a kind of bag) such that:
▪ the total weight of the items must not exceed the limit of
the knapsack and
▪ its total profit value is maximum.
▪ Types of Knapsack problem:
1. Fractional Knapsack Problem
2. 0/1 Knapsack Problem
SOEng3021- DDU - 2024 G.C 26
Knapsack problem
1. Fractional Knapsack Problem
▪ In this problem, items are divisible.
▪ We can even put the fraction of any item into the knapsack
if taking the complete item is not possible.
▪ It is solved using Greedy Method.
2. 0/1 Knapsack Problem
▪ In this problem, items are indivisible.
▪ 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.
SOEng3021- DDU - 2024 G.C 27
Fractional Knapsack Problem
▪ Algorithm:
1. Compute value / weight ratio of all the items .
2. Sort the items in descending order based on their value /
weight ratio.
3. Start putting the items into the knapsack beginning from
the item with the highest ratio.
‾ Put as many items as you can into the knapsack.
4. If the knapsack can still store some weight, but the
weights of other items exceed the limit, the fractional part
of the next item can be added.
SOEng3021- DDU - 2024 G.C 28
Fractional Knapsack problem: Example
▪ Suppose we have a knapsack of 15 kg. The total weight of the
items to be included in the knapsack should have the weight
less than or equal to the weight of knapsack (15 kg).
▪ Here, we have to decide whether we
have to include the item or not.
SOEng3021- DDU - 2024 G.C 29
Fractional Knapsack problem: Example
Solution:
1. First calculate the profit (pi)/weight (wi) ratio of the items.
2. Sort the items in descending order based on their pi/wi ratio.
SOEng3021- DDU - 2024 G.C 30
Fractional Knapsack problem: Example
Solution:
3. Start putting the items into the knapsack beginning from the
item with the highest ratio.
• Therefore, the knapsack holds the weights = [(1 * 4) + (1 * 1)
+ (1 * 2) + (1 * 1) + (7/12 * 4)] = 15, with maximum profit of
[(1 * 10) + (1 * 2) + (1 * 2) + (1 * 1) + (7/12 * 4)] = 17.33.
SOEng3021- DDU - 2024 G.C 31
0/1 Knapsack problem: Algorithm
▪ Step 1: Create a table (T) with maximum weight of
knapsack (W) as columns and items (N) with respective
weights (wi) and profits (pi) as rows: T[N][W].
▪ Step 2: add zeroes to the 0th row and 0th column because:
– if the weight of item is 0, then it weighs nothing;
– if the maximum weight of knapsack is 0, then no item can be
added into the knapsack.
SOEng3021- DDU - 2024 G.C 32
0/1 Knapsack problem: Algorithm
▪ Step 3: Start filling the table row wise top to bottom from
left to right by using following formula:
T (i , j) = max { T ( i-1 , j ) , valuei + T( i-1 , j – weighti ) }
▪ Here, T(i , j) = maximum value of the selected items if we
can take items 1 to i and have weight restrictions of j.
▪ This step leads to completely filling the table.
▪ Then, value of the last box represents the maximum
possible value that can be put into the knapsack.
SOEng3021- DDU - 2024 G.C 33
0/1 Knapsack problem: Algorithm
for j = 0 to W
T[0, j] = 0
Fill the 0th row and 0th column of the
for i = 0 to n table to Zeros
T[i, 0] = 0
for i = 1 to n
for j = 1 to W
if wi <= j // item i can be part of the solution
if vi + T[i-1, j-wi] > T[i-1, j]
T[i, j] = vi + T[i-1, j- wi]
else
T[i, j] = T[i-1, j]
else T[i, j] = T[i-1, j] // wi > j
SOEng3021- DDU - 2024 G.C 34
0/1 Knapsack problem: Example
▪ Find an optimal solution for following 0/1 Knapsack
problem using dynamic programming:
▪ Number of Items n = 4,
▪ Knapsack Capacity W = 5,
▪ Weights (w1, w2, w3, w4) = (2, 3, 4, 5) and
▪ profits/values (v1, v2, v3, v4) = (3, 4, 5, 6).
SOEng3021- DDU - 2024 G.C 35
0/1 Knapsack problem: Example
Solution:
▪ Solution of the knapsack problem is defined as:
• Create a Table T with
N+1 rows and W+1
columns
SOEng3021- DDU - 2024 G.C 36
0/1 Knapsack problem: Example
Solution:
for j = 0 to W
T[0, j] = 0
for i = 0 to n
T[i, 0] = 0
SOEng3021- DDU - 2024 G.C 37
0/1 Knapsack problem: Example
Solution: Filling first row, i = 1
T[1, 1] ⇒ i = 1, j = 1, wi = w1 = 2, vi = 3
As, j < wi, T[i, j] = T[i – 1, j]
T[1, 1] = T[0, 1] = 0
T[1, 2] ⇒ i = 1, j = 2, wi = w1 = 2, vi = 3
As, j ≥ wi, T[i, j] = max{T[i – 1, j], vi + T[i – 1, j – wi]}
= max {T[0, 2], 3 + T[0, 0]}
T[1, 2] = max(0, 3 + 0) = 3
T[1, 3] ⇒ i = 1, j = 3, wi = w1 = 2, vi = 3
As, j ≥ wi, T[i, j] = max{T[i – 1, j], vi + T[i – 1, j – wi]}
= max {T[0, 3], 3 + T[0, 1]}
T[1, 3] = max(0, 3 + 0) = 3
SOEng3021- DDU - 2024 G.C 38
0/1 Knapsack problem: Example
Solution: Filling first row, i = 1
T[1, 4] ⇒ i = 1, j = 4, wi = w1 = 2, vi = 3
As, j ≥ wi, T[i, j] = max{T[i – 1, j], vi + T[i – 1, j – wi]}
= max{T[0, 4], 3 + T[0, 2]}
T[1, 4] = max(0, 3 + 0) = 3
T[1, 5] ⇒ i = 1, j = 5, wi = w1 = 2, vi = 3
As, j ≥ wi, T[i, j] = max{T[i – 1, j], vi + T[i – 1, j – wi]}
= max{T[0, 5], 3 + T[0, 3]}
T[1, 5] = max(0, 3 + 0) = 3
SOEng3021- DDU - 2024 G.C 39
0/1 Knapsack problem: Example
Solution: Filling first row, i = 1
SOEng3021- DDU - 2024 G.C 40
0/1 Knapsack problem: Example
Solution: Filling second row, i = 2
T[2, 1] ⇒ i = 2, j = 1, wi = 3, vi = 4
As, j < wi, T[i, j] = T[i – 1, j]
T[2, 1] = T[1, 1] = 0
T[2, 2] ⇒ i = 2, j = 2, wi = 3, vi = 4
As, j < wi, T[i, j] = T[i – 1, j]
T[2, 2] = T[1, 2] = 3
T[2, 3] ⇒ i = 2, j = 3, wi = 3, vi = 4
As, j ≥ wi, T[i, j] = max{T[i – 1, j], vi + T[i – 1, j – wi]}
= max{T[1, 3], 4 + T[1, 0]}
T[2, 3] = max(3, 4) = 4
SOEng3021- DDU - 2024 G.C 41
0/1 Knapsack problem: Example
Solution: Filling second row, i = 2
T[2, 4] ⇒ i = 2, j = 4, wi = 3, vi = 4
As, j ≥ wi, T[i, j] = max{T[i – 1, j], vi + T[i – 1, j – wi]}
= max{T[1, 4], 4 + T[1, 1]}
T[2, 4] = max(3, 4) = 4
T[2, 5] ⇒ i = 2, j = 5, wi = 3, vi = 4
As, j ≥ wi, T[i, j] = max{T[i – 1, j], vi + T[i – 1, j – wi]}
= max{T[1, 5], 4 + T[1, 2]}
T[1, 5] = max(3, 4+3) = 7
SOEng3021- DDU - 2024 G.C 42
0/1 Knapsack problem: Example
Solution: Filling second row, i = 2
SOEng3021- DDU - 2024 G.C 43
0/1 Knapsack problem: Example
Solution: Filling third row, i = 3
T[3, 1] ⇒ i = 3, j = 1, wi = 4, vi = 5
As, j < wi, T[i, j] = T[i – 1, j]
T[3, 1] = T[2, 1] = 0
T[3, 2] ⇒ i = 3, j = 2, wi = 4, vi = 5
As, j < wi, T[i, j] = T[i – 1, j]
T[3, 2] = T[2, 2] = 3
T[3 3] ⇒ i = 3, j = 3, wi = 4, vi = 5
As, j < wi, T[i, j] = T[i – 1, j]
T[3, 3] = T[2, 3] = 4
SOEng3021- DDU - 2024 G.C 44
0/1 Knapsack problem: Example
Solution: Filling third row, i = 3
T[3, 4] ⇒ i = 3, j = 4, wi = 4, vi = 5
As, j ≥ wi, T[i, j] = max{T[i – 1, j], vi + T[i – 1, j – wi]}
= max{T[2, 4], 5 + T[2, 0]}
T[3, 4] = max(4, 5 + 0) = 5
T[3, 5] ⇒ i = 3, j = 5, wi = 3, vi = 5
As, j ≥ wi, T[i, j] = max{T[i – 1, j], vi + T[i – 1, j – wi]}
= max{T[2, 5], 5 + T[2, 1]}
T[3, 5] = max(7, 5 + 0) = 7
SOEng3021- DDU - 2024 G.C 45
0/1 Knapsack problem: Example
Solution: Filling third row, i = 3
SOEng3021- DDU - 2024 G.C 46
0/1 Knapsack problem: Example
Solution: Filling fourth row, i = 4
T[4, 1] ⇒ i = 4, j = 1, wi = 5, vi = 6
As, j < wi, T[i, j] = T[i – 1, j]
T[4, 1] = T[3, 1] = 0
T[4, 2] ⇒ i = 4, j = 2, wi = 5, vi = 6
As, j < wi, T[i, j] = T[i – 1, j]
T[4, 2] = T[3, 2] = 3
T[4, 3] ⇒ i = 4, j = 3, wi = 5, vi = 6
As, j < wi, T[i, j] = T[i – 1, j]
T[4, 3] = T[3, 3] = 4
SOEng3021- DDU - 2024 G.C 47
0/1 Knapsack problem: Example
Solution: Filling fourth row, i = 4
T[4, 4] ⇒ i = 4, j = 4, wi = 5, vi = 6
As, j < wi, T[i, j] = T[i – 1, j]
T[4, 4] = T[3, 4] = 5
T[4, 5] ⇒ i = 4, j = 5, wi = 5, vi = 6
As, j ≥ wi, T[i, j] = max{T[i – 1, j], vi + T[i – 1, j – wi]}
= max{T[3, 5], 6 + T[3, 0]}
T[3, 5] = max(7, 6 + 0) = 7
SOEng3021- DDU - 2024 G.C 48
0/1 Knapsack problem: Example
Solution: Filling fourth row, i = 4
SOEng3021- DDU - 2024 G.C 49
0/1 Knapsack problem: Example
Solution:
▪ This algorithm only finds the maximum possible value
that can be carried in the knapsack
▪ i.e., the value in T[n,W]
▪ To know the items that make this maximum value, an
addition to this algorithm is necessary.
▪ So, how to find the actual Knapsack items?
SOEng3021- DDU - 2024 G.C 50
0/1 Knapsack problem: Example
Solution:
▪ All of the information we need is in the table.
▪ T[n,W] is the maximal value of items that can be placed
in the Knapsack.
▪ Let i=n and j=W
if T[i, j] ≠ T[i - 1, j] then
i = i – 1 and j = j – wi
//mark the ith item as in the knapsack
else
i = i - 1 //assume the ith item is not in the knapsack
SOEng3021- DDU - 2024 G.C 51
0/1 Knapsack problem: Example
Solution:
▪ Therefore, find the selected items for W = 5.
Step 1: Initially, set i = n = 4 and j = W = 5
▪ T[i, j] = T[4, 5] = 7
▪ T[i – 1, j] = T[3, 5] = 7
▪ T[i, j] = T[i – 1, j], so don’t select ith item and check for the
previous item.
▪ so i = i – 1 = 4 – 1 = 3
Solution Set S = { }
SOEng3021- DDU - 2024 G.C 52
0/1 Knapsack problem: Example
Solution:
Step 2: i = 3 and j = 5
▪ T[i, j] = T[3, 5] = 7
▪ T[i – 1, j] = T[2, 5] = 7
▪ T[i, j] = T[i – 1, j], so don’t select ith item and check for the
previous item.
▪ so i = i – 1 = 3 – 1 = 2
Solution Set S = { }
SOEng3021- DDU - 2024 G.C 53
0/1 Knapsack problem: Example
Solution:
Step 3: i = 2 and j = 5
▪ T[i, j] = T[2, 5] = 7
▪ T[i – 1, j] = T[1, 5] = 3
▪ T[i, j] ≠ T[i – 1, j], so add item Ii = I2 in solution set.
▪ Reduce problem size j by wi
▪ j = j – wi = j – w2 = 5 – 3 = 2
▪ i=i–1=2–1=1
Solution Set S = { I2 }
SOEng3021- DDU - 2024 G.C 54
0/1 Knapsack problem: Example
Solution:
Step 4: i = 1 and j = 2
▪ T[i, j] = T[1, 2] = 3
▪ T[i – 1, j] = T[0, 2] = 0
▪ T[i, j] ≠ T[i – 1, j], so add item Ii = I1 in solution set.
▪ Reduce problem size j by wi
▪ j = j – wi = j – w1 = 2 – 2 = 0
▪ i=i–1=1–1=0
Solution Set S = { I1, I2}
▪ Problem size has reached to 0, so final solution is S = {I1, I2}
with a maximum value (profit) = v1 + v2 = 7.
SOEng3021- DDU - 2024 G.C 55
0/1 Knapsack problem: Exercise
▪ Find an optimal solution for following 0/1 Knapsack
problem using dynamic programming:
▪ Number of Items n = 3,
▪ Knapsack Capacity W = 6,
▪ Weights (w1, w2, w3) = (3, 4, 2) and
▪ profits/values (v1, v2, v3) = (9, 8, 10).
SOEng3021- DDU - 2024 G.C 56
Conclusion
▪ Dynamic programming is a useful technique of solving
certain kind of problems.
▪ When the solution can be recursively described in terms
of partial solutions, we can store these partial solutions
and reuse them as necessary (memorization).
▪ Running time of dynamic programming algorithm vs.
naïve algorithm, for 0-1 Knapsack problem: O(W*n) vs.
O(2n).
SOEng3021- DDU - 2024 G.C 57
THANK YOU!
SOEng3021- DDU - 2024 G.C 58