Greedy_DP
Greedy_DP
(CSL2020)
[Greedy & DP Techniques]
Dr. Suchetana Chakraborty
Department of Computer Science and Engineering
IIT Jodhpur
Jan 2025
Greedy Algorithm
• Greedy is a strategy that works well on optimization problems with
the following characteristics:
➢make decision incrementally in small steps without backtracking
➢decision at each step is based on improving local or current state in a myopic
fashion without paying attention to the global situation
➢decisions often based on some fixed and simple priority rules
Interval Scheduling
• Interval i starts at sj and finishes at fj.
• Two intervals are compatible if they don't overlap.
• Goal: find maximum subset of mutually compatible intervals.
Interval Scheduling: Greedy Algorithm
Greedy template. Consider intervals in some natural order. Take each
interval provided, it's compatible with the ones already taken.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Scheduling to Minimize Lateness
Counterexamples
Greedy template. Consider jobs in some
order. 1 2
[Shortest processing time first] Consider jobs tj 1 10
in ascending order of processing time tj →
dj 100 10
A B C Solution:
Let W = 4 pound
$100 $10 $120 [A] 2 pounds of item A + 2pounds of item C
=> profit = $100+$80=$180
2 pound 2 pound 3 pound [B] 3 pound of item c
=> profit = $120
• Fractional Knapsack: Thief can take a fraction of an item.
• 0-1 Knapsack: Thief can only take or leave item. He can’t take a fraction.
Fractional Knapsack has a Greedy Solution
• For each object i, suppose a fraction 𝑥𝑖 , 0 ≤ 𝑥𝑖 ≤ 1 (i.e. 1 is the maximum
amount) can be placed in the knapsack, then the profit earned is 𝑣𝑖 𝑥𝑖 .
Objective is to maximize profit subject to capacity constraint.
i.e. Maximize σ𝑛𝑖=1 𝑣𝑖 𝑥𝑖 (1)
subject to σ𝑛𝑖=1 𝑤𝑖 𝑥𝑖 ≤ 𝑊 (2)
where 0 ≤ 𝑥𝑖 ≤ 1, 𝑣𝑖 > 0 𝑎𝑛𝑑 𝑤𝑖 > 0 (3)
• A feasible solution is any sub-set {𝑥1 , … , 𝑥𝑛 } satisfying (2) and (3).
• An optimal solution is a feasible solution that maximize (1).
• Knapsack problems appear in real-world decision making processes in a
wide variety of fields, such as finding the least wasteful way to cut raw
materials, selection of investments and portfolios, resources allocation, etc.
Example
• Let n=3, M=20
• Strategy1: maximize the objective function
• (v1,v2,v3)= (25,24,15) • Strategy 2 : maximise capacity
• (w1,w2,w3)=(18,15,10) • Strategy 3: balancing profit and capacity
(maximum profit per unit of capacity)
Feasible solutions
𝑛 𝑛
(x1,x2,x3)
𝑤𝑖 𝑥𝑖 𝑣𝑖 𝑥𝑖
𝑖=1 𝑖=1
1 (1, 2/15, 0) 20 28.2
2 (0, 2/3, 1) 20 31
3 (0, 1, 1/2 ) 20 31.5
Fractional Knapsack Greedy Algorithm
Input : the objects are in increasing order so that v[i]/w[i] > v[i + 1]/w[i + 1];
Output : optimal solution vector x
Analysis:
Greedy-fractional-knapsack (w, v, W) If the items are already sorted into decreasing order
FOR i =1 to n of vi / wi, then the while-loop takes a time in O(n);
do x[i] =0 Therefore, the total time including the sort is in O(n
weight = 0 log n).
while weight < W
do i = best remaining item If we keep the items in heap with largest vi/wi at the
IF weight + w[i] ≤ W root. Then creating the heap takes O(n) time
then x[i] = 1 while-loop now takes O(log n) time (since heap
weight = weight + w[i] property must be restored after the removal of root)
else
x[i] = (w - weight) / w[i] Although this data structure does not alter the worst-
weight = W case, it may be faster if only a small number of items
return x are need to fill the knapsack.
Greedy Algorithm: Pros and Cons
• Pros:
A. Usually (too) easy to design greedy algorithms
B. Easy to implement and often run fast since they are simple
C. Several important cases where they are effective/optimal
D. Lead to a first-cut heuristic when problem not well understood
Cons:
A. Very often greedy algorithms don’t work. Easy to lull oneself into believing
they work
B. Many greedy algorithms possible for a problem and no structured way to
find effective ones
❖Every greedy algorithm needs a proof of correctness
More Problems
• Coin changing:
• Scheduling to minimize average completion time
• Acyclic subgraphs
• Scheduling variations
• Off-line caching
• Huffman Encoding
• Minimum spanning trees
Divide and Conquer Greedy Dynamic Programming
Divide the problems into Optimizes by making the Breaks a problem down into
independent non-overlapping choice that seems best at non-independent
subproblems the moment overlapping sub-problems
Solves the subproblems Chooses the locally optimal The sub-problems are
recursively and then Combine solution hoping that it solved only once and save
the solutions to solve the would lead to globally the values in a table in
original problem optimal solution order to avoid the costly re-
computation
Solve in a top-down approach Follows top-down approach Solve in a bottom-up
for solving; makes its first approach; solves the
choice before solving any subproblems before making
subproblems the first choice
Does more work than Generally more efficient, Generally less efficient as
necessary by repeatedly but does not guarantee requires more space, but
solving the same sub-problems optimal solution guarantee optimal solution
15-04-2025 24
Dynamic Programming
• ‘Programming’ in dynamic programming refers to scheduling or
planning: mainly to the use of tables (arrays) to construct a solution
• We typically apply dynamic programming to optimization problems
• Like Divide and conquer DP solves problem by combining the
solutions to sub-problems
• DnC algorithms divide a problem into independent sub-problems
• DP is applicable when sub-problems are not independent
• Space-time trade-off: In dynamic programming we usually reduce
time by increasing the amount of space
15-04-2025 25
Dynamic Programming
To develop dynamic-programming algorithm we follow these sequence
of steps:
15-04-2025 26
Computing the nth Fibonacci Number
Recursive approach:
• F(n) = F(n-1) + F(n-2)
• F(0) = 0
• F(1) = 1
• Top-down approach
15-04-2025 27
Fibonacci Number
• What is the Recurrence relationship?
• T(n) = T(n-1) + T(n-2) + 1
• What is the solution to this?
• Clearly it is O(2n), but this is not tight.
• A lower bound is (2n/2).
• You should notice that T(n) grows very similarly to F(n), so in fact T(n) =
(F(n)).
• Obviously not very good, but we know that there is a better way to
solve it
15-04-2025 28
Fibonacci Number: Why is the top-down so
inefficient? Fib(5)
+
• Re-computes many sub-problems.
Fib(4) Fib(3)
+ +
Fib(1) Fib(0)
15-04-2025 29
Fibonacci Numbers by Caching
Memo(r)ization: Remember Everything
15-04-2025 30
Memoization vs. Iteration
• In DP, we write the recursive formulas that express large sub-
problems in terms of smaller sub-problems
• Then use them to fill up a table of solution values in a bottom-up
manner, from smallest sub-problem to the largest
• The trick is to apply a more intelligent recursive algorithm that
remembers its previous invocations and thereby avoid wasteful
repeated computations!
• In some cases, memoization pays off: dynamic programming
automatically solves every subproblem that could conceivably be
needed, while memorization only ends up solving the ones that are
actually used.
15-04-2025 31
Fibonacci Numbers by Dynamic Programming
• Computing the nth Fibonacci number using a bottom-up approach:
• Efficiency:
• Time – O(n) 0 1 1 … F(n-1) F(n)
• Space – O(n)
15-04-2025 32
Fibonacci Numbers by Dynamic Programming
• More Elegant solution: don’t remember everything after all
• 𝐼𝑇𝐸𝑅𝐹𝐼𝐵𝑂2 𝑛
𝑝𝑟𝑒𝑣 ← 1
𝑐𝑢𝑟𝑟 ← 0
𝑓𝑜𝑟 𝑖 ← 1 𝑡𝑜 𝑛
𝑛𝑒𝑥𝑡 ← 𝑐𝑢𝑟𝑟 + 𝑝𝑟𝑒𝑣
𝑝𝑟𝑒𝑣 ← 𝑐𝑢𝑟𝑟
𝑐𝑢𝑟𝑟 ← 𝑛𝑒𝑥𝑡
𝑟𝑒𝑡𝑢𝑟𝑛 𝑐𝑢𝑟𝑟
Note: This algorithm uses the non-standard but perfectly consistent base case F−1 = 1 so that IterFibo2(0)
returns the correct value 0.
15-04-2025 33
0-1 Knapsack
• Given I={1,2,..,n} items with values {v1,v2,…,vn} and weights
{w1,w2,…,wn} and a knapsack of maximum capacity W, determine a
subset of items from I that can fit in the knapsack and maximizes the
total value of the knapsack.
• Maximize σ𝑛𝑖=1 𝑥𝑖 𝑣𝑖 subject to σ𝑛𝑖=1 𝑥𝑖 𝑤𝑖 ≤ 𝑊, 𝑤ℎ𝑒𝑟𝑒 𝑥𝑖 𝜖 0,1 . 𝑥𝑖 =
1 if item i is chosen, otherwise 𝑥𝑖 = 0
1. Optimal Substructure:
Let M[i,w] =the maximum value that a subset of items {1,2…,i} can have
such that the total weight of this subset is ≤ 𝑤
=> The goal is to compute M[n,W]
15-04-2025 34
0-1 Knapsack
• Observations: captures the optimal substructure
➢If item i is included in M[i,w], then
𝑴 𝒊, 𝒘 = 𝑴 𝒊 − 𝟏, 𝒘 − 𝒘𝒊 + 𝒗𝒊
➢If item i is not included in M[i,w], then
𝑴 𝒊, 𝒘 = 𝑴[𝒊 − 𝟏, 𝒘]
Now, do we know whether the item i is included or not?
– Definitely No!
Hence, 𝑴 𝒊, 𝒘 = 𝐦𝐚𝐱 𝑴 𝒊 − 𝟏, 𝒘 − 𝒘𝒊 + 𝒗𝒊 , 𝑴 𝒊 − 𝟏, 𝒘
--What if 𝑤𝑖 > 𝑤?
Obviously we can not include the item i in the knapsack
So, 𝑀 𝑖, 𝑤 = 𝑀[𝑖 − 1, 𝑤] if 𝑤𝑖 > 𝑤
15-04-2025 35
0-1 Knapsack
• Base cases:
When 𝑖 = 0 𝑜𝑟 𝑊 = 0, 𝑀[𝑖, 𝑤] = 0
• We can now proceed to recursive formulation
2. Recursive formulation:
0 𝑖𝑓 𝑖 = 0 𝑜𝑟 𝑊 = 0
𝑀 𝑖, 𝑤 = ൞ 𝑀 𝑖 − 1, 𝑤 𝑖𝑓 𝑤𝑖 ≥ 𝑤
max[𝑣𝑖 + 𝑀 𝑖 − 1, 𝑤 − 𝑤𝑖 , 𝑀 𝑖 − 1, 𝑤 𝑖𝑓 𝑖 > 0 𝑎𝑛𝑑 𝑤 ≥ 𝑤𝑖
15-04-2025 36
0-1 Knapsack
RM01K(w,v,n,W,M)
3. (a)Recurse and Memoize: 1. If i=0 or j=0
0-1 KNAPSACK(w, v, n, W) 2. M[i,j] 0
3. If M[i,j] < 0
1. If n=0 and W=0
4. If wi> j
2. Return 5. Return RM01K(w,v,i-1,j,M)
3. For i=0 to n 6. Else
4. For j=0 to W 7. Return
5. M[i,j]=-1 𝑅𝑀01𝐾 𝑤, 𝑣, 𝑖 − 1, 𝑗 − 𝑤𝑖 , 𝑀 + 𝑣𝑖
max ቊ
6. Return RM01K(w,v,n,W,M) 𝑅𝑀01𝐾(𝑤, 𝑣, 𝑖 − 1, 𝑗, 𝑀)
15-04-2025 37
0-1 Knapsack
3. (b)Bottom-up Dynamic Programming:
01KNAPSACK(w,v,n,W) 9. If wi > w
1. If n=0 or W=0 10. M[i,w] M[i-1, w]
2. Return 11. OPT[i,w] N //N=not included
3. For i=0 to n 12. else
13. M[i,w] max(M[i-1,w-wi]+vi, M[i-1, w])
4. M[i,0] 0
14. If M[i,w] =M[i-1,w]
5. For j=0 to W 15. OPT[i] N //N=Not included
6. M[0,w] 0 16. else
7.For i=1 to n 17. OPT[i] I //I =included
8. For j=1 to W 18. Return M and OPT
15-04-2025 38
0-1 Knapsack
4. Construct the Optimal Solution
➔Print the content of the knapsack
PRINT_KNAPSACK(i,w,OPT)
1. If i>0 and w>0
2. If OPT[i,w] = I
3. PRINT_KNAPSACK(i-1,w-wi,OPT)
4. Print i
5. Else PRINT_KNAPSACK(i-1,w,OPT)
To print the content of the knapsack invoke PRINT_KNAPSACK(n,W,OPT)
15-04-2025 39
0-1 Knapsack W+1
0 1 2 3 4 5 6 7 8 9 10 11
𝜑 0 0 0 0 0 0 0 0 0 0 0 0
{1} 0 1 1 1 1 1 1 1 1 1 1 1
n+1
{1, 2} 0 1 6 7 7 7 7 7 7 7 7 7
{1, 2, 3} 0 1 6 7 7 18 19 24 25 25 25 25
{1, 2, 3, 4} 0 1 6 7 7 18 22 24 28 29 29 40
{1, 2, 3, 4, 5} 0 1 6 7 7 18 22 28 29 34 35 40
15-04-2025 41
Weighted Interval Scheduling
• Job j starts at sj, finishes at fj,
and has weight or value vj
• Two jobs compatible if they
don't overlap.
• Goal: find maximum weight
subset of mutually
compatible jobs
15-04-2025 42
Unweighted Interval Scheduling Review
• Recall. Greedy algorithm works if all weights are 1.
• Consider jobs in ascending order of finish time.
• Add job to subset if it is compatible with previously chosen jobs.
• Observation. Greedy algorithm can fail spectacularly if arbitrary weights are allowed.
15-04-2025 43
Weighted Interval Scheduling
• Notation. Label jobs by finishing time: 𝑓1 ≤ 𝑓2 ≤ . . . ≤ 𝑓𝑛 .
• Def. p(j) = largest index i < j such that job i is compatible with j.
j 0 1 2 3 4 5 6 7 8
P(j) - 0 0 0 1 0 2 3 5
15-04-2025 44
Dynamic Programming: Binary Choice
• Notation. OPT(j) = value of optimal solution to the problem consisting of
job requests 1, 2, ..., j.
❑Case 1: Optimum selects job j.
– can't use incompatible jobs { 𝑝(𝑗) + 1, 𝑝(𝑗) + 2, … , 𝑗 − 1 }
– must include optimal solution to problem consisting of
remaining compatible jobs 1, 2, ..., p(j)
❑Case 2: Optimum does not select job j.
– must include optimal solution to problem consisting of
remaining compatible jobs 1, 2, ..., j-1
𝟎 𝒊𝒇 𝒋 = 𝟎
𝑶𝑷𝑻 𝒋 = ൝
𝐦𝐚𝐱 𝒗𝒋 + 𝑶𝑷𝑻 𝒑 𝒋 , 𝑶𝑷𝑻 𝒋 − 𝟏 𝒐𝒕𝒉𝒆𝒓𝒘𝒊𝒔𝒆
15-04-2025 45
Weighted Interval Scheduling: Brute Force
Input: 𝑛, 𝑠1, … , 𝑠𝑛 , 𝑓1 , … , 𝑓𝑛 , 𝑣1 , … , 𝑣𝑛
Sort jobs by finish times so that 𝑓1 ≤ 𝑓2 ≤ … ≤ 𝑓𝑛.
Compute 𝑝(1), 𝑝(2), … , 𝑝(𝑛)
ComputeOPT(j)
{
if (j = 0)
return 0
else
return max(𝑣𝑗 + 𝐶𝑜𝑚𝑝𝑢𝑡𝑒𝑂𝑃𝑇(𝑝(𝑗)), 𝐶𝑜𝑚𝑝𝑢𝑡𝑒𝑂𝑃𝑇(𝑗 − 1))
}
15-04-2025 46
Weighted Interval Scheduling: Brute Force
Observation. Recursive algorithm fails spectacularly because of redundant sub-
problems ⇒ exponential algorithms.
Ex. Number of recursive calls for family of "layered" instances grows like Fibonacci
sequence.
15-04-2025 47
Weighted Interval Scheduling: Bottom-Up
Dynamic Programming
Input: 𝑛, 𝑠1 , … , 𝑠𝑛 , 𝑓1 , … , 𝑓𝑛 , 𝑣1 , … , 𝑣𝑛
Sort jobs by finish times so that 𝑓1 ≤ 𝑓2 ≤ … ≤ 𝑓𝑛.
Compute 𝑝(1), 𝑝(2), … , 𝑝(𝑛)
IterComputeOPT{
OPT[0] = 0
for j = 1 to n
𝑶𝑷𝑻[𝒋] = 𝒎𝒂𝒙(𝒗𝒋 + 𝑶𝑷𝑻[𝒑(𝒋)], 𝑶𝑷𝑻[𝒋 − 𝟏])
}
Output OPT[n]
Claim: OPT[j] is value of optimal solution for jobs 1..j
Timing: Easy. Main loop is 𝑂(𝑛); sorting is 𝑂(𝑛 log 𝑛)
15-04-2025 48
Weighted Interval Scheduling
Q. Dynamic programming algorithms computes optimal value. What if we want the
solution itself?
A. Do some post-processing – “traceback”
Find-Solution(j) {
if (j = 0)
output nothing
else if (𝒗𝒋 + 𝑶𝑷𝑻[𝒑(𝒋)] > 𝑶𝑷𝑻[𝒋 − 𝟏])
print j
Find-Solution(p(j))
else
Find-Solution(j-1)
}
➔ Total number of recursive calls ≤ 𝑛 ⇒ 𝑂(𝑛)
15-04-2025 49
Weighted Interval Scheduling
10 1
20 2
5 3
20 4
15 5
1 2 3 4 5
𝒑(𝒋) 0 0 1 1 3
𝒗𝒋 + 𝑶𝑷𝑻 𝒑 𝒋 10 20 15 30 35
𝑶𝑷𝑻 𝒋 − 𝟏 0 10 20 20 30
15-04-2025 50
Matrix Chain Multiplication
• Matrix multiplication is not commutative (in general, 𝐴 × 𝐵 ≠ 𝐵 × 𝐴), but
it is associative, which means for instance that 𝐴 × (𝐵 × 𝐶) = (𝐴 × 𝐵) ×
𝐶.
• Suppose that we want to multiply four matrices, A, B, C, D, of dimensions
50X20, 20X1,1X10, and 10X100, respectively
• Thus we can compute our product of four matrices in many different ways,
depending on how we parenthesize it.
• Multiplying an 𝑚 × 𝑛 matrix by an 𝑛 × 𝑝 matrix takes 𝑚𝑛𝑝 multiplications
Parenthesization Cost computation Cost
𝐴 × ((𝐵 × 𝐶) × 𝐷) 20.1.10 + 20.10.100 + 50.20.100 120,200
(𝐴 × 𝐵 × 𝐶 ) × 𝐷 20.1.10 + 50.20.10 + 50.10.100 60,200
(𝐴 × 𝐵)(𝐶 × 𝐷) 50.20.1 + 1.10.100 + 50.1.100 7000
15-04-2025 51
Matrix Chain Multiplication
• The matrix-chain multiplication problem: given a chain <
𝐴1 , 𝐴2 , … , 𝐴𝑛 > of n matrices, where for 𝑖 = 1,2, … , 𝑛, matrix 𝐴𝑖 has
dimension 𝑝𝑖−1 × 𝑝𝑖 , fully parenthesize the product 𝐴1 𝐴2 … 𝐴𝑛 in a
way that minimizes the number of scalar multiplications.
• Observation: A particular parenthisazation can be represented by a
binary tree in which the individual matrices correspond to the leaves,
the root is the final product, and interior nodes are intermediate
products.
• The possible orders in which to do the multiplication correspond to
the various full binary trees with n leaves, whose number is
exponential in n→ brute-force can not be used!
15-04-2025 52
Matrix Chain Multiplication
15-04-2025 55
Matrix Chain Multiplication
𝑨𝟏 × 𝑨𝟐 × 𝑨𝟑 × 𝑨𝟒 × 𝑨𝟓
Running time analysis: 4X10 10X3 3X12 12X20 20X7
• This just takes O(1) work for any
i\j 1 2 3 4 5
given k, and there are at most n
different values k to consider, so 1 0 120 264 1080 1344
overall we just spend O(n) time
per subproblem. 2 x 0 360 1320 1350
• So, if we use DP to save our 3 x x 0 720 1140
results in a lookup table, then
since there are only O(n2) 4 x x x 0 1680
subproblems we will spend only
O(n3) time overall. 5 x x x x 0
15-04-2025 56
Matrix Chain Multiplication
15-04-2025 57
Matrix Chain Multiplication
15-04-2025 58
Matrix Chain Multiplication
15-04-2025 59
Elements of Dynamic Programming
• Optimal substructure: problem exhibits optimal substructure if an
optimal solution to the problem contains within it optimal solutions
to subproblems
• Common pattern in discovering optimal substructure:
1. You show that a solution to the problem consists of making a choice
2. You suppose that for a given problem, you are given the choice that leads
to an optimal solution
3. Given this choice, you determine which subproblems ensue and how to
best characterize the resulting space of subproblems.
4. You show that the solutions to the subproblems used within an optimal
solution to the problem must themselves be optimal by using a “cut-and-
paste” technique.
Edit Distance
Figur e 6.4 (a) The tabl e of subpr obl ems. Entr ies E (i − 1, j − 1), E (i − 1, j ), and E (i , j − 1) ar e
needed to fi l l in E (i , j ). (b) The fi nal tabl e of val ues found by dynamic pr ogr ammi ng.
(a) (b)
j − 1 j n P O L Y N O M I A L
0 1 2 3 4 5 6 7 8 9 10
E 1 1 2 3 4 5 6 7 8 9 10
X 2 2 2 3 4 5 6 7 8 9 10
P 3 2 3 3 4 5 6 7 8 9 10
i− 1 O 4 3 2 3 4 5 5 6 7 8 9
N 5 4 3 3 4 4 5 6 7 8 9
i
E 6 5 4 4 4 5 5 6 7 8 9
N 7 6 5 5 5 4 5 6 7 8 9
T 8 7 6 6 6 5 5 6 7 8 9
I 9 8 7 7 7 6 6 6 6 7 8
GOA L
A 10 9 8 8 8 7 7 7 7 6 7
m
L 11 10 9 8 9 8 8 8 8 7 6
for i = 0, 1, 2, . . . , m:
E (i , 0) = i
for j = 1, 2, . . . , n:
E (0, j ) = j
for i = 1, 2, . . . , m:
for j = 1, 2, . . . , n:
E (i , j ) = min{ E (i − 1, j ) + 1, E (i , j − 1) + 1, E (i − 1, j − 1) + diff(i , j )}
return E (m, n)
This pr ocedur e fi lls in t he t able r ow by r ow, and left t o r ight wi t hi n each r ow. Each ent r y t ak
The running time is just the size of the table , O(mn)
const ant t ime t o fi l l in, so t he over al l r unning t ime is just t he si ze of t he t able, O(mn).
P O L Y N O M I A L
0 𝑖𝑓 𝑖 = 0 𝑎𝑛𝑑 𝑗 = 0
𝑐 𝑖, 𝑗 = ൞𝑐 𝑖 − 1, 𝑗 − 1 + 1 𝑖𝑓 𝑖, 𝑗 > 0 𝑎𝑛𝑑 𝑥𝑖 = 𝑦𝑗
max 𝑐 𝑖, 𝑗 − 1 , 𝑐 𝑖 − 1, 𝑗 𝑖𝑓 𝑖, 𝑗 > 0 𝑎𝑛𝑑 𝑥𝑖 ≠ 𝑦𝑗