/*! 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} Q24E Time and space complexity of dyn... [FREE SOLUTION] | 91影视

91影视

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.

Short Answer

Expert verified

This space can be reduced by calculating the values of the adjacent cells in the table and the space complexity will be reduced to O(n).

  1. By considering the value of the adjacent square, the edit distance value can be computed in the given space complexity.
  2. Yes, the algorithm can be modified as follows,
    K(i,0)=iifj>n2ifE(i,j)=E(i-1,j)+1ki,j-n2=Ki-1,j-n2elseifE(i,j)=E(i,j-1)+1Ki,j-n2=Ki,j-n2-1ElseifE(i,j)=E(i-1,j-1)+diff(i,j)Ki,j-n2=Ki-1,j-n2-1
    this scheme can be made to run in O(mn) time and O(n) space.
  3. The given scheme can be made to run in O(mn) time and O(n) space because of the size and the difficulty of the algorithm.

Step by step solution

01

Explain space complexity and how the space requirement can be reduced.

Space Complexity for any algorithm defines the total space that is required or used by that algorithm, for different sizes of the input.

To create any variable there required some space for the algorithm to run. All the space that is required by the algorithm is collectively known as the Space Complexity of the algorithm.

In the dynamic programming algorithm for computing the edit distance between strings of length m and n creates a table of size and therefore needs O(mn) time and space. In practice, it will run out of space long before it runs out of time. This space can be reduced by calculating the values of the adjacent cells in the table and the space complexity will be reduced to O(n).

02

Show that the value of the edit distance can be computed.

(a)

Consider the Figure 6.4 in the text book, the calculation of a certain cell only needs the values of the upper left, upper and left cells of the cell. Considering the values of the adjacent cells in three directions, the current row and the previous row will be saved.

The state values are enough and they can be stored in a rolling array and the space complexity is O(n).

Therefore, By considering the value of the adjacent square, the edit distance value can be computed in the given space complexity.

03

Show how the algorithm can be modified to also return this value k.

(b)

Consider the edit-distance algorithm to calculate the path from the node (0,0) to node (n,m). The optimal path in the dag must pass through an intermediate node k,m2 for some k. In the algorithm swap n and m, which means represents rows and n represents listed rows. The rolling array K with n2+1 columns to store the values of k.

The modified edit-distance algorithm is as follows.

K(i,0)=iifj>n2ifE(i,j)=Ei-1,j+1Ki,j-n2=Ki-1,j-n2elseifE(i,j)=E(i,j-1)+1Ki,j-n2=Ki-1,j-n2-1ElseifE(i,j)=E(i-1,j-1)+diff(i,j)Ki,j-n2=Ki-1,j-n2-1

04

Show that the given scheme can be made to run in O(mn) time and O(n) space.

(c)

Consider that the given scheme is the recurrence relation, the time complexity is as follows,

T(m,n)=O(mn)+12O(mn)+14O(mn)+18O(mn)...=O(mn)

The time complexity is still O(mn), the constant is due to that the size of the algorithm is doubles and the difficulty of the algorithm implementation has also been greatly improved.

Therefore, it has been shown that the given scheme can be made to run in O(mn) time and O(n) space.

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

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

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

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

Consider the following variation on the change-making problem (Exercise 6.17): you are given denominations x1,x2,...,xn, and you want to make change for a value v, but you are allowed to use each denomination at most once. For instance, if the denominations are 1,5,10,20,then you can make change for 16=1+15and for 31=1+10+20but not for 40(because you can鈥檛 use 20 twice).

Input: Positive integers; x1,x2,...,xnanother integer v.

Output: Can you make change for v, using each denominationxi at most once?Show how to solve this problem in time O(nV).

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.

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.