0% found this document useful (0 votes)
3 views

Greedy_DP

The document discusses Greedy and Dynamic Programming techniques in algorithms, particularly focusing on interval scheduling, interval partitioning, and the knapsack problem. It outlines the characteristics of greedy algorithms, their applications, and the conditions under which they yield optimal solutions. Additionally, it contrasts greedy algorithms with dynamic programming, highlighting their respective approaches to problem-solving and efficiency.
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)
3 views

Greedy_DP

The document discusses Greedy and Dynamic Programming techniques in algorithms, particularly focusing on interval scheduling, interval partitioning, and the knapsack problem. It outlines the characteristics of greedy algorithms, their applications, and the conditions under which they yield optimal solutions. Additionally, it contrasts greedy algorithms with dynamic programming, highlighting their respective approaches to problem-solving and efficiency.
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/ 81

Data Structure and Algorithms

(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.

• [Earliest start time] Consider intervals in ascending order of sj.

• [Earliest finish time] Consider intervals in ascending order of fj

• [Shortest interval] Consider intervals in ascending order of fj - sj.

• [Fewest conflicts] For each interval j, count the number of conflicting


intervals cj . Schedule in ascending order of cj
Interval Scheduling: Greedy Algorithm
Earliest start time does
not give optimal solution

Shortest interval does


not give optimal solution

Fewest conflicts does


not give optimal solution
Interval Scheduling
• Greedy algorithm. Consider jobs in increasing order of finish time.
Take each interval provided it's compatible with the ones already
taken.
1. Sort the intervals in I by increasing finish time
2. Initialize A = ∅
3. Iterate through the intervals in I
4. If the current interval does not conflict with any interval in A,
add it to A
5. return A as the maximum set of scheduled intervals
Sample run of Interval scheduling Algorithm
Interval Partitioning Problem
• Many identical resources available
• Objective: to schedule all the intervals using as few resources as possible
• Example: each interval corresponds to a lecture and resource to classroom
• Lecture j starts at sj and finishes at fj.
• Goal: find minimum number of classrooms to schedule all lectures so that no two occur at the
same time in the same room.

(a) 4 classrooms to schedule 10 lectures (b) Uses 3 classrooms


Interval Partitioning Problem
• A solution using k resources as a rearrangement of the intervals into k rows of non-overlapping
intervals
• Definition: The depth of a set of open intervals is the maximum number that contain any given
time.
• Theorem: In any instance of Interval Partitioning the number of resources needed is at least
the depth of the set of intervals

(a) 4 classrooms to schedule 10 lectures (b) Uses 3 classrooms


Interval Partitioning: The Greedy Algorithm
Let d be the depth of the set of intervals
We assign label {1,2…,d} to each interval such that the overlapping intervals are labeled with different numbers.
----------------------------------------------------------------------------------------------------------------------------------------------------
Sort the intervals by their start time, breaking ties arbitrarily
Let I1, I2, …In denote the intervals in this order
For j=1 to n
For each interval Ii that precedes Ij in sorted order and overlaps it
Exclude the label of Ii from consideration for Ij
Endfor
If there is any label from [1,2..,d] that has not been excluded then
assign a non-excluded label to Ij
Else
Leave Ij unlabeled
Endif
Endfor
Scheduling to Minimize Lateness
• Single resource processes one job at a time.
• Job j requires tj units of processing time and is due at time dj.
• If j starts at time sj, it finishes at time fj = sj + tj.
• Lateness: lj = max { 0, fj - dj }.
• Goal: schedule all intervals to minimize the maximum lateness L = max lj.
1 2 3 4 5 6
tj 3 2 1 4 3 2 lateness=0 max lateness=6
lateness=2
dj 6 8 9 9 14 15

d3=9 d2=8 d6=15 d1=6 d5=14 d4=9

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

[Earliest deadline first] Consider jobs in


ascending order of deadline dj .
1 2
[Smallest slack] Consider jobs in ascending tj 1 10
order of slack dj - tj →
dj 2 10
Minimizing Lateness: Greedy Algorithm (EDF)
Let s be the start time for all jobs and f be the finishing time of the last
scheduled job.

Order the jobs with respect to deadlines


Assume for simplicity of notation that 𝑑1 ≤ ⋯ ≤ 𝑑𝑛
Initially, 𝑓 = 𝑠
Consider the jobs 𝑖 = 1,2, … , 𝑛 in this order
Assign job i to the time interval from 𝑠(𝑖) = 𝑓 𝑡𝑜 𝑓(𝑖) = 𝑓 + 𝑡𝑖
Let 𝑓 = 𝑓 + 𝑡𝑖
End
Return the set of scheduled intervals [𝑠(𝑖), 𝑓(𝑖)] 𝑓𝑜𝑟 𝑖 = 1,2, … , 𝑛
Elements of Greedy Strategy
• A greedy algorithm obtains an optimal solution to a problem by
making a sequence of choices.
• At each decision point, the algorithm makes choice that seems best at
the moment.
• NOT always produce an optimal solution
• Two ingredients that are exhibited by most problems that lend
themselves to a greedy strategy
• Greedy-choice property
• Optimal substructure
Greedy Algorithm Design
1. Cast the optimization problem as one in which we make a choice
and are left with one subproblem to solve.
2. Prove that there is always an optimal solution to the original
problem that makes the greedy choice, so that the greedy choice is
always safe.
3. Demonstrate optimal substructure by showing that, having made
the greedy choice, what remains is a subproblem with the property
that if we combine an optimal solution to the subproblem with the
greedy choice we have made, we arrive at an optimal solution to
the original problem.
Greedy Choice Property
• A globally optimal solution can be arrived at by making a locally
optimal (greedy) choice
• Make whatever choice seems best at the moment and then solve the sub-
problem arising after the choice is made
• The choice made by a greedy algorithm may depend on choices so far, but it
cannot depend on any future choices or on the solutions to sub-problems
• Of course, we must prove that a greedy choice at each step yields a
globally optimal solution
Optimal Substructure
• A problem exhibits optimal substructure if an optimal solution to the
problem contains within it optimal solutions to sub-problems
• Assume we arrived at a subproblem by having made the greedy choice in the
original problem.
• All we really need to do is argue that an optimal solution to the subproblem,
combined with the greedy choice already made, yields an optimal solution to
the original problem.
The Knapsack Problem
• There are n items in a store. For i =1,2, . . . , n, item i has weight wi > 0 and worth vi > 0.
• Thief can carry a maximum weight of W pounds in a knapsack.
• Which of the items should he steal to maximize value(profit)?

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:

1. Characterize the structure of an optimal solution.


2. Recursively define the value of an optimal solution.
3. Compute the value of an optimal solution, typically in a bottom-up
fashion.
4. Construct an optimal solution from computed information.

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(3) Fib(2) Fib(2) Fib(1)


+ + +

Fib(2) Fib(1) Fib(1) Fib(0) Fib(1) Fib(0)


+

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

Item Value Weight


OPT:={4,3} W=11 1 1 1
value=22+18=40 2 6 2
if 𝑤𝑖 > 𝑤
M[i,w]=M[i-1,w] 3 18 5
else 4 22 6
𝑀[𝑖, 𝑤] = 𝑚𝑎𝑥(𝑀 𝑖 − 1, 𝑤 − 𝑤𝑖 + 𝑣𝑖 , 𝑀 𝑖 − 1, 𝑤 ) 5 28 7
15-04-2025 40
0-1 Knapsack: Complexity Analysis
Time:
• Size of the table T be filled is (n+1)(W+1)= O(nW)
• Each entry takes Θ 1 time to compute
• Time=O(nW)
Space:
• Size of the table + constant amount of extra space =O(nW)
PRINT_KNAPSACK:
• Time: O(n) since each recursive call decrements n by 1
Variation
• Multiple copies of an item is available i.e. REPITITION allowed
• Subset-sum problem
• Partition problem

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

➢ For a tree to be optimal, its subtrees must also be optimal


2𝑛 !
➢ There are number of possible binary trees with n nodes
𝑛! 𝑛+1 !
15-04-2025 53
Matrix Chain Multiplication
1. Optimal Substructure:
Each subproblem looks like “what is the best way to multiply some sub-
interval of the matrices 𝐴𝑖 × 𝐴𝑖+1 × ⋯ × 𝐴𝑗 ?” So, there are only O(n2)
different subproblems.
Let, C(i, j) = minimum cost of multiplying 𝐴𝑖 × 𝐴𝑖+1 × ⋯ × 𝐴𝑗 .
The size of this subproblem is the number of matrix multiplications, |𝑗 − 𝑖|.
The smallest subproblem is when i = j, in which case there's nothing to
multiply, so C(i, i) = 0.
For j > i, consider the optimal subtree for C(i, j). The first branch in this
subtree, the one at the top, will split the product in two pieces, of the form
𝐴𝑖 , … , 𝐴𝑘 and 𝐴𝑘+1 , … , 𝐴𝑗 , for some k between i and j.
15-04-2025 54
Matrix Chain Multiplication
2. A recursive solution:
The cost of the subtree is then the cost of these two partial products,
plus the cost of combining them:
𝐶 𝑖, 𝑘 + 𝐶 𝑘 + 1, 𝑗 + 𝑚𝑖−1 . 𝑚𝑗 . 𝑚𝑘
And we just need to find the splitting point k for which this is smallest:
𝐶(𝑖, 𝑗) = min {𝐶(𝑖, 𝑘) + 𝐶(𝑘 + 1, 𝑗) + 𝑚𝑖−1 . 𝑚𝑘 . 𝑚𝑗 }.
𝑖≤𝑘<𝑗
Using bottom-up Dynamic Programming, first solve for all sub-
problems with j − i = 1, then solve for all with j − i = 2, and so on,
storing your results in an n by n matrix.

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.

4/15/2025 CS 201, IIITG, Monsoon 2019 60


Elements of Dynamic Programming
• Optimal substructure varies across problem domains in two ways:
1. how many subproblems an optimal solution to the original problem uses, and
2. how many choices we have in determining which subproblem(s) to use in an
optimal solution.
• Informally, the running time of a dynamic-programming algorithm depends
on the product of two factors:
• the number of subproblems overall and
• how many choices we look at for each subproblem.
• Sub-problem graph: each vertex corresponds to a subproblem, and the
choices for a subproblem are the edges incident to that subproblem.

4/15/2025 CS 201, IIITG, Monsoon 2019 61


Elements of Dynamic Programming
• Both dynamic programming and greedy algorithm exploit the optimal
substructure of the given problem

• DP first finds the optimal solutions to subproblems and then makes


an informed choice; where as greedy algorithms first make a “greedy”
choice—the choice that looks best at the time—and then solve a
resulting subproblem, without bothering to solve all possible related
smaller subproblems

4/15/2025 CS 201, IIITG, Monsoon 2019 62


When optimal substructure does not apply
• There are problems for which optimal substructure does not apply.
• Example: the problem of (unweighted longest simple path) finding a
simple path from u to v consisting of the most edges.
• No efficient dynamic-programming algorithm for this problem has
ever been found. In fact, this problem is NP-complete
• Although a solution to a problem for both longest and shortest paths
uses two subproblems, the subproblems in finding the longest simple
path are not independent, whereas for shortest paths they are.
• In matrix-chain multiplication problem, subchains are disjoint, so that
no matrix could possibly be included in both of them.

4/15/2025 CS 201, IIITG, Monsoon 2019 63


Elements of Dynamic Programming
Overlapping subproblems:
• When a recursive algorithm revisits the same problem repeatedly, we say
that the optimization problem has overlapping subproblems
• For DP to apply, the space of subproblems must be “small” in the sense
that a recursive algorithm for the problem solves the same subproblems
over and over, rather than always generating new subproblems
• Typically, the total number of distinct subproblems is a polynomial in the
input size.
• In contrast, a problem for which a divide-and conquer approach is suitable
usually generates brand-new problems at each step of the recursion.

4/15/2025 CS 201, IIITG, Monsoon 2019 64


Understanding the sub-problems
• The input is 𝑥1 , 𝑥2 , … , 𝑥𝑛 and a subproblem is 𝑥1 , 𝑥2 , … , 𝑥𝑖
➢The number of sub-problems is therefore linear
• The input is 𝑥1 , 𝑥2 , … , 𝑥𝑛 𝑎𝑛𝑑 𝑦1 , 𝑦2 , … , 𝑦𝑚 and a subproblem is
𝑥1 , 𝑥2 , … , 𝑥𝑖 𝑎𝑛𝑑 𝑦1 , 𝑦2 , … , 𝑦𝑗
➢The number of sub-problems is 𝑂(𝑚𝑛)
• The input is 𝑥1 , 𝑥2 , … , 𝑥𝑛 and a subproblem is 𝑥𝑖 , 𝑥𝑖+1 , … , 𝑥𝑗
➢The number of sub-problems is 𝑂 𝑛2
• The input is a rooted tree. A subproblem is a rooted subtree.
➢If the tree has n nodes, what is the total number of sub-problems?

4/15/2025 CS 201, IIITG, Monsoon 2019 65


Understanding overlapping sub-problems

4/15/2025 CS 201, IIITG, Monsoon 2019 66


Understanding overlapping sub-problems
𝑇 1 ≥ 1, 𝑇 𝑛 ≥ 1 + σ𝑛−1 𝑘=1 𝑇 𝑘 + 𝑇 𝑛 − 𝑘 + 1 𝑓𝑜𝑟 𝑛 > 1
Note that for i = 1,2,…,n; each term T(i) appears once as T(k) and once as T(n-k)

4/15/2025 CS 201, IIITG, Monsoon 2019 67


Memoization

4/15/2025 CS 201, IIITG, Monsoon 2019 68


Elements of Dynamic Programming: Summary
• If the given problem exhibits optimal substructure, we can use either top-down
memorized algorithm or bottom-up DP approach to solve.
• Both methods take advantage of the overlapping-subproblems property and cuts
down the exponential cost of repeated computation by natural recursive
algorithm.
• In general practice, if all subproblems must be solved at least once, a bottom-up
dp algorithm usually outperforms the corresponding top-down memoized
algorithm by a constant factor, because the bottom-up algorithm has no
overhead for recursion and less overhead for maintaining the table
• Moreover, for some problems we can exploit the regular pattern of table accesses
in the dp algorithm to reduce time or space requirements even further.
• Alternatively, if some subproblems in the subproblem space need not be solved
at all, the memoized solution has the advantage of solving only those
subproblems that are definitely required

4/15/2025 CS 201, IIITG, Monsoon 2019 69


Edit Distance
• A natural measure of the distance between two strings is the extent to
which they can be aligned, or matched up.
• Technically, an alignment is simply a way of writing the strings one above
the other.
• The cost of an alignment is the number of columns in which the letters
differ.
• And the edit distance between two strings is the cost of their best possible
alignment.

4/15/2025 CS 201, IIITG, Monsoon 2019 70


Edit Distance
• The minimum number of edits—insertions, deletions, and
substitutions of characters—needed to transform the first string into
the second.
• Goal: to find minimum edit distance between two strings
𝑥[1 … 𝑚] 𝑎𝑛𝑑 𝑦[1 … 𝑛]
• Sub-problems: consider some prefix of the first string, 𝑥[1 · · · 𝑖], and
some prefix of the second, 𝑦[1 · · · 𝑗], and call it sub-problem 𝐸(𝑖, 𝑗).
The goal is to find 𝐸(𝑚, 𝑛)
• First we will try to express 𝐸 𝑖, 𝑗 in terms of smaller sub-problems.

4/15/2025 CS 201, IIITG, Monsoon 2019 71


Edit Distance
• Consider the right-most column of 𝑥 1 · · · 𝑖 and 𝑦[1 · · · 𝑗]. Only three
possibilities are
Case-I Case-II Case-III
x[i] - x[i]
- y[j] y[j]
• Case-I: incurs a cost of 1, and it remains to align 𝑥 1 · · · 𝑖 − 1 𝑤𝑖𝑡ℎ 𝑦 1 · · · 𝑗 →
𝑬 𝒊 − 𝟏, 𝒋
• Case-II: incurs a cost of 1, and it remains to align 𝑥 1 · · · 𝑖 𝑤𝑖𝑡ℎ 𝑦 1 · · · 𝑗 − 1 →
𝑬 𝒊, 𝒋 − 𝟏
• Case-III: incurs a cost either 1 (if 𝑥[𝑖] ≠ 𝑦[𝑗]) or 0 (if 𝑥[𝑖] = 𝑦[𝑗]) →
𝑬 𝒊 − 𝟏, 𝒋 − 𝟏
𝑬(𝒊, 𝒋) = 𝒎𝒊𝒏{𝟏 + 𝑬(𝒊 − 𝟏, 𝒋), 𝟏 + 𝑬(𝒊, 𝒋 − 𝟏), 𝒅𝒊𝒇𝒇(𝒊, 𝒋) + 𝑬(𝒊 − 𝟏, 𝒋 − 𝟏)}
where for convenience diff(i, j) is defined to be 0 if x[i] = y[j] and 1 otherwise.

4/15/2025 CS 201, IIITG, Monsoon 2019 72


176 Algor it hms

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

4/15/2025 CS 201, IIITG, Monsoon 2019 73


r emain t he “base cases” of t he dynamic pr ogramming, t he ver y smal lest subpr obl ems. I n t h
pr esent sit uation, t hese ar e E (0, ·) and E (·, 0), bot h of which ar e easily solved. E (0, j ) is t h
edit distance bet ween t he 0-lengt h pr efi x of x, namel y t he empt y st r ing, and t he fi r st j let t e
Edit Distance: DP Solution
of y: clear ly, j . And similar l y, E (i , 0) = i .
At t his point , t he algor it hm for edit di st ance basical ly wr it es it self.

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).

