0% found this document useful (0 votes)
13 views16 pages

DP Problem Algortithms

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)
13 views16 pages

DP Problem Algortithms

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/ 16

Dp problem algortithms

There are maybe one or more mistake ,please igorne it

Wriiten by binoy and prince

Without no.36 problem


Number 4. Longest Palindrome subsequence
1. Define a function lps(str, i, j) to find the longest palindromic subsequence in the substring str[i...j].

2. If i > j, return 0.

3. If i == j, return 1.

4. If str[i] == str[j], Return 2 added to the result of lps(str, i + 1, j - 1).

5. If str[i] != str[j],

- Exclude character at position i: Return the result of lps(str, i + 1, j).

- Exclude character at position j: Return the result of lps(str, i, j - 1).

Return the maximum of these two options.

6. Print "LPS : " followed by the result of lps(str, 0, str.length() - 1).

7. End of the algorithm.

The worst-case time complexity of the above solution is exponential O(2n)


07 Shortest Common Supersequence Problem

1. Define a function scs(s1,s2, i, j) to find the shortest common supersequence in the substring str[1...i]
and substring[1…j];

2. If i ==0, return j.

3. If j== 0, return i.

4. If s1[i-1] == s2[j-1], Return 1 added to the result of scs(s1,s2,i-1,j-1).

5. s1[i-1] != s2[j-1]

- Exclude character at position i: Return 1 added to the result of scs(s1,s2,i-1,j).

- Exclude character at position j: Return 1 added to the result of scs(s1,s2,i,j-1).

Return the minimum of these two options.

6. Print "SCS : " followed by the result of of scs(s1,s2,s1.length(),s2.length()).

7. End of the algorithm.

The worst-case time complexity of the above solution is O(2(m+n)).


09.Longest Increasing Subsequence
using Dynamic Programming
To find the longest increasing subsequence (LIS) in an array (nums) of
size n.
1.create an array IN[n];

2.Intialized IN[0]=1;

3.iterate nums array index 1 to n-1 for each element nums[i].


3.1 initialized variable h=1.
3.2 iterate for nums array index 0 to i
- If nums[j]<nums[i] and h<IN[j]+1 , h=IN[j]+1.
3.4 IN[i]=h;

4.iterate IN array index 0 to n and find the maximum value and print the
value.

5.End of Algorithm.
the worst-case time complexity of the above solution is O(N(2)).
10. Longest Bitonic Subsequence
To find the longest increasing subsequence (LIS) in an array (nums) of size
n.
1.create an array IN[n];

2.Intialized IN[0]=1;

3.iterate nums array index 1 to n-1 for each element nums[i].


3.1 initialized variable h=1.
3.2 iterate for nums array index 0 to i
-If(nums[j]<nums[i] and h<IN[j]+1 ) , h=IN[j]+1.
3.3 IN[i]=h;

4.create an array DE[n];


5.Intialized DE[n-1]=1;
6.iterate nums array index n-2 to 0 for each element nums[i].
6.1 Take variable h,initialized variable h=1.
6.2 iterate for nums array index n-1 to i.
- If nums[j]<nums[i] and h<DE[j]+1 , h=DE[j]+1.
6.4 DE[i]=h;
7. Find the IN[i]+DE[i] -1 maximum value iterate index 0 to n-1 and Print
the value.
8. End the algorithm
the worst-case time complexity of the above solution is O(N(2)).
12 The Levenshtein distance (Edit
distance) Problem
1. Define a function dist(s1,s2, i, j) to find minimum number of operations required for levenshtein
distance two substring s1[1...i] and s2[1…j].

2. If i ==0, return j.

3. If j==0 , return i.

4. Initialized variable operation=1.

4. If str[i-1] == str[j-1], operation =0.

5. If str[i] != str[j],

- Deletion: Moving to the previous index in s1. Return dist(s1,s2,i-1,j)+1;

-Insertion: Moving to the previous index in s2. Return dist(s1,s2,i,j-1)+1;

