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 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)

Short Answer

Expert verified

There are two approach of doing this problem:

1. Brute Force Method

Here, we will consider all the substring of string x=x1x2….xn and check if how many of those possible substring of x can be found in string y=y1y2….ym.

And also keeping track of maximum length of common substring.

But in this case, the run time is much greater then O(mn).

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 O(mn).

Step by step solution

01

Defining Recursive Equation

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

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

Let zk=z1z2....zk.be substring common in string ‘x’ and ‘y’, i.e., Z1...kis LCS in role="math" localid="1657260451767" X1....iandY1....j

We have two cases:

  • Ifxi=yi

That means, zk=xi=yj

And Zkis LCS X1....j-1,Y1....j-1followed by zk.

  • Ifxiyi

That means,

Either role="math" localid="1657261518179" Zk=X1....i-1,Y1....jorZk=X1....i,Y1....j-1

So, our recursive equation is:

di,j={di-1,j-1+1;ifxi=yjmaxdi-1.j,di.j-1;ifxiyj

02

Algorithm

m=Lenghtxn=Lenghtyfori=0tomdi,0=0forj=0tond0,j=0fori=1tomforj=1tonifxi=yidi,j=di-1,j-1+1elseifdi-1,jdi,j-1di,j=di,j-1elsedi,j=di,j-1

return d

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

Local sequence alignment. Often two DNA sequences are significantly different, but contain regions that are very similar and are highly conserved. Design an algorithm that takes an input two strings x[1Kn]and y[1Km]and a scoring matrix δ(as defined in Exercise 6.26), and outputs substrings x'andy'of x and y respectively, that have the highest-scoring alignment over all pairs of such substrings. Your algorithm should take time O(mn).

A subsequence is palindromic if it is the same whether read left to right or right to left. For instance, the sequence

A,C,G,T,G,T,C,A,A,A,A,T,C,G

has many palindromic subsequences, including A,C,G,C,Aand A,A,A,A(on the other hand, the subsequence A,C,Tis not palindromic). Devise an algorithm that takes a sequence X[1...n]and returns the (length of the) longest palindromic subsequence. Its running time should be0(n2).

Time and space complexity of dynamic programming. Our dynamic programming algorithm for computing the edit distance between strings of length m and n creates a table of size n×mand therefore needs O (mn) time and space. In practice, it will run out of space long before it runs out of time. How can this space requirement be reduced?

  1. Show that if we just want to compute the value of the edit distance (rather than the optimal sequence of edits), then only O(n) space is needed, because only a small portion of the table needs to be maintained at any given time.
  2. Now suppose that we also want the optimal sequence of edits. As we saw earlier, this problem can be recast in terms of a corresponding grid-shaped dag, in which the goal is to find the optimal path from node (0,0) to node (n,m). It will be convenient to work with this formulation, and while we’re talking about convenience, we might as well also assume that is a power of 2.
    Let’s start with a small addition to the edit distance algorithm that will turn out to be very useful. The optimal path in the dag must pass through an intermediate node (k,m2) for some k; show how the algorithm can be modified to also return this value k.
  3. Now consider a recursive scheme:
    Procedure find-path((0,0)(n,m))
    Compute the value kabove
    find-path ((0,0)k,m2)
    find-path k,m2n,m
    concatenate these two paths, with kin the middle.
    Show that this scheme can be made to run inO (mn) time and O(n) space.

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.

You are given a string of n characters s[1...n], which you believe to be a corrupted text document in which all punctuation has vanished (so that it looks something like “itwasthebestoftimes...”). You wish to reconstruct the document using a dictionary, which is available in the form of a Boolean function dict(.): for any string w,

dict(w)={trueifwisavalidwordfalseotherwise

Give a dynamic programming algorithm that determines whether the string s[.]can be reconstituted as a sequence of valid words. The running time should be at mostO(n2) , assuming calls to dict take unit time.

In the event that the string is valid, make your algorithm output the corresponding sequence of words.

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