And in our example, t he edit dist ance t ur ns out t o be 6:


4/15/2025 CS 201, IIITG, Monsoon 2019 74
Edit Distance: The underlying DAG
• underlying dag structure: each node representing a subproblem
where as each edge indicates the precedence constraint on the order
in which the subproblems can be tackled
• Its edges are the precedence constraints, of the form (𝑖 − 1, 𝑗) →
(𝑖, 𝑗), (𝑖, 𝑗 − 1) → (𝑖, 𝑗), 𝑎𝑛𝑑 (𝑖 − 1, 𝑗 − 1) → (𝑖, 𝑗)
• Set all edge lengths to 1, except for { 𝑖 − 1, 𝑗 − 1 → 𝑖, 𝑗 : 𝑥 𝑖 =
𝑦 𝑗 }, whose length is 0
• The final answer is then simply the distance between nodes 𝑠 =
(0, 0) 𝑎𝑛𝑑 𝑡 = (𝑚, 𝑛).

4/15/2025 CS 201, IIITG, Monsoon 2019 75


S. Dasgupt a, C.H . Papadimit r iou, and U.V. Vazir ani

Edit Distance: The underlying DAG


F igur e 6.5 The under lying dag, and a pat h of lengt h 6.

P O L Y N O M I A L

• For any possible shortest path, each E


