Dynamic programming

Nov 22, 2023

When going over the lab7b I wanted to understan more clearly how to identity patterns and similarties to other problems and when to use dynamic programming.

A subsequence of a sequence like a and array, a linked list, or a string, is obtained by removing zero or more elements and keeping the rest in the same sequence order. It is also called a substring if the elements are contiguous in the original sequence.

Working on the examples:

  1. Given an array A[1...n] of integers, compute the length of a longest increasing subsequence of A. A sequence B[1...] is increasing if B[i]>B[i1] for every index i2.

LIS(i,j)={0if j>nLIS(i,j+1)if jn and A[i]A[j]max{LIS(i,j+1),1+LIS(j,j+1)} otherwise

Algorithm 1 Longest Increasing Subsequence

procedure LIS(A[1n])

A[0]

for i0 to n do

LIS[i,n+1]0

end for

for jn down to 1 do

for ij1 down to 0 do

if A[i]A[j] then

LIS[i,j] LIS[i,j+1]

else

LIS[i,j]max{LIS[i,j+1],1+LIS[j,j+1]}

end if

end for

end for

return LIS[0,1]

end procedure