Question

4 Dynamic Programmii Consider the following problem based on the transformation of a sequence (or collection) of coloured dis

i need the solution in pseudo code please.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Let us consider two sequences of disks(A[0 ...n-1],B[0.....m-1]):
n: Length of first sequences of disks
m: Length of second sequences of disks

//Pseudocode
Transform(A[0 ...n-1],B[0.....m-1])
{
// Create a table to store results of subproblems
int dp[n+1][m+1];
  
// Fill d[][] in bottom up manner
for (i=0 to n)
{
for (j=0; to m)
{
// If first sequence is empty, only option is to
// insert all disks of second sequence
if (i==0)
dp[i][j] = j; // Min. operations = j
  
// If second sequence is empty, only option is to
// remove all disks of second sequence
else if (j==0)
dp[i][j] = i; // Min. operations = i
  
// If last sequences are same, ignore last disk
// and recur for remaining sequence
else if (A[i-1] == B[j-1])
dp[i][j] = dp[i-1][j-1];
  
// If the last disk is different, consider all
// possibilities and find the minimum
else
dp[i][j] = 1 + min(dp[i][j-1], // Insert
dp[i-1][j], // Remove
dp[i-1][j-1]); // Replace
}
}
  
print( dp[n][m]);
}

Time complexity = O(n*m)

Add a comment
Know the answer?
Add Answer to:
i need the solution in pseudo code please. 4 Dynamic Programmii Consider the following problem based on the transf...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT