keyboard

CoSc2030: Computer Science II

Fall Semester 2007
Instructor: Dr. Thomas Bailey

Lab 8: Vector Largest Sum

Often in the real world, images or noises need to be identified amidst extraneous images or sounds. There are different techniques to seperate the desired data from the surrounding noisy data. One way of doing this is to identify patterns within the signals transmitting the data. While the actual algorithm is more complicated than what we could accomplish in lab, we will be doing a simplified version of this pattern finding in lab today using vectors filled with ramdomly generated doubles to represent our signals. The algorithm to determine where the pattern lies, is to find the largest sum within a contiguous section of the vector. There is a slow, simple way of finding the largest sum and there is a fast, more complex, recursive way of finding the largest sum. We will be writing both today and comparing their performance by timing both version and plotting them.

To get started get the following source files: timer.cpp, timer.h, Random.cpp, Random.h, SASSolver.cpp, SASSolver.h, SubArrSumTimer.cpp Note: code compiles, but the methods you will be writing contain dummy return values, so do not try to run the code until you have completed part one.

Lab Assignment

  1. First we will be writing the code for the slow simple way. Write the method double SASOne::run( vector & vec) This method finds the sum by starting at the beginning of the vector and iteratively walking through it keeping track of the current largest sum, and replacing it every time a new largest sum is found. Each time the end of the vector is reached, the starting index is increased and the the vector is traversed again. Once the vector has been traversed with every index as the starting point, the largest contiguous sum will have been found.
  2. Time the SASOne for some n value which gives a run time of approximately 8 to 12 seconds, then run the code for for n/2, n/4, n/8, n/16. Enter these times into an Excel spreadsheet just like in lab 4.
  3. Now that we have written the slow and easy way to calculate the largest sum we are now going to write the fast recursive way. In order to calculate the largest sum we must do several things. First, we divide the vector into two sides, left and right. Each side of the vector has four parts which must be tracked: a sum which is the entire sum for that half of the vector, the sum starting at the end of that half of the vector going to the left (so bestToRight starts at the left half's end going down, and rhs.bestToRight starts at the right half's end and going down), bestToLeft which starts at the beginning of each half of the vector going to the right (so bestToLeft starts at the left half's beginning going up, and rhs.bestToLeft starts at the right half's beginning going up). And the best which keeps track of the best sum found for either half of the vector.

    This way of calculating the largest sum involves writing two methods:
    1. ResType ResType::operator+= ( const ResType & rhs) which is called by the + operator. += must check to ensure that the end of the left half of the vector and the beginning of the right half of the vector are adjacent. Expand the left hand side's end to be equal to the the right hand side's endIt must also check to see if the left hand side's best is less than the sums composed of the left hand side's bestToRight and rhs.bestToLeft. If so, update best to reflect this. Then, check to see if the left-hand side's best is less than the right-hand side's best, if so update the left hand side's best accordingly. Next, check to see if the lefthand side's sum plus the right hand side's bestToLeft is greater than the left hand side's bestToLeft, if so update the left hand side's bestToLeft. Now, check to see if the sum of the left hand side's bestToRight and the right hand side'sbestToLeft is greater than the left hand side's bestToLeft, if so update bestToLeft appropriately. The next check that must be performed is to see if the left hand side's bestToRight summed with the right hand side's sum is less than the right hand side's bestToRight, if it is, update the left hand side's bestToRight appropriately. Finally, update the left hand side's sum to include the right hand side's sum
    2. Write the code for SASFour::run you will be writing the recursive code to calculate the sum. For this code there will be a base case in which you want to check if end is less than or equal to begin + 1 else divide the vector into two parts and recurse on the left and right sides.
  4. Time the SASFour for n, n/2, n/4, n/8, n/16, for some appropriate n
  5. Compare the times for SASOne and SASFour and explain the results by using Excel to plot time/f(n) vs time[log scale] to verify your conjecture of f(n3), the time complexity index function of SASOne. Then use Excel to plot time/f(n) vs time[log scale] to verify your conjecture that the runtime for SASFour is linear, the time complexity index function of SASFour.

Turn In:

Upload the modified with your name in comments Lab8 SASSolver.cpp and SubArrSumTimer2

<< CoSc2030 Home
Copyright © 2005 Thomas Bailey