How to recover the actual block of years from the array

Assignment Help Data Structure & Algorithms
Reference no: EM132700846

For all algorithms design problems, describe your algorithm in words, then give a (concise and human- readable) pseudo-code.

1. Longest streak of CO2 increase

Suppose that you are working with a scientist studying how the levels of CO2 in the atmosphere change over time. She obtained the data showing the level of CO2 in the atmosphere every year for a long period of time, and would like to find a block of consecutive years during which the increase of CO2 was maximal. To help with algorithm design, she converted the data into a list of numbers showing relative increase or decrease of CO2 this year in comparison to the previous year, and now all that is left is to design an algorithm to solve this problem (as fast as possible).

For example, suppose that the sequence of relative increases/decreases is f63; -59; 41; 11; -13; 53;
-35; 11g.Then the largest increase happens over years 3 to 6, with 41+11-13+53=92.

a) Design a brute-force Θ(n2) algorithm to solve this problem. Make sure your description and pseudo-code are concise and clean, and include an explanation why this algorithm works.
b) Now, design a Θ(n log n) divide-and-conquer algorithm for this problem.
i. Describe your algorithm in words
ii. Give (concise) pseudo-code.
iii. Give a recurrence for its running time, and explain why this recurrence gives you Θ(n log n).
iv. Explain why your algorithm computes an optimal solution.
c) Finally, give a Θ(n) dynamic programming algorithm solving this problem.
i. First, describe your algorithm in words.
ii. Give the definition of an array A that you will use to solve the problem. How can you get the amount of the maximal increase in CO2 over all blocks of consecutive years from this array A?
iii. Give a recurrence to compute elements of A, including initialization.
iv. Show the filled array for the example above produced by your algorithm.
v. Give pseudo-code for a dynamic programming algorithm that finds the amount of the maximal increase in CO2 over all blocks of consecutive years.
vi. Explain (in text and pseudo-code) how to recover the actual block of years from the array computed while finding the maximal increase in CO2. You can add information to the array when computing it, if you wish.
vii. Explain why your algorithm works and why it runs in linear time

2. Farmers barter

You are trying to facilitate barter among farmers at a market using your algorithm design skills. The problem is as follows: suppose one farmer brings a list of items to the market, with each item having a certain price (eg, a bunch of squashes), and wants to barter with another farmer for an item they have available (eg, a baby goat), where that second farmer has a selection of baby goats of different prices. To make the barter fair, the sum of prices of the squashes the first farmer gives to the second should exactly equal to the price of the baby goat she gets in exchange.

For example, the first farmer might have brought squashes costing 10; 12; 15; 15; 21; 24; 33, and the goats cost 20; 25; 30; 32. Then she can get the second goat with the first and third squashes, or the third goat with third and fourth squashes, but she cannot get the first or the fourth goats, since none of subsets of squashes cost 20 or 32.

Here, you will need to design a dynamic programming algorithm that takes as an input the list of prices of items the first farmer brought to barter (let's just call them squashes), and list of prices of items out of which she would like to buy one (let's call them goats), and outputs a list of goats that can be bartered, and, for each goat, the list of squashes that can be bartered for it. Remember that she only wants one goat, just choosing which one she can get, so lists of squashes for different goats do not have to be disjoint.

a) Describe your algorithm in words.
b) Give the definition of an array A that you will use to solve the problem. How can you get the list of goats each of which the first farmer can buy with her squashes from this array A? How can you check if there is no solution (that is, that she cannot find a set of squashes costing as much as any of the goats in the list)?
c) Give a recurrence to compute elements of A, including initialization.
d) Show the filled array as produced by your algorithm for the example above.
e) Give pseudo-code for a dynamic programming algorithm that finds which goats can be bartered for a (subset of) given squashes.
f) Explain (in text and pseudo-code) how, given a specific goat which can be bartered, to recover a set of squashes that sum up to its price from the array A. You can add information to the array when computing it, if you wish.
g) Finally, implement your algorithm. It should take as an input a comma-separated list of squash prices, followed by a newline, followed by a comma-separated list of goat prices, and output first the list of goats that can be bartered given this list of squashes, and then, for each goat that can be bartered, a corresponding list of squashes in the same order as in the input.

Reference no: EM132700846

Questions Cloud

Problem - LESSEE ENTRIES FOR AN OPERATING LEASE : Problem - LESSEE ENTRIES FOR AN OPERATING LEASE - Prepare the remaining entries for December 31, 2020 (the end of the first year)
Evaluate the impact of the internet on your everyday life : Evaluate the impact of the internet on your everyday life. Discuss how your life would change if the internet disappeared today. What would they miss the most?
Value of martell mining stock : Martell Mining was doing well the first two years growing at a rate of 20%. But from the beginning of the third year (or at the end of the second year)
Cost of the plant including flotation costs-leonardo co : Calculate the cost of the plant including flotation costs. (Round to 2 decimals)
How to recover the actual block of years from the array : How to recover the actual block of years from the array computed while finding the maximal increase in CO2. You can add information to the array when computing
Calculate the operating cash flows for the first year : Calculate the operating cash flows for the first year of the project.
Create shellcode for either unlink : Create shellcode for either unlink (Linux system call 10) or rmdir (Linux system call 40). unlink deletes a name and possibly the file it refers to
How a violation of the fourth amendment might occur : Describe the impact of the Fourth Amendment to the Constitution on issues of privacy. What are our rights? Describe how a violation of the 4th Amendment might.
How do social inequality and stratification : Public Sociology Blog - How do social inequality and stratification among social groups influence the population in the locality.

Reviews

Write a Review

Data Structure & Algorithms Questions & Answers

  Write the preorder traversal representation of the tree

Explain your key steps in determining the shortest path, and give the node sequence that corresponds to that shortest path - Write the preorder traversal representation of the tree.

  Learning for numeric prediction

Write down the output (class) values and number of instances that appear in each of the leaf nodes A, B and C of the tree - Learning for Numeric Prediction

  Design and build a prototype data warehouse

Design and build a prototype data warehouse using the data on Spend over £500 in the Department of Energy and Climate Change for the financial year 2012-2013 (April 2012 to March 2013 inclusive).

  Implement a state-space search

You will implement a state-space search that will find a solution to the sixteenpuzzle. For this program, in addition to the state-space search control, you will need to implement at least two other classes

  Using pseudocode, design an algorithm

BuzzButtons is a novelty item company manufacturing personalized lapel buttons. The owner is promoting his buttons by offering them at 99 cents each. He wants you to design a program asking the user for his or her name for the button, an e-mail addre..

  Virtualization & memory

Evaluate the efficiency and reliability of both the most common nonpreemptive dispatch algorithms and the most common preemptive dispatch algorithms used for scheduling decisions. Provide one (1) example of the best use for each dispatch algorithm..

  Order of semaphores in the customer changes

Explain what will happen if the order of semaphores in the customer changes - Explain, in words, what changes should be made and why

  Explain the need for complex data structures

Explain the need for complex data structures. Explain the design and application of arrays to program logic and data manipulation.

  Display the threaded bst that results from right-threading

The BST obtained by inserting the following C++ keywords in the order given: void, unsigned, short, long, into, double, char, bolo.

  Content of the queue at the beginning

Assume you are at the airport, waiting for the security check. There is one line (which is a FIFO queue), and 5 security check gates. Each person reaching in front of the queue is checked by the first available security gate.

  Find the weight range of normal onion bags

A packaging equipment is used to put onions into five pound bags. In fact the weights vary according to the normal distribution with expected price of average µ = 5.01 lb and standard deviation s = 0.05 lb.

  Which is best case at first iteration of quicksort algorithm

Please generate an array with 11 elements, which is the best case at the first iteration of QUICKSORT algorithm and then is the worst case for both subarrays.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd