Dynamic Programming M. Ashraf Iqbal http://www.istockphoto.com/stock-illustration- 12665058-armed-robbery.php Distributed by : www.myUET.net.tc www.myUET.net.tc
Oct 14, 2014
Distributed by : www.myUET.net.tc
Dynamic Programming
M. Ashraf Iqbal
http://www.istockphoto.com/stock-illustration-12665058-armed-robbery.php
www.myUET.net.tc
Distributed by : www.myUET.net.tc
The Knapsack Problem
Distributed by : www.myUET.net.tc
The 0/1 Knapsack Problem
• The 0-1 knapsack problem: Either put an item once in the knapsack or ignore it so as to maximize total profit while keeping the weight inside the knapsack within its capacity. Alternately we may like to ignore profits and try to maximize the weight in the knapsack such that it does not exceed its capacity. This is quite similar to the subset sum problem in mathematics.
Distributed by : www.myUET.net.tc
The Fractional knapsack problem: Either put an item or its fraction in the knapsack or ignore it so as to maximize total profit while keeping the weight inside the knapsack exactly equal to its capacity.
You can Cut an Item to Fit
http://www.google.com.pk/imgres?imgurl=http://images.clipartof.com/small/5669-Male-Carpenter-Cutting-Wood-On-A-Sawhorse-Clipart-Illustration.jpg&imgrefurl=http://www.clipartof.com/details/clipart/5669.html&h=368&w=450&sz=55&tbnid=QzoxPWsL_w7nAM:&tbnh=104&tbnw=127&prev=/images%3Fq%3Dimage%2Bof%2Bcutting%2Bwood&zoom=1&q=image+of+cutting+wood&hl=en&usg=__13lywSPGfp84b9pE_jq9zFTGznw=&sa=X&ei=1KuRTLvNGoGyvgO81ICJBA&ved=0CBgQ9QEwAA
Distributed by : www.myUET.net.tc
Un-Bounded 0/1 Knapsack
The unbounded knapsack problem assumes that we have an unlimited supply of each weight. We intend to fill the knapsack as far as possible while keeping the total number of weights minimum (also known as the Change Problem). We can also maximize profit provided profit for each weight is also given.
Distributed by : www.myUET.net.tc
Un-bounded 0/1 Knapsack
http://www.fotosearch.com/BLD007/jp2005_0002521/
Distributed by : www.myUET.net.tc
Another Knapsack Problem
The bounded 0/1 knapsack problem restricts the number of each item to a fixed maximum value.
Distributed by : www.myUET.net.tc
Objectives1. We may like to maximize the number of weights to be placed in the knapsack
such that the total weight remains within the knapsack capacity in a 0/1 scenario.
2. We may like to maximize the profit provided the profit of each weight is the same.
3. We may like to maximize profit provided the weight of each item is the same.
Distributed by : www.myUET.net.tc
How Many Knapsack Problems?
• What Strategies should be used?• Greedy?• Dynamic Programming?• Brute Force – look at all options?• What is Greedy• What is Dynamic Programming• What is the relationship between Greedy, Dynamic
Programming, & Brute force Strategies?
How Many Knapsack?
Distributed by : www.myUET.net.tc
Which Strategy should be used?• Brute Force is expensive but?• Dynamic Programming is intelligent but?• Greedy is FAST but?
Distributed by : www.myUET.net.tc
Which Strategy should be used?• Brute Force can solve every thing?• Dynamic Programming can solve some thing?• How about Greedy?
Distributed by : www.myUET.net.tc
Start with the Change Problem?
Distributed by : www.myUET.net.tc
Start with the Change Problem?
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
The Recursion Tree
Distributed by : www.myUET.net.tc
The Change Problem?Recursive Formulation? Recursion Tree?
Distributed by : www.myUET.net.tc
The Change Problem transforms into a Graph Problem?
Find a Change = Find a Path?
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Find a Change = Find a Shortest Path?
Distributed by : www.myUET.net.tc
Where is Brute Force, DP, & Greedy?
Distributed by : www.myUET.net.tc
Do it by Brute Force?
Distributed by : www.myUET.net.tc
Do it by Dynamic Programming?
Distributed by : www.myUET.net.tc
Do it by Greedy?
Distributed by : www.myUET.net.tc
The Change Problem?
• Brute Force provides an Optimal answer?
• Dynamic Programming provides an Optimal answer?
• Greedy strategy provides an Optimal answer?
Distributed by : www.myUET.net.tc
Profits & Weights in a Knapsack?
How to start now?
Distributed by : www.myUET.net.tc
Profits & Weights in a Knapsack?
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Where is Brute Force, DP, & Greedy?
Distributed by : www.myUET.net.tc
Do it by Brute Force?
Distributed by : www.myUET.net.tc
Greedy: The Graph collapses?
Distributed by : www.myUET.net.tc
Dynamic Programming: It Collapses?
Distributed by : www.myUET.net.tc
Do it by Dynamic Programming?
Distributed by : www.myUET.net.tc
The Change ProblemDo it by Dynamic
Programming?
Distributed by : www.myUET.net.tc
The Change ProblemDo it by Dynamic
Programming?
Distributed by : www.myUET.net.tc
The Change ProblemDo it by Dynamic
Programming?
Distributed by : www.myUET.net.tc
Maximizing ProfitDo it by Dynamic
Programming?
Weights = 4 3 2 Profit = 10 8 9
Distributed by : www.myUET.net.tc
Maximizing ProfitDo it by Dynamic
Programming?
Weights = 4 3 2 Profit = 10 8 9
Distributed by : www.myUET.net.tc
Time complexity calculations?
Distributed by : www.myUET.net.tc
The Knapsack Problem?
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
The Knapsack Problem?
• The above diagram shows a recursive implementation of which recursive equation? Discuss briefly. We have not yet gone into dynamic programming.
• How can you find (from the above diagram) the combination of weights that exactly fits the knap sack of given capacity equal to 11. Discuss briefly.
• The answer to the above part requires a cute reduction of this problem into a graph problem? What is that graph problem? Please discuss.
• What will be the time complexity of this recursive implementation?
Distributed by : www.myUET.net.tc
A New Recursive Formulation?
P(5,11) = P(4,5) + 6 = P(4,6) + 5 = P(4,7) + 4 = P(4,8) + 3 = P(4,9) + 2
max
Distributed by : www.myUET.net.tc
A “Last Year” Mistake??
Distributed by : www.myUET.net.tc
Last Year Class: Hisaan to the rescue
Distributed by : www.myUET.net.tc
Alternate Recursive Formulation?
Distributed by : www.myUET.net.tc
Old Recursive Formulation?
Distributed by : www.myUET.net.tc
The Knapsack Problem
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Another Knapsack Problem
• Problem: We have k lists of integers and a knapsack of capacity T. We need to select one item from each list such as to maximize the total weight in the knapsack without overflowing it. Here is recursive solution unfolding itself with some common sense.
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
• Problem: We have k lists of integers and a knapsack of capacity T. We need to select one item from each list such as to maximize the total profit in the knapsack such that the weight in the knapsack remains within its capacity. Here is recursive solution unfolding itself with some common sense.
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Problem: We have k lists of integers and a knapsack of capacity T. We need to select one item from each list such as to maximize the total weight in the knapsack without overflowing it. Here is an efficient dynamic programming solution.
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
The Knapsack Problem• We intend to maximize profit keeping the total weight
within the knapsack capacity. Find a recursive equation corresponding to this scenario. How will you implement this recursive equation on the diagram above?
• How can you find (from the above recursive diagram) the combination of weights that maximizes total profit keeping the total weight within knapsack capacity? Discuss briefly.
• How can you reduce this graph and thus reduce the time complexity of this recursive implementation (without going into dynamic programming).
Distributed by : www.myUET.net.tc
The Problem of Maximizing Profit?
Distributed by : www.myUET.net.tc
The Problem of Maximizing Number of Elements in a single Knapsack
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
The Activity Selection Problem?
Distributed by : www.myUET.net.tc
Activity Selection Problem?
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Activity Selection Problem
• Maximize Total Merit with Start time and Finish time fixed for each activity?
• Maximize Total Merit when only Duration is fixed for each activity?
• Maximize the Total Utilization of the Hall?• Maximize the Total Number of activities?
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Activity Selection Problem?
Distributed by : www.myUET.net.tc
Maximize Merit
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Com
plex
ity o
f Gra
ph C
onst
ructi
on
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
The Partitioning Problem• Problem: Given a fixed sequence of n numbers, we wish
to divide the sequence into at most p partitions as fairly as possible (you know the problem already). Assume that we are given the following sequence (5, 1, 2, 3, 4) and assume that p =2.
• Write down a general recursive formulation of the linear partition problem. Find the optimal answer by inspection for the specific problem given above.
• Draw the recursion tree similar to the one drawn in Fig. 7(attached herewith). Remember that in the present problem p=2. Highlight the edges in your recursion tree, which corresponds to the optimal solution.
Distributed by : www.myUET.net.tc
The Partitioning Problem
(5, 1, 2, 3, 4, 3)
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
(5, 1, 2, 3, 4, 3)
Partition the sequence into 4 partitions
Distributed by : www.myUET.net.tc
Partition the sequence into 4 partitions (5, 1, 2, 3, 4, 3)
Distributed by : www.myUET.net.tc
Partition the sequence into 4 partitions (5, 1, 2, 3, 4, 3)
Time Complexity?Number of VerticesNumber of Edges
Distributed by : www.myUET.net.tc
Partition the sequence into 4 partitions (5, 1, 2, 3, 4, 3)
Distributed by : www.myUET.net.tc
Once again the Partitioning Problem?
Distributed by : www.myUET.net.tc
The Same Problem but?
Distributed by : www.myUET.net.tc
Partition the sequence into 4 partitions (5, 1, 2, 3, 4, 3)
Distributed by : www.myUET.net.tcAdd one more level – but why?
Distributed by : www.myUET.net.tcAdd one more level – but why?
Distributed by : www.myUET.net.tc
The Rod Partitioning Problem
The size - price tableCutting costs?
Distributed by : www.myUET.net.tc
The Rod Partitioning Problems
1. No cutting cost?2. Uniform cutting
cost?3. Non uniform
cutting cost
Distributed by : www.myUET.net.tc
The Rod Partitioning Problem
The size - price tableCutting Cost = 0
Distributed by : www.myUET.net.tc
The Rod Partitioning
Problem
The size - price tableUniform Cutting Cost = x
Distributed by : www.myUET.net.tc
The Rod Partitioning
Problem
The size - price tableNon Uniform Cutting Cost
Distributed by : www.myUET.net.tc
The Rod Partitioning Problem
Cutting Cost = 0Maximum Price = 13
Distributed by : www.myUET.net.tc
The Rod Partitioning Problem
The size - price tableCutting Cost = 0
Distributed by : www.myUET.net.tc
The Rod Partitioning
Problem
Distributed by : www.myUET.net.tc
Length & Price is given – put weights?
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tcHow many vertices, edges?
Distributed by : www.myUET.net.tcDynamic Programming?
Distributed by : www.myUET.net.tc
Dynamic Programming
Distributed by : www.myUET.net.tc
Dynamic Programming
Distributed by : www.myUET.net.tc
Dynamic Programming
Total vertices and edges?
Distributed by : www.myUET.net.tc
Dynamic Programming
Distributed by : www.myUET.net.tc
Directed Acyclic Graph
Distributed by : www.myUET.net.tc
How about a fixed cutting cost?
Distributed by : www.myUET.net.tc
Maximizing profits after subtracting total cutting costs
Distributed by : www.myUET.net.tc
Maximizing profits after subtracting total cutting costs
Distributed by : www.myUET.net.tc
Maximizing profits after subtracting total cutting costs
Distributed by : www.myUET.net.tc
Maximizing profits after subtracting total cutting costs
Distributed by : www.myUET.net.tc
Profits after cutting costs
Distributed by : www.myUET.net.tc
• Total Vertices?• Total Edges?• Time
Complexity?
Distributed by : www.myUET.net.tc
An alternate way of solving it?
Distributed by : www.myUET.net.tc
An alternate way of solving it?
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
How the DAG will change? In shape? In edge weights?
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Once again the partitioning problem
• Given a fixed sequence of n positive numbers how can we partition it in k partitions such that the weight of the heaviest partition is minimum.
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
ghh
Distributed by : www.myUET.net.tc
Once again the partitioning problem?
• Given a fixed sequence of n positive numbers how can we partition it in k partitions such that the weight of the heaviest partition is minimum.
• How about if k=n?
Distributed by : www.myUET.net.tc
Once again the partitioning problem?
• Given a fixed sequence of n positive numbers how can we partition it in k partitions such that the weight of the heaviest partition is minimum.
• How about if some numbers are negative?
Distributed by : www.myUET.net.tc
Once again the partitioning problem?
• How about if some numbers are negative? • What should be the value of k so as to
minimize the weight of the heaviest partition?• The value of k could be just 1?
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
ghh
Why can not be the DAG like this or that?
Distributed by : www.myUET.net.tc
How the DAG would look like if some edge weights are negative but k is fixed from above?
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Matrix Multiplication Problem?
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc
The Longest or shortest path problem
Distributed by : www.myUET.net.tc
The Longest path problem
Distributed by : www.myUET.net.tc
Longest Increasing sequence
• What is this problem?
Distributed by : www.myUET.net.tc
Longest Increasing Sub-Sequence
• What is this problem?
Distributed by : www.myUET.net.tc
Distributed by : www.myUET.net.tc