Download presentation
Presentation is loading. Please wait.
1
Lecture 5 Dynamic Programming
2
Recap State: Subproblems that we need to solve. (e.g. a[i, j] = maximum possible value by considering first i items for a knapsack of capacity j) Transition function: A recurrence relationship between (optimal solutions of) states (e.g. a[i, j] = max{a[i-1,j], a[i-1,j-wi]+vi} Dynamic Programming Table: An array that stores the optimal value for all states. This (and next) lecture: more examples
3
Example 2: Longest Increasing Subsequence
Input: Array of numbers a[] = {4, 2, 5, 3, 9, 7, 8, 10, 6} Subsequence: list of numbers appearing in the same order, but may not be consecutive Example: {4, 2, 5}, {4, 3, 8}, {2, 5, 7, 8, 10} Subsequence b[] is increasing if b[i+1] > b[i] for all i. Output: Length of the longest increasing subsequence. Example: 5, the subsequence is {2, 5, 7, 8, 10} or {2, 3, 7, 8, 10} (both have length 5)
4
Designing a DP algorithm for LIS: Attempt 1
Step 1: think of the problem as making a seq. of decisions For each number, decide whether it is in the LIS Step 2: Focus on last decision, enumerate the options For the last number, we either put it in, or leave it out. Step 3: Try to relate each option to a smaller subproblem Subproblem: Longest Increasing Subsequence of a Prefix leave it out: length of sequence is now smaller. put it in: length of sequence is now smaller additional constraint: numbers need to be < a[n] Can be made to work using a 2-d table
5
Designing a DP algorithm for LIS: Attempt 2
Step 1: think of the problem as making a seq. of decisions In each step, pick the next number in the LIS Step 2: Focus on last decision, enumerate the options Choose the last number in LIS Step 3: Try to relate each option to a smaller subproblem Subproblem: Longest Increasing Subsequence that ends at a[i] Transition function: to compute a[i], enumerate the number that is before a[i] in the subsequence.
6
Example 3: Longest Common Subsequence
Input: two strings a[] = ‘ababcde’ and b[] = ‘abbecd’ Subsequence: same definition as in LIS (can skip characters) E.g. ‘abac’ is a subsequence of a[], but not b[] ‘abed’ is a subsequence of b[] but not a[] Goal: Find the length of the longest common subsequence (LCS) In this example: LCS = ‘abbcd’, length = 5.
7
Designing a DP algorithm for LCS
Step 1: think of the problem as making a seq. of decisions For any pair of characters, decide whether they are matched in the Longest Common Subsequence Step 2: Focus on last decision, enumerate the options For the last characters of both sequence, they are either both in LCS, or not Step 3: Try to relate each option to a smaller subproblem Subproblem: Longest Common Subsequence of two Prefixes. both in LCS: both sequences are now shorter “not” (one of them is not in LCS): one of the sequences is now shorter
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.