Question: Run each algorithm on three randomly generated integer arrays of sizes N=1,000, 10,000, and 100,000, measure the running times, and determine if they are consistent

Run each algorithm on three randomly generated integer arrays of sizes N=1,000, 10,000, and 100,000, measure the running times, and determine if they are consistent with the theoretical analysis results of those algorithms given in class, i.e., if the running time of algorithm 1 for the MSS problem is proportional to N3 and that for Algorithm 2 is proportional to N2 , etc. Include a table in your report that summarizes the actual running times (in appropriate time units) and narrative about your observations regarding whether the implemented algorithms indeed demonstrate behaviors entailed by theoretical analysis.

Please run those tests with the below c++ code, and explain your observations. Thank you in advance

C++ Code:

#include "stdafx.h"

#include

#include

using namespace std;

// Cubic maximum contiguous subsequence sum algorithm.

int maxSubSum1(const vector & a)

{

int maxSum = 0;

for (int i = 0; i < a.size(); i++)

for (int j = i; j < a.size(); j++)

{

int thisSum = 0;

for (int k = i; k <= j; k++)

thisSum += a[k];

if (thisSum > maxSum)

maxSum = thisSum;

}

return maxSum;

}

//Quadratic maximum contiguous subsequence sum algorithm.

int maxSubSum2(const vector & a)

{

int maxSum = 0;

for (int i = 0; i < a.size(); i++)

{

int thisSum = 0;

for (int j = i; j < a.size(); j++)

{

thisSum += a[j];

if (thisSum > maxSum)

maxSum = thisSum;

}

}

return maxSum;

}

// Return maximum of three integers.

int max3(int a, int b, int c)

{

return a > b ? a > c ? a : c : b > c ? b : c;

}

/**

* Recursive maximum contiguous subsequence sum algorithm.

* Finds maximum sum in subarray spanning a[left..right].

* Does not attempt to maintain actual best sequence.

*/

int maxSumRec(const vector & a, int left, int right)

{

if (left == right) // Base case

if (a[left] > 0)

return a[left];

else

return 0;

int center = (left + right) / 2;

int maxLeftSum = maxSumRec(a, left, center);

int maxRightSum = maxSumRec(a, center + 1, right);

int maxLeftBorderSum = 0, leftBorderSum = 0;

for (int i = center; i >= left; i--)

{

leftBorderSum += a[i];

if (leftBorderSum > maxLeftBorderSum)

maxLeftBorderSum = leftBorderSum;

}

int maxRightBorderSum = 0, rightBorderSum = 0;

for (int j = center + 1; j <= right; j++)

{

rightBorderSum += a[j];

if (rightBorderSum > maxRightBorderSum)

maxRightBorderSum = rightBorderSum;

}

return max3(maxLeftSum, maxRightSum,

maxLeftBorderSum + maxRightBorderSum);

}

// Driver for divide-and-conquer maximum contiguous

// subsequence sum algorithm.

int maxSubSum3(const vector & a)

{

return maxSumRec(a, 0, a.size() - 1);

}

// Linear-time maximum contiguous subsequence sum algorithm.

int maxSubSum4(const vector & a)

{

int maxSum = 0, thisSum = 0;

for (int j = 0; j < a.size(); j++)

{

thisSum += a[j];

if (thisSum > maxSum)

maxSum = thisSum;

else if (thisSum < 0)

thisSum = 0;

}

return maxSum;

}

// main

int main()

{

// Declaration of a vector.

vector a(8);

a[0] = 6; a[1] = -7; a[2] = 2; a[3] = -3;

a[4] = -5; a[5] = 4; a[6] = 6; a[7] = -4;

// Declaration of a variable of integer datatype.

int maxisum;

maxisum = maxSubSum1(a);

cout << "Maximum sum of the subsequence "

<< "using algorithm 1 is " << maxisum << endl;

maxisum = maxSubSum2(a);

cout << "Maximum sum of the subsequence "

<< "using algorithm 2 is " << maxisum << endl;

maxisum = maxSubSum3(a);

cout << "Maximum sum of the subsequence "

<< "using algorithm 3 is " << maxisum << endl;

maxisum = maxSubSum4(a);

cout << "Maximum sum of the subsequence "

<< "using algorithm 4 is " << maxisum << endl;

system("pause");

return 0;

}

Please run those tests, and explain your observations

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!