/*! 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} 1E A contiguous subsequence of a li... [FREE SOLUTION] | 91影视

91影视

A contiguous subsequence of a list Sis a subsequence made up of consecutive elements of S. For instance, if Sis 5,15,30,10,5,40,10

then15,30,10 is a contiguous subsequence but5,15,40 is not. Give a linear-time algorithm for the following task:Input: A list of numbers a1,a2,...,an.

Output: The contiguous subsequence of maximum sum (a subsequence of length zero has sum zero).For the preceding example, the answer would be 10,5,40,10, with a sum of 55. (Hint: For each j{1,2,...,n}, consider contiguous subsequences ending exactly at position j.)

Short Answer

Expert verified

The algorithm to find contiguous subsequence of maximum sum is implemented using dynamic programming in linear time.

Step by step solution

01

Define Dynamic Programming

Dynamic programming is used to solve the problem efficiently having the following two properties:

  1. Overlapping Subproblems
  2. Optimal Substructure

Dynamic programming is used for the problem that can be divided into subproblems and the solution of subproblems can be reused to solve the problem. So, optimal solution of the problem is constructed using the optimal solution of subproblems.

02

Determine the recurrence relation to find continuous subsequence with maximum sum

Consider Kjn!r!nr!denotes the maximum contiguous sequence鈥檚 sum.

Here, represent the index at which the sequence ends.

For Kj+1, there are two possible options:

  1. Start new contiguous sequence with sum aj+1.
  2. Add the next value in the previous calculated sumKj.

Thus, the recurrence is written as:

Kj=MAXKj1+aj,aj

This is recursive call onKj which will add contiguous subsequence if it is encapsulate in a loop.

03

Determine the algorithm to find continuous subsequence with maximum sum

Consider a variable w to hold the maximum sum. The algorithm to find contiguous subsequence of maximum sum is as follows:

K0=0forj=1迟辞鈥n鈥勨赌勨赌勨赌勨赌勨赌勨赌勨赌勨赌Kj=MAX0,Kj1+xjs=k[j]i=0j=0k=0

fori=0迟辞鈥n鈥勨赌勨赌刬蹿鈥Ki>s

Then update new value ofs as maximum value of contiguous subsequence

ifKi=0 andi<k

Then setj=j+1

returnj,...,k

04

Determine the time complexity of algorithm

The first loop will takeOn time to execute as it executes for n times. The second loop will takeOn+1 time to execute as it is executedn+1 times. The time complexity of entire program isO2n+1 which is asymptotically equal to On.

Thus, the algorithm takes linear time.

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

Give an O(nt) algorithm for the following task. Input: A list of n positive integers a1,a2,...,an; a positive integer t. Question: Does some subset of the ai鈥檚 add up to t? (You can use each ai at most once.) (Hint: Look at subproblems of the form 鈥渄oes a subset of{a1,a2,...,ai} add up to ?鈥)

Alignment with gap penalties. The alignment algorithm of Exercise 6.26 helps to identify DNA sequences that are close to one another. The discrepancies between these closely matched sequences are often caused by errors in DNA replication. However, a closer look at the biological replication process reveals that the scoring function we considered earlier has a qualitative problem: nature often inserts or removes entire substrings of nucleotides (creating long gaps), rather than editing just one position at a time. Therefore, the penalty for a gap of length 10 should not be 10 times the penalty for a gap of length 1, but something significantly smaller.

Repeat Exercise 6.26, but this time use a modified scoring function in which the penalty for a gap of length k is c0 + c1k, where c0 and c1 are given constants (and c0 is larger than c1).

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.

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

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.

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.