Download Dynamic Programming II: Algorithms and Applications and more Lecture notes Algorithms and Programming in PDF only on Docsity!
Lecture 8:
Dynamic Programming II
– Stable Matching
– Greedy algorithms
– Divide & Conquer algorithms
– Dynamic programming algorithms
– Network flow algorithms
General techniques in this course
Key steps: Dynamic programming
1. Define subproblems
2. Find recurrences
3. Solve the base cases
4. Transform recurrence into an efficient algorithm
Recap: Weighted Interval Scheduling
, finishes at f
j
, and has value v
j
- Two jobs compatible if they don't overlap.
- Goal: find maximum value subset of mutually compatible jobs. Time 0 1 2 3 4 5 6 7 8 9 10 11 f g h e a b c d
Recap: Dynamic Programming – Step 1
Step 1: Define subproblems
OPT(j) = value of optimal solution to the problem
consisting of job requests 1, 2, ..., j.
Recap: Dynamic Programming – Step 2
Step 2: Find recurrences
- Case 1: OPT selects job j.
- can't use incompatible jobs { p(j) + 1, p(j) + 2, ..., j - 1 }
- must include optimal solution to problem consisting of
remaining compatible jobs 1, 2, ..., p(j)
- Case 2: OPT does not select job j.
- must include optimal solution to problem consisting of
remaining compatible jobs 1, 2, ..., j-
OPT(j) = v
j
max { + OPT (p(j)), OPT(j-1)}
Case 1 Case 2
Recap: Knapsack Problem
- Knapsack problem.
- Given n objects and a "knapsack."
- Item i weighs wi > 0 kilograms and has value v (^) i > 0.
- Knapsack has capacity of W kilograms.
- Goal: fill knapsack so as to maximize total value. 1 Value 18 22 28 1 Weight 5 6 6 2 7 Item 1 3 4 5 2 W = 11
Knapsack Algorithm Recap
n + 1 1 Value 18 22 28 1 Weight 5 6 6 2 7 Item 1 3 4 5 2 ∅ { 1, 2 } { 1, 2, 3 } { 1, 2, 3, 4 } { 1 } { 1, 2, 3, 4, 5 } 0 0 0 0 0 0 0 1 0 1 1 1 1 1 2 0 6 6 6 1 6 3 0 7 7 7 1 7 4 0 7 7 7 1 7 5 0 7 18 18 1 18 6 0 7 19 22 1 22 7 0 7 24 24 1 28 8 0 7 25 28 1 29 9 0 7 25 29 1 34 10 0 7 25 29 1 35 11 0 7 25 40 1 40 W + 1 W = 11 OPT: { 4, 3 } value = 22 + 18 = 40
Recap: Dynamic Programming – Step 2
Step 2: Find recurrences
- Case 1: OPT does not select item i.
- OPT selects best of { 1, 2, …, i-1 } using weight limit w
- Case 2: OPT selects item i.
- new weight limit = w – wi
- OPT selects best of { 1, 2, …, i–1 } using this new weight limit
OPT(i,w) = max { v
i
+ OPT (i-1,w-w
i
), OPT(i-1,w) }
Recap: Dynamic Programming – Step 2
Step 2: Find recurrences
- Case 1: OPT does not select item i.
- OPT selects best of { 1, 2, …, i-1 } using weight limit w
- Case 2: OPT selects item i.
- new weight limit = w – wi
- OPT selects best of { 1, 2, …, i–1 } using this new weight limit
If w
i
>w
OPT(i,w) = OPT (i-1,w)
otherwise
OPT(i,w) = max { v
i
+ OPT (i-1,w-w
i
), OPT(i-1,w) }
Recap: Longest increasing subsequence
Given a sequence of numbers X[1..n] find the longest increasing
subsequence (i
1
, i
2
, …, i
k
), that is a subsequence where numbers in
the sequence increase.
j i
LIS 1 1 1 1 1 1 1 1
Recap: Longest increasing subsequence
Given a sequence of numbers X[1..n] find the longest increasing
subsequence (i
1
, i
2
, …, i
k
), that is a subsequence where numbers in
the sequence increase.
j i
L 1 1 2 1 1 1 1 1
If X[i] > X[j] then L[i] + 1 = 1 + 1 = 2 is this > L[i] yes L[i] = 2 move j to next position and check again
Recap: Longest increasing subsequence
Given a sequence of numbers X[1..n] find the longest increasing
subsequence (i
1
, i
2
, …, i
k
), that is a subsequence where numbers in
the sequence increase.
j i
L 1 1 2 2 1 1 1 1
If X[i] > X[j] then L[i] + 1 = 1 + 1 = 2 is this > L[i] yes L[i] = 2 move j to next position and check again
Recap: Longest increasing subsequence
Given a sequence of numbers X[1..n] find the longest increasing
subsequence (i
1
, i
2
, …, i
k
), that is a subsequence where numbers in
the sequence increase.
j i
L 1 1 2 2 2 3 1 1
If X[i] > X[j] then L[i] + 1 = 1 + 1 = 2 is this > L[i] yes L[i] = 2 move j to next position and check again