-Substitution: Moving previous indices in both s1 and s2, Return dist(s1,s2,i-1,j-1)+operation;

Return the minimum of these three options.

6. Print operation followed by the result of dist(s1,s2,s1.length(),s2.length());

7. End of the algorithm.

The worst-case time complexity of the above solution is O(3(m+n)).


29.Count the number of times a pattern
appears in a given string as a
subsequence
1. Define a function counts(str,ptr, i, j) to find count the number two substring str[1...i] and ptr[1…j].

2.if i==1 and j==1 , if str[0]==ptr[0] return 1,if str[0]!=ptr[0] return 0.

3. If i ==0, return 0.

4. If j==0 , return 1.

5.if str[i-1]==str[j-1],

-Exclude the last character from both str and ptr counts(str,ptr,i-1,j-1)

-Exclude the last character from ptr counts(str,ptr,i-1,j-1)


Return the sum of these option.

6.if str[i-1]==str[j-1],

-Exclude the last character from ptr counts(str,ptr,i-1,j).


Return the these option

6. Print operation followed by the result of counts(str,ptr,str.length(),ptr.length()).

7. End of the algorithm.


27.Coin Change Problem
1. Define a function count(arr[], target) to find count the number way coin change

2. if target==0, return 1.

3. If target<0, return 0.

4.intialized variable result=0.

5.iterate the array each element index 0 to element arr[i].

-result +=count(arr,target-arr[i]).

6. return result

6. Print followed by the result of count(arr[],target).

7. End of the algorithm.

The worst-case time complexity of the above solution is O(N^2).


26.Coin change-making problem
1. Define a function count(arr[], target) to find count the number way coin change

2. if target==0, return 0.

3. If target<0, return INT_MAX.

4.intialized variable result=INT_MAX.

5.iterate the array each element index 0 to element arr[i].

-if count(arr,target-arr[i])+1 < result, result=count(arr,target-arr[i])+1.

6. return result

6. Print followed by the result of count(arr[],target).

7. End of the algorithm.

The worst-case time complexity of the above solution is O(N^2).


23Find all n-digit binary numbers
without any consecutive 1’s
1. Define a function count(n, last_digit) to find count the number way coin change

2. if n==0, return 0.

3. If n==1

-if last_digit==1, return 1.

- If last_digit==0, return 2.

4.if last_digit==0,

-include 0 current postion count(n-1,0).

-include 1 current postion count(n-1,1).

Return the sum these two option.

5.if last_digit==1,

-include 0 current postion count(n-1,0).

Return the this option.

6. Print followed by the result of count(n,0).

7. End of the algorithm.

The worst-case time complexity of the above solution is exponential O(2n)


18.0–1 Knapsack Problem
1. Define a function knapsack(value [], weight [],n,rem_weight) to find maximum profit

2.if rem_weight<0 , return INT_MIN.

3. if n<0 or rem_weight==0 , return 0.

4. -Include current item v[n]+knapsack(value[],weight[],n-1,rem_weight-weight).

-exclude current item knapsack(value[],weight[],n-1,rem_weight).

Return maximum these two option .

5. . Print followed by the result of knapsack(value[],weight[],value.size()-1,maximum_weight).


6.End of the algorithm.

The worst-case time complexity of the above solution is exponential O(2n)


16.Find the longest sequence formed by
adjacent numbers in the matrix
1. Define a function path(mat[][], i,j,n) to find longest sequence adjacent number 2D matrix
2. If i<0 or j<0 or i>n or j>n , return 0.
3. -if i+1<n ,
if mat[i][j]+1== mat[i+1][j], recur to down cell path(mat,i+1,j,n)+1.

- if j+1<n ,

if mat[i][j]+1== mat[i][j+1], recur to right cell path(mat,i,j+1,n)+1.

-if i-1>0 ,
if mat[i][j]+1== mat[i-1][j], recur to top cell path(mat,i-1,j,n)+1.

-if j-1>0 ,
if mat[i][j]+1== mat[i][j-1], recur top left cell path(mat,i,j-1,n)+1.
Return maximum these four option.

5.Intialized variable length=0,row=0,colum=0.

6.Iterate the each element mat[0…i][0…j]

- if h<path(mat,I,j)+1, h=path(mat,I,j), row=i,colum=j.

7.print the value mat[row][colum] to mat[row][colum]+h.

8. End of algoritm
P-8: Print all shortest common super sequence (SCS)
Algorithm:

1. Create a 2D array ‘dp table’ to store the length of SCS for substrings of ‘X’ and ‘Y’.

2. Fill ‘dp table’ using dynamic programming to determine the length of the SCS for all substrings of ‘X’
and ‘Y’.

3. Recursive SCS Construction:

If ‘X’ is empty, return ‘Y’.

If ‘Y’ is empty, return ‘X’.

If the last characters of ‘X’ and ‘Y’ match, append that character to all SCS of the remaining substrings.

If the last characters do not match:

• If the top cell of ‘dp table’ has a smaller value than the left cell, append the last character of ‘X’ to all
SCS of the remaining substrings. (dp [i - 1] [j] < dp[i] [j - 1])

• If the left cell of ‘dp table’ has a smaller value than the top cell, append the last character of ‘Y’ to all
SCS of the remaining substrings. (dp [i - 1] [j] > dp[i] [j - 1])

• If both cells have the same value, append both characters to all SCS of the remaining substrings.

4. Convert the list of SCS strings to a set to remove duplicates.

5. Return SCS Set.

Time Complexity:

m is the length of first string and n is the length of second string.

Time complexity: O (m*n + 2(m+n))


P-13: Largest square submatrix (LSS)
Algorithm:

1. Define a function ‘findLargestSquareSubmatrix’ that takes a 2D vector ‘mat’ representing the


binary matrix.

2. Get the number of rows ‘M’ by calling ‘mat.size()’, and the number of columns ‘N’ by calling
‘mat[0].size()’.

3. Create a 2D vector ‘dp’ of size ‘M’x’N’, initialized with all elements set to 0.

4. Initialize the first row of ‘dp’ with the corresponding elements from the first row of ‘mat’, and
do the same for the first column.

5. Iterate over the remaining positions of ‘dp’ (starting from index 1,1):

if (mat[i][j] == 1)

dp[i][j] = 1 + min ({dp [i - 1] [j], dp[i] [j - 1], dp [i - 1] [j - 1]});

Otherwise, keep of dp[i][j] =0

6. Let, assume maxSize =0

7. Now, start (0,0) index and calculate dp[i][j] and compare to ‘maxSize’ and update if
necessary.

8. Print the size of the largest square submatrix of 1's, which is ‘maxSize’.

9. Return ‘maxSize’.

Time Complexity:

M is the number of rows and N is the number of columns

Time complexity: O (M*N)


15.Find minimum cost to reach the last
cell of a matrix from its first cell
1.Take 2D array matrix[r][c] ,matrix row r and matrix collum c.

2.iterate array matrix first row j index 1 to c-1.

Matrix[0][j]+=matrix[0][j-1].

3.iterate array matrix first collum index 1 to r-1.

Matrix[i][0]+=matrix[i-1][0].

4.iterate matrix elment skip first row and first collum matrix[1…i][1…j]

-Matrix[i][j] add minimum of top cell matrix[i-1][j] ,left cell matrix[i][j-1].

5. print the last element the matrix[r-1][c-1].

6.end the algortihm

Time complexity O(n*n)


13.Find the size of the largest square
submatrix of 1’s present in a binary
matrix
1.Take the 2d array matrix[r][c], here r is row and c is collum.
2.take 2d array lookup
3.intialized array first row and first collum is 0.
4.intialized variable h=0.
5. Iterate the matrix each element matrix[0…i][0…j].
-if matrix[i][j]==1,lookup[i+1][j+1]=min(lookup[i][j],lookup[i][j+1],lookup[i+1][j])+1.
6.find to maximum value in lookup 2d array and print it.
7.End of Algoritnm.

You might also like