move down is a deletion, each move X
right is an insertion, and each P
diagonal move is either a match or a O
substitution. N
• a dotted line indicates an edge with E
length 0 where { 𝑖 − 1, 𝑗 − 1 → N
𝑖, 𝑗 : 𝑥 𝑖 = 𝑦 𝑗 }, T
I
A
L

4/15/2025 CS 201, IIITG, Monsoon 2019 76


Longest Common Subsequence
• Given two sequences X and Y , we say that a sequence Z is a common
subsequence of X and Y if Z is a subsequence of both X and Y . For
example, if X =<A, B, C, B, D, A, B> and Y = <B, D, C, A, B, A>, the
sequence <B,C,A> is a common subsequence of X and Y, but not a LCS.
However, both sequences <B,C,B,A> and <B,D,A,B> are LCS

4/15/2025 CS 201, IIITG, Monsoon 2019 77


Longest Common Subsequence
Optimal Substructure of LCS:
Let 𝑋 =< 𝑥1 , 𝑥2 , … , 𝑥𝑛 > and 𝑌 = 𝑦1 , 𝑦2 , … , 𝑦𝑚 > be sequences, and
let 𝑍 =< 𝑧1 , 𝑧2 , … , 𝑧𝑘 >be any LCS of X and Y .
1. If 𝑥𝑚 = 𝑦𝑛 , then 𝑧𝑘 = 𝑥𝑚 = 𝑦𝑛 and 𝑍𝑘−1 is an LCS of 𝑋𝑚−1 𝑎𝑛𝑑 𝑌𝑛−1
2. If 𝑥𝑚 ≠ 𝑦𝑛 , then 𝑧𝑘 ≠ 𝑥𝑚 implies that 𝑍 is an LCS of 𝑋𝑚−1 𝑎𝑛𝑑 𝑌
3. If 𝑥𝑚 ≠ 𝑦𝑛 , then 𝑧𝑘 ≠ 𝑦𝑛 implies that 𝑍 is an LCS of 𝑋 𝑎𝑛𝑑 𝑌𝑛−1

4/15/2025 CS 201, IIITG, Monsoon 2019 78


LCS: a recursive solution
• There are 2m subsequences of X. Testing a subsequence (length k)
takes time O(k + n). →So brute force algorithm is O(n.2 m).

0 𝑖𝑓 𝑖 = 0 𝑎𝑛𝑑 𝑗 = 0
𝑐 𝑖, 𝑗 = ൞𝑐 𝑖 − 1, 𝑗 − 1 + 1 𝑖𝑓 𝑖, 𝑗 > 0 𝑎𝑛𝑑 𝑥𝑖 = 𝑦𝑗
max 𝑐 𝑖, 𝑗 − 1 , 𝑐 𝑖 − 1, 𝑗 𝑖𝑓 𝑖, 𝑗 > 0 𝑎𝑛𝑑 𝑥𝑖 ≠ 𝑦𝑗

4/15/2025 CS 201, IIITG, Monsoon 2019 79


LCS

4/15/2025 CS 201, IIITG, Monsoon 2019 80


LCS: Constructing an LCS

4/15/2025 CS 201, IIITG, Monsoon 2019 81

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy