Advertisement
Vermiculus

Maximum Contiguous Subsequence Sum {O(n), O(n^2), O(n^3)}

Oct 3rd, 2011
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.91 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. /**
  4.  * @author Sean Allred
  5.  * @author Molly Domino
  6.  *
  7.  *         Tests the Maximum Contiguous Subsequence Sum Algorithms N^3 and N^2
  8.  */
  9. public class driver {
  10.  
  11.     public static final long MAX_LONG = 9223372036854775807L;
  12.    
  13.     /**
  14.      * n^3 algorithm. Takes every possible subset, sums, and returns the maximum
  15.      * sum to the user
  16.      *
  17.      * @param arr
  18.      *            the array to operate on
  19.      * @return the maximum contiguous subset sum
  20.      */
  21.     private static int subSum_N3(int arr[]) {
  22.         int maxSum = 0;
  23.  
  24.         for (int startElement = 0; startElement < arr.length - 1; startElement++) {
  25.             for (int endElement = startElement; endElement < arr.length; endElement++) {
  26.                 int thisSum = 0;
  27.                 for (int thisElementIndex = startElement; thisElementIndex <= endElement; thisElementIndex++) {
  28.                     thisSum += arr[thisElementIndex];
  29.                 }
  30.                 // update max
  31.                 if (thisSum > maxSum)
  32.                     maxSum = thisSum;
  33.             }
  34.         }
  35.         return maxSum;
  36.     }
  37.  
  38.     /**
  39.      * n^2 algorithm. Also takes every possible subset, but sums common subsets
  40.      * only once
  41.      *
  42.      * @param arr
  43.      *            the array to operate on
  44.      * @return the maximum contiguous subset sum
  45.      */
  46.     private static int subSum_N2(int arr[]) {
  47.  
  48.         int maxSum = 0, thisSum = 0;
  49.  
  50.         for (int startElement = 0; startElement < arr.length; startElement++) {
  51.             thisSum = 0;
  52.             for (int endElement = startElement; endElement < arr.length; endElement++) {
  53.                 thisSum += arr[endElement];
  54.                 // update max
  55.                 if (thisSum > maxSum)
  56.                     maxSum = thisSum;
  57.             }
  58.         }
  59.         return maxSum;
  60.  
  61.     }
  62.  
  63.     private static int subSum_N1(int a[]) {
  64.         int maxSum = 0, thisSum = 0, startElement=0, endElement = 0;
  65.         // Cycle through all possible end indexes.
  66.         for (endElement = 0; endElement < a.length; endElement++) {
  67.             thisSum += a[endElement]; // No need to re-add all values.
  68.             if (thisSum > maxSum) {
  69.                 maxSum = thisSum;
  70.             } else if (thisSum < 0) {
  71.                 startElement = endElement + 1; // Only possible MCSSs start with an index >j.
  72.                 thisSum = 0; // Reset running sum.
  73.             }
  74.         }
  75.         return maxSum;
  76.     }
  77.     private static int rand(java.util.Random r, int min, int max) {
  78.         return r.nextInt(max - min + 1) + min;
  79.     }
  80.  
  81.     public static void main(String[] args) {
  82.  
  83.         // used with {"5"}, the output is nicely formatted
  84.        
  85.         // get the param argument
  86.         int size = Integer.parseInt(args[0]);
  87.        
  88.         // populate a random array
  89.         int[] randArray = getRandArray((long)size);
  90.  
  91.         // print every random number you got
  92.         for (int i : randArray)
  93.             System.out.printf("%3d", i);
  94.                
  95.         System.out.printf("%nMax Sum N^3:%3d", subSum_N1(getRandArray(65536L)));
  96.                
  97.         System.out.printf("%nMax Sum N^3:%3d", subSum_N3(randArray));
  98.  
  99.         System.out.printf("%nMax Sum N^2:%3d", subSum_N2(randArray));
  100.        
  101.         // Efficiency Testing
  102.  
  103.         int[] testArray = getRandArray(4096L);
  104.         System.out.printf("%n%nTesting N^2 against N^3 for efficiency on a %d-element array...%nGO!%n%n", testArray.length);
  105.         Stopwatch s = new Stopwatch();
  106.        
  107.         s.start(); System.out.printf("Max Sum N^1: %d", subSum_N1(testArray));
  108.         s.stop();  System.out.printf("%nTime: %d ms%n", s.getElapsedTime());
  109.        
  110.         s.start(); System.out.printf("Max Sum N^2: %d", subSum_N2(testArray));
  111.         s.stop();  System.out.printf("%nTime: %d ms%n", s.getElapsedTime());
  112.        
  113.         s.start(); System.out.printf("Max Sum N^3: %d", subSum_N3(testArray));
  114.         s.stop();  System.out.printf("%nTime: %d ms%n", s.getElapsedTime());
  115.     }
  116.    
  117.     public static int[] getRandArray(long size) {
  118.        
  119.         // populate a random array
  120.         ArrayList<Integer> arr = new ArrayList<Integer>();
  121.         java.util.Random randSeed = new java.util.Random();
  122.         for (int i = 0; i < size; i++) {
  123.             arr.add(rand(randSeed, -9, 10));
  124.         }
  125.         return arrayListToArray(arr);
  126.     }
  127.    
  128.     public static int[] arrayListToArray(ArrayList<Integer> ints)
  129.     {
  130.         int[] r = new int[ints.size()];
  131.        
  132.         for (int i=0; i < r.length; i++)
  133.             r[i] = ints.get(i);
  134.        
  135.         return r;
  136.     }
  137.  
  138. }
  139.  
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement