/*! This file is auto-generated */ .wp-block-button__link{color:#fff;background-color:#32373c;border-radius:9999px;box-shadow:none;text-decoration:none;padding:calc(.667em + 2px) calc(1.333em + 2px);font-size:1.125em}.wp-block-file__button{background:#32373c;color:#fff;text-decoration:none} Q8E Given two strings x=x1x2路路路xn... [FREE SOLUTION] | 91影视

91影视

Given two strings x=x1x2xnand y=y1y2ym, 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+1xi+k-1=yjyj+1yj+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鈥.虫n and check if how many of those possible substring of x can be found in string y=y1y2鈥.测m.

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=x1x2xnandy=y1y2ym.

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 鈥榵鈥 and 鈥榶鈥, 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 91影视!

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

Let us define a multiplication operation on three symbols a,b,caccording to the following table; thus ab=b,ba=c, and so on. Notice that the multiplication operation defined by the table is neither associative nor commutative.

Find an efficient algorithm that examines a string of these symbols, say bbbbac, and decides whether or not it is possible to parenthesize the string in such a way that the value of the resulting expression is . For example, on input bbbbacyour algorithm should return yes because((b(bb))(ba))c=a.

A certain string-processing language offers a primitive operation which splits a string into two pieces. Since this operation involves copying the original string, it takes n units of time for a string of length n, regardless of the location of the cut. Suppose, now, that you want to break a string into many pieces. The order in which the breaks are made can affect the total running time. For example, if you want to cut a 20-character string at positions 3 and 10, then making the first cut at position 3 incurs a total cost of 20+17=37, while doing position first has a better cost of 20+17=37.

Give a dynamic programming algorithm that, given the locations of m cuts in a string of length , finds the minimum cost of breaking the string into m +1 pieces.

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 nmand 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鈥檙e talking about convenience, we might as well also assume that is a power of 2.
    Let鈥檚 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.

You are given a convex polygon P on n vertices in the plane (specified by their x and y coordinates). A triangulation of P is a collection of n-3diagonals of such that no two diagonals intersect (except possibly at their endpoints). Notice that a triangulation splits the polygon鈥檚 interior into n-2 disjoint triangles. The cost of a triangulation is the sum of the lengths of the diagonals in it. Give an efficient algorithm for finding a triangulation of minimum cost. (Hint: Label the vertices of P by 1,....,n, starting from an arbitrary vertex and walking clockwise. For 1i<jn, let the subproblem A(i,j)denote the minimum cost triangulation of the polygon spanned by vertices i,i+1,...,j.).

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 鈥済ap.鈥 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 matrixof 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.