Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Given two strings x=x1x2···xnand y=y1y2···ym, we wish to find the length of their longest common subsequence, that is, the largest k for which there are indices i1<i2<···<ikand j1<j2<···<jkwith xi1xi2···xik=yj1yj2···yjk. Show how to do this in time 0(mn).

Short Answer

Expert verified

There are two approaches to doing this problem:

1. Brute Force Method

Here, we will consider all the substring of stringx=x1x2···xnand check if how many of those possible substrings of x can be found in stringy=y1y2···ym.

And also keeping track of maximum length of common substring.

But in this case, the run time is much greater then0mn.

2.Dynamic Algorithm Method

Here simultaneously we will find the longest suffix string pattern of both string and store them in a table. This will reduce the function call and hence our runtime will be 0mn.

Step by step solution

01

Defining Recursive Equation

Let di,j be the length of Longest Common Subsequence(LCS) in stringsx=x1x2···xnandy=y1y2···ym.

Let D be the matrix of m*ndi,j. This matrix will act as table to consecutively store the common substring.

Let Zk=z1z2···zkbe substring common in string ‘x’ and ‘y’, i.e., Z1...kis LCS in X1....iandY1....j

We have two cases:

  • Ifxi=yi

That means, zk=xi=yj

AndZkis LCSrole="math" localid="1657268874063" X1....i-1,Y1....j-1followed byzk.

  • Ifxiyi

That means,

Either Zk=X1....i-1,Y1....jor Zk=X1....i,Y1....j-1

So, our recursive equation is:

di,j={0;ifi=0,j=0di-1,j-1+1;ifxi=yjMaxdi-1,j,di,j-1;ifxiyj

02

Algorithm

m=Lengthxn=Lengthyfori=0tomdi,0=0forj=0tond0,j=0fori=1tomforj=1tonifxi=yidi,j=di-1,j-1+1elseifdi-1,jdi,j-1di,j=di-1,jelsedi,jdi,j-1

returnd

This algorithm will be run in 0mntime.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Optimal binary search trees. Suppose we know the frequency with which keywords occur in programs of a certain language, for instance:

begin5%do40%else8%end4%

if10%then10%while23%

We want to organize them in a binary search tree, so that the keyword in the root is alphabetically bigger than all the keywords in the left subtree and smaller than all the keywords in the right subtree (and this holds for all nodes). Figure 6.12 has a nicely-balanced example on the left. In this case, when a keyword is being looked up, the number of comparisons needed is at most three: for instance, in finding “while”, only the three nodes “end”, “then”, and “while” get examined. But since we know the frequency 196 Algorithms with which keywords are accessed, we can use an even more fine-tuned cost function, the average number of comparisons to look up a word. For the search tree on the left, it is

cost=1(0.04)+2(0.40+0.10)+3(0.05+0.08+0.10+0.23)=2.42

By this measure, the best search tree is the one on the right, which has a cost of Give an efficient algorithm for the following task. Input: n words (in sorted order); frequencies of these words: p1,p2,...,pn.

Output: The binary search tree of lowest cost (defined above as the expected number of comparisons in looking up a word).

Figure 6.12 Two binary search trees for the keywords of a programming language.

Given two strings x=x1x2···xnand y=y1y2···ym, we wish to find the length of their longest common substring, that is, the largest k for which there are indices i and j with xixi+1···xi+k-1=yjyj+1···yj+k-1. Show how to do this in time0(mn)

Counting heads. Given integersn and k, along with p1,...,pn[0,1], you want to determine the probability of obtaining exactly heads when biased coins are tossed independently at random, where pi is the probability that the ith coin comes up heads. Give an 0(n2)algorithm for this task. Assume you can multiply and add two numbers in [0,1]in 0(1)time.

Here is yet another variation on the change-making problem (Exercise 6.17). Given an unlimited supply of coins of denominations x1,x2,x3.........xnwe wish to make change for a value v using at most k coins; that is, we wish to find a set ofkcoins whose total value is v. This might not be possible: for instance, if the denominations are 5 and 10 and k=6, then we can make change for 55 but not for 65. Give an efficient dynamic-programming algorithm for the following problem. Input: ; x1,x2,x3.........xn;k;v.Question: Is it possible to make change for v using at most k coins, of denominations x1,x2,x3.........xn?

Sequence alignment. When a new gene is discovered, a standard approach to understanding its function is to look through a database of known genes and find close matches. The closeness of two genes is measured by the extent to which they are aligned. To formalize this, think of a gene as being a long string over an alphabet ={A,C,G,T}. Consider two genes (strings) x=ATGCCand y=TACGCA. An alignment of x and y is a way of matching up these two strings by writing them in columns, for instance:

A-T-GCCTA-CGC

Here the “_” indicates a “gap.” The characters of each string must appear in order, and each column must contain a character from at least one of the strings. The score of an alignment is specified by a scoring matrixδof size (+1)×(+1), where the extra row and column are to accommodate gaps. For instance the preceding alignment has the following score:

δ(-T)+δ(A,A)+δ(T,-)+δ(G,G)+δ(C,C)+δ(C,A)

Give a dynamic programming algorithm that takes as input two strings X[1K n] and Y {1K m} and a scoring matrix δand returns the highest-scoring alignment. The running time should be O(mn) .

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free