Advertisement
skb50bd

MSA_Full

Oct 5th, 2016
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. #include <ctime>
  5. #include <cstdlib>
  6. #include <climits>
  7. #define SIZE 100000
  8.  
  9. using namespace std;
  10.  
  11.  
  12. void findMaxCrossingSubarray(int A[], int lo, int mid, int hi, int &maxLeft, int &maxRight, int &crossSum) {
  13.     int leftSum = INT_MIN;
  14.     int Sum = 0;
  15.     for (int i = mid; i >= lo; i--) {
  16.         Sum += A[i];
  17.         if (Sum > leftSum) {
  18.             leftSum = Sum;
  19.             maxLeft = i;
  20.         }
  21.     }
  22.  
  23.     int rightSum = INT_MIN;
  24.     Sum = 0;
  25.     for (int i = mid + 1; i <= hi; i++) {
  26.         Sum += A[i];
  27.         if (Sum > rightSum) {
  28.             rightSum = Sum;
  29.             maxRight = i;
  30.         }
  31.     }
  32.  
  33.     crossSum = leftSum + rightSum;
  34.     return;
  35. }
  36.  
  37.  
  38. void findMaxSubarray(int A[], int lo, int hi, int &maxLo, int &maxHi, int &maxSum) {
  39.     if (lo == hi) {
  40.         maxLo = lo;
  41.         maxHi = hi;
  42.         maxSum = A[lo];
  43.         return;
  44.     }
  45.     else {
  46.         int mid = (lo + hi) / 2;
  47.         int leftLo, leftHi, leftSum, rightLo, rightHi, rightSum, crossLo, crossHi, crossSum;
  48.         findMaxSubarray(A, lo, mid, leftLo, leftHi, leftSum);
  49.         findMaxSubarray(A, mid + 1, hi, rightLo, rightHi, rightSum);
  50.         findMaxCrossingSubarray(A, lo, mid, hi, crossLo, crossHi, crossSum);
  51.  
  52.         if (leftSum >= rightSum && leftSum >= crossSum) {
  53.             maxLo = leftLo;
  54.             maxHi = leftHi;
  55.             maxSum = leftSum;
  56.         }
  57.         else if (rightSum >= leftSum && rightSum >= crossSum) {
  58.             maxLo = rightLo;
  59.             maxHi = rightHi;
  60.             maxSum = rightSum;
  61.         }
  62.         else {
  63.             maxLo = crossLo;
  64.             maxHi = crossHi;
  65.             maxSum = crossSum;
  66.         }
  67.         return;
  68.     }
  69. }
  70.  
  71.  
  72. void kadanes (int A[], int N, int &start, int &end, int &maxSoFar) {
  73.     int maxEndingHere = A[0];
  74.     maxSoFar = A[0];
  75.     int tStart = start = end = 0;
  76.  
  77.     for (int i = 1; i < N; i++) {
  78.         maxEndingHere += A[i];
  79.  
  80.         if (maxEndingHere < A[i]) {
  81.             maxEndingHere = A[i];
  82.             tStart = i;
  83.         }
  84.         if (maxSoFar < maxEndingHere) {
  85.             maxSoFar = maxEndingHere;
  86.             start = tStart;
  87.             end = i;
  88.         }
  89.     }
  90.     return;
  91. }
  92.  
  93.  
  94.  
  95. void bruteForce(int A[], int N, int &start, int &end, int &max) {
  96.     max = INT_MIN;
  97.     int sum;
  98.     for (int i = 0; i < N; i++) {
  99.         sum = 0;
  100.         for (int j = i; j < N; j++) {
  101.             sum += A[j];
  102.             if (sum > max) {
  103.                 start = i;
  104.                 end = j;
  105.                 max = sum;
  106.             }
  107.         }
  108.     }
  109. }
  110.  
  111.  
  112.  
  113. void display (int A[], int lo, int hi) {
  114.     for (int i = lo; i <= hi; i++)
  115.         cout << A[i] << " ";
  116.     cout << endl << endl;
  117. }
  118.  
  119.  
  120.  
  121.  
  122. int main() {
  123.     int A[SIZE];
  124.  
  125.     int lo, hi, Sum;
  126.     long long start, end;
  127.  
  128.     srand(time(NULL));
  129.     for (int i = 0; i < SIZE; i++)
  130.         A[i] = rand() % 2 ? rand() % 100 : rand() % 100 * -1;
  131.  
  132.  
  133.     // for (int i = 0; i < SIZE; i++)
  134.     //     cin >> A[i];
  135.  
  136.     //for (int i = 0; i < SIZE; i++)
  137.     //    cout << A[i] << " ";
  138.     //cout << endl << endl;
  139.  
  140.     ofstream out;
  141.     out.open("MSA-DATA.txt", ios::app);
  142.  
  143.     cout << "SIZE: " << SIZE << endl;
  144.     out << "SIZE: " << SIZE << endl;
  145.  
  146.     start = clock();
  147.     findMaxSubarray(A, 0, SIZE - 1, lo, hi, Sum);
  148.     end = clock();
  149.     cout << "Max Sum: " << Sum << endl;
  150.     cout << "Sub-array: [" << lo << "..." << hi << "]" << endl;;
  151.     out << "Recursive: "  << fixed << setprecision(4) << (end - start) * 1.0 / CLOCKS_PER_SEC << " Seconds" << endl;
  152.     cout << "Recursive: "  << fixed << setprecision(4) << (end - start) * 1.0 / CLOCKS_PER_SEC << " Seconds" << endl << endl << endl;
  153.  
  154.  
  155.     start = clock();
  156.     kadanes(A, SIZE, lo, hi, Sum);
  157.     end = clock();
  158.     cout << "Max Sum: " << Sum << endl;
  159.     cout << "Sub-array: [" << lo << "..." << hi << "]" << endl;;
  160.     out << "Kadane's: "  << fixed << setprecision(4) << (end - start) * 1.0 / CLOCKS_PER_SEC << " Seconds" << endl;
  161.     cout << "Kadane's: "  << fixed << setprecision(4) << (end - start) * 1.0 / CLOCKS_PER_SEC << " Seconds" << endl << endl << endl;
  162.  
  163.  
  164.     start = clock();
  165.     bruteForce(A, SIZE, lo, hi, Sum);
  166.     end = clock();
  167.     cout << "Max Sum: " << Sum << endl;
  168.     cout << "Sub-array: [" << lo << "..." << hi << "]" << endl;;
  169.     out << "Brute-Force: "  << fixed << setprecision(4) << (end - start) * 1.0 / CLOCKS_PER_SEC << " Seconds" << endl << endl << endl;
  170.     cout << "Brute-Force: "  << fixed << setprecision(4) << (end - start) * 1.0 / CLOCKS_PER_SEC << " Seconds" << endl << endl << endl;
  171.  
  172.  
  173.  
  174.     //cout << "Max Sum: " << Sum << endl;
  175.     //cout << "Sub-array: [" << lo << "..." << hi << "] = ";
  176.     //display(A, lo, hi);
  177.  
  178.     return 0;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement