Dynamic Programming
OOTA SUBBA REDDY
IIIT-Hyderabad
2015
Dynamic Programing
Definition
A dynamic programming algorithm solves each sub problem just
one time and save the answer in a table rather than solving the
same sub problem multiple times. This kind of algorithm is popular
in optimization problems, which usually have many solutions.
When you are developing dynamic programming algorithms you
should.
Characterize the structure an optimal solution
Recursively define the value of an optimal solution.
Computer the value of an optimal solution.
Construct the solution from the computer information.
Steps 1-3 are the most important here, but step four is useful if
you need to actual data that led to your optimized solution.
Rod Cutting
Problem
Given a rod of length n inches and an array of prices that contains
prices of all pieces of size smaller than n. Determine the maximum
value obtainable by cutting up the rod and selling the pieces.
length i 1 2 3 4 5 6 7 8
price pi 1 5 8 9 10 17 17 20
Optimal Substructure (recurrence formula as follow )
rn = max1 i n(pi + rn−i ) (1)
LIS(Longest Increasing Subsequence)
Given a sequence of integers S1, ...., Sn. Find a Subsequence
Si1 < Si2 < ... < Sik
with i1 < ...ik so that k is as large as possible.
Recurrence Formula:
Let L[i]=length of longest increasing subsequence in S1, ...Sn
that ends in Si
L[j] = 1 + maxL[i] : i < jandSi < Sj
Given 9,5,2,8,7,3,1,6,4 as input,
possible increasing subsequence is 5,7
better is 2,3,6 or 2,3,4 (either of which would be a correct)
Note: Similar problems
LDS (Longest Decreasing Subsequence)
LMIS (Longest Monotonically Increasing Subsequnce)
LMDS (Longest Monotonically Decreasing Subsequence)
Longest Palindrome Subsequence
Problem
Describe an O(n2) algorithm to find the length of the longest
palindromic subsequence of a given string.
Example: a longest palindromic subsequence of
ALGORITHMSISAGREATCLASS is ALGIMIGLA , which has
length 9.
Recurrence Formula:
T[i, j] =



1 if i = j
2+M[i+1,j-1] if A[i]=A[j] and i < j
maxM[i+1,j],M[i,j-1] if A[i]!=A[j] and i < j
0 if i < j



Note: Similar problems
LCS (Longest Common Subsequence)
LCS (Convert LCS to LIS only if unique characters exists in
Strings, try O(nlogn) approach)
Palindrome Partitioning
Problem
Given a string s, partition s such that every substring of the
partition is a palindrome. Return the minimum cuts needed for a
palindrome partitioning of s.
For example, given s = ”aab”, Return 1 since the palindrome
partitioning [”aa”,”b”] could be produced using 1 cut.
D[i] equals the minimum cut number for string [i,n].
D[i] = min(D[i], D[j+1]+1) j i, and [j+1, i] forms a
palindrome.
Note: Similar problems
MM (Matrix Chain Multiplication)
Mixtures (Check in SPOJ)
Word Break
Some Other Problems Using DP
EDIST (Edit Distance)
SHPATH ( Shortest Path Using Bellam-Ford Algorithm)
ALL PAIR SHPATH ( Floyd-Warshall Algorithm)
OBST (Optimal Binary Search Tree)
TSP (Travelling Salesman Problem)
Reliability Design
Thank You.

DP

  • 1.
    Dynamic Programming OOTA SUBBAREDDY IIIT-Hyderabad 2015
  • 2.
    Dynamic Programing Definition A dynamicprogramming algorithm solves each sub problem just one time and save the answer in a table rather than solving the same sub problem multiple times. This kind of algorithm is popular in optimization problems, which usually have many solutions. When you are developing dynamic programming algorithms you should. Characterize the structure an optimal solution Recursively define the value of an optimal solution. Computer the value of an optimal solution. Construct the solution from the computer information. Steps 1-3 are the most important here, but step four is useful if you need to actual data that led to your optimized solution.
  • 3.
    Rod Cutting Problem Given arod of length n inches and an array of prices that contains prices of all pieces of size smaller than n. Determine the maximum value obtainable by cutting up the rod and selling the pieces. length i 1 2 3 4 5 6 7 8 price pi 1 5 8 9 10 17 17 20 Optimal Substructure (recurrence formula as follow ) rn = max1 i n(pi + rn−i ) (1)
  • 4.
    LIS(Longest Increasing Subsequence) Givena sequence of integers S1, ...., Sn. Find a Subsequence Si1 < Si2 < ... < Sik with i1 < ...ik so that k is as large as possible. Recurrence Formula: Let L[i]=length of longest increasing subsequence in S1, ...Sn that ends in Si L[j] = 1 + maxL[i] : i < jandSi < Sj Given 9,5,2,8,7,3,1,6,4 as input, possible increasing subsequence is 5,7 better is 2,3,6 or 2,3,4 (either of which would be a correct) Note: Similar problems LDS (Longest Decreasing Subsequence) LMIS (Longest Monotonically Increasing Subsequnce) LMDS (Longest Monotonically Decreasing Subsequence)
  • 5.
    Longest Palindrome Subsequence Problem Describean O(n2) algorithm to find the length of the longest palindromic subsequence of a given string. Example: a longest palindromic subsequence of ALGORITHMSISAGREATCLASS is ALGIMIGLA , which has length 9. Recurrence Formula: T[i, j] =    1 if i = j 2+M[i+1,j-1] if A[i]=A[j] and i < j maxM[i+1,j],M[i,j-1] if A[i]!=A[j] and i < j 0 if i < j    Note: Similar problems LCS (Longest Common Subsequence) LCS (Convert LCS to LIS only if unique characters exists in Strings, try O(nlogn) approach)
  • 6.
    Palindrome Partitioning Problem Given astring s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = ”aab”, Return 1 since the palindrome partitioning [”aa”,”b”] could be produced using 1 cut. D[i] equals the minimum cut number for string [i,n]. D[i] = min(D[i], D[j+1]+1) j i, and [j+1, i] forms a palindrome. Note: Similar problems MM (Matrix Chain Multiplication) Mixtures (Check in SPOJ) Word Break
  • 7.
    Some Other ProblemsUsing DP EDIST (Edit Distance) SHPATH ( Shortest Path Using Bellam-Ford Algorithm) ALL PAIR SHPATH ( Floyd-Warshall Algorithm) OBST (Optimal Binary Search Tree) TSP (Travelling Salesman Problem) Reliability Design
  • 8.