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

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.

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

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

Reconstructing evolutionary trees by maximum parsimony. Suppose we manage to sequence a particular gene across a whole bunch of different species. For concreteness, say there are n species, and the sequences are strings of length k over alphabet={A,C,G,T}. How can we use this information to reconstruct the evolutionary history of these species?

Evolutionary history is commonly represented by a tree whose leaves are the different species, whose root is their common ancestor, and whose internal branches represent speciation events (that is, moments when a new species broke off from an existing one). Thus we need to find the following:

• An evolutionary tree with the given species at the leaves.

• For each internal node, a string of length K: the gene sequence for that particular ancestor.

For each possible tree T annotated with sequencess(u)kat each of its nodes , we can assign a score based on the principle of parsimony: fewer mutations are more likely.

localid="1659249441524" score(T)=(u.v)E(T)(numberofpositionsonwhichs(u)ands(v)disagree)

Finding the highest-score tree is a difficult problem. Here we will consider just a small part of it: suppose we know the structure of the tree, and we want to fill in the sequences s(u) of the internal nodes u. Here’s an example with k=4 and n=5:


(a) In this particular example, there are several maximum parsimony reconstructions of the internal node sequences. Find one of them.

(b) Give an efficient (in terms of n and k ) algorithm for this task. (Hint: Even though the sequences might be long, you can do just one position at a 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?

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

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

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