28, 31Dynamic Programming (Fibonacci) (Dynamic Knapsack)
28, 31Dynamic Programming (Fibonacci) (Dynamic Knapsack)
Programming
Dynamic Programming is a
general algorithm design
technique for solving problems
defined by recurrences with
overlapping subproblems
2
Dynamic Programming
•Not every sub-problem is new.
•Save time: retain prior results.
•Dynamic programming works by storing the result of
subproblems so that when their solutions are required, they
are at hand and we do not need to recalculate them.
3
Dynamic programming is mostly applied to recursive
algorithms. most optimization problems require recursion
and dynamic programming is used for optimization.
Recursion vs But not all problems that use recursion can use Dynamic
Programming.
Dynamic
Programmin Unless there is a presence of overlapping subproblems like
in the fibonacci sequence problem, a recursion can only
g reach the solution using a divide and conquer approach.
4
Greedy Algorithms are similar to dynamic
programming in the sense that they are
both tools for optimization.
Greedy
Algorithms However, greedy algorithms look for locally
optimum solutions or in other words, a
vs Dynamic
greedy choice, in the hopes of finding a
global optimum. Hence greedy algorithms
can make a guess that looks optimum at the
time but becomes costly down the line and
5
Approaches of dynamic programming
6
Top-down approach
Here memorization is equal to the sum of recursion and
caching. Recursion means calling the function itself, while
caching means storing the intermediate results.
Advantages
•It is very easy to understand and implement.
•It solves the subproblems only when it is required.
•It is easy to debug.
Disadvantages
It uses the recursion technique that occupies more memory in the call
stack. Sometimes when the recursion is too deep, the stack overflow
condition will occur.
It occupies more memory that degrades the overall performance.
Bottom-Up approach
• The bottom-up approach is also one of the techniques which can be used to
implement the dynamic programming.
F(n)
F(n-1) + F(n-2)
• Efficiency:
• Time – O(n)
Fibonacci Numbers
• The bottom-up approach is only (n).
• Why is the top-down so inefficient?
• Recomputes many sub-problems.
• How many times is F(n-5) computed?
F(n)
•Shortest path algorithms like Dijkstra's Algorithm that aren't able to detect such a cycle can
give an incorrect result because they can go through a negative weight cycle and reduce the
path length.
Steps of Bellman-Ford Algorithm
• This algorithm takes as input a directed weighted graph and a starting vertex.
• The first step is to initialize the vertices.
• The algorithm initially set the distance from starting vertex to all other vertices to
infinity. The distance between starting vertex to itself is 0.
• After the initialization step, the algorithm started calculating the shortest distance
from the starting vertex to all other vertices.
• This step runs (|V| - 1 ) times. Within this step, the algorithm tries to explore
different paths to reach other vertices and calculates the distances.
• If the algorithm finds any distance of a vertex that is smaller then the previously
stored value then it relaxes the edge and stores the new value.
• Finally, when the algorithm iterates (|V| - 1 ) times and relaxes all the required
edges, the algorithm gives a last check to find out if there is any negative cycle in
the graph.
Steps of Bellman-Ford Algorithm
• If there exists a negative cycle then the distances will keep decreasing.
• In such a case, the algorithm terminates and gives an output that the
graph contains a negative cycle hence the algorithm can’t compute the
shortest path.
• To identify the items that must be put into the knapsack to obtain that
maximum profit,
• Consider the last column of the table.
• Start scanning the entries from bottom to top.
• On encountering an entry whose value is not same as the value
stored in the entry immediately above it, mark the row label of that
entry.
• After all the entries are scanned, the marked labels represent the
items that must be put into the knapsack.
Time Complexity-
• Each entry of the table requires constant time θ(1) for its
computation.
• It takes θ(nw) time to fill (n+1)(w+1) table entries.
• It takes θ(n) time for tracing the solution since tracing process
traces the n rows.
• Thus, overall θ(nw) time is taken to solve 0/1 knapsack problem
using dynamic programming.
PRACTICE PROBLEM BASED ON
0/1 KNAPSACK PROBLEM-
• 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)
for i = 1 to n
B[i,0] = 0
Items:
We’re DONE!!
The max possible value that can be carried in this knapsack is $7
Knapsack 0-1 Algorithm
• This algorithm only finds the max possible value that can be carried in
the knapsack
• The value in B[n,W]
• To know the items that make this maximum value, we need to trace
back through the table.
Knapsack 0-1 Algorithm
Finding the Items
• Let i = n and k = W
if B[i, k] ≠ B[i-1, k] then
mark the ith item as in the knapsack
i = i-1, k = k-wi
else
i = i-1 // Assume the ith item is not in the knapsack
// Could it be in the optimally packed knapsack?
Items: Knapsack:
Knapsack 0-1 Algorithm 1: (2,3)
Finding the Items 2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=4
1 0 0 3 3 3 3 k=5
2 0 0 3 4 4 7 vi = 6
3 0 0 3 4 5 7 wi = 5
4 0 0 3 4 5 7 B[i,k] = 7
B[i-1,k] = 7
i=n,k=W
while i, k > 0
if B[i, k] ≠ B[i-1, k] then
mark the ith item as in the knapsack
i = i-1, k = k-wi
else
i = i-1
Items: Knapsack:
Knapsack 0-1 Algorithm 1: (2,3)
Finding the Items 2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=3
1 0 0 3 3 3 3 k=5
2 0 0 3 4 4 7 vi = 5
3 0 0 3 4 5 7 wi = 4
4 0 0 3 4 5 7 B[i,k] = 7
B[i-1,k] = 7
i=n,k=W
while i, k > 0
if B[i, k] ≠ B[i-1, k] then
mark the ith item as in the knapsack
i = i-1, k = k-wi
else
i = i-1
Items: Knapsack:
Knapsack 0-1 Algorithm 1: (2,3) Item 2
Finding the Items 2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=2
1 0 0 3 3 3 3 k=5
2 0 0 3 4 4 7 vi = 4
3 0 0 3 4 5 7 wi = 3
4 0 0 3 4 5 7 B[i,k] = 7
B[i-1,k] = 3
k – wi = 2
i=n,k=W
while i, k > 0
if B[i, k] ≠ B[i-1, k] then
mark the ith item as in the knapsack
i = i-1, k = k-wi
else
i = i-1
Items: Knapsack:
Knapsack 0-1 Algorithm 1: (2,3) Item 2
Finding the Items 2: (3,4) Item 1
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=1
1 0 0 3 3 3 3 k=2
2 0 0 3 4 4 7 vi = 3
3 0 0 3 4 5 7 wi = 2
4 0 0 3 4 5 7 B[i,k] = 3
B[i-1,k] = 0
k – wi = 0
i=n,k=W
while i, k > 0
if B[i, k] ≠ B[i-1, k] then
mark the ith item as in the knapsack
i = i-1, k = k-wi
else
i = i-1
Items: Knapsack:
Knapsack 0-1 Algorithm 1: (2,3) Item 2
Finding the Items 2: (3,4) Item 1
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=1
1 0 0 3 3 3 3 k=2
2 0 0 3 4 4 7 vi = 3
3 0 0 3 4 5 7 wi = 2
4 0 0 3 4 5 7 B[i,k] = 3
B[i-1,k] = 0
k – wi = 0
k = 0, so we’re DONE!
for i = 1 to n O(n)
B[i,0] = 0
Repeat n times
for i = 1 to n
O(W)
for w = 0 to W
< the rest of the code >
n
Knapsack Problem
1) Fill out the dynamic
programming table
for the knapsack
problem to the
right.
2) Trace back through
the table to find the
items in the
knapsack.
Example
•Let us consider that the capacity of the
knapsack is W = 8 and the items are as
shown in the following table.
Item A B C D
Profit 2 4 7 10
Weigh 1 3 5 7
t