Advertisement
Vermiculus

Maximum Contiguous Subsequence Sum Algorithms N^3 and N^2

Oct 2nd, 2011
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.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 rand(java.util.Random r, int min, int max) {
  64.         return r.nextInt(max - min + 1) + min;
  65.     }
  66.  
  67.     public static void main(String[] args) {
  68.  
  69.         // used with {"5"}, the output is nicely formatted
  70.        
  71.         // get the param argument
  72.         int size = Integer.parseInt(args[0]);
  73.        
  74.         // populate a random array
  75.         int[] randArray = getRandArray((long)size);
  76.  
  77.         // print every random number you got
  78.         for (int i : randArray)
  79.             System.out.printf("%3d", i);
  80.  
  81.         System.out.printf("\nMax Sum N^3:%3d", subSum_N3(randArray));
  82.  
  83.         System.out.printf("\nMax Sum N^2:%3d", subSum_N2(randArray));
  84.  
  85.         int[] testArray = getRandArray(4096L);
  86.         System.out.printf("\n\nTesting N^2 against N^3 for efficiency...\nGO!\n\n");
  87.         System.out.printf("Max Sum N^2: %d\n", subSum_N2(testArray));
  88.         System.out.printf("Max Sum N^3: %d\n", subSum_N3(testArray));
  89.     }
  90.    
  91.     public static int[] getRandArray(long size) {
  92.        
  93.         // populate a random array
  94.         ArrayList<Integer> arr = new ArrayList<Integer>();
  95.         java.util.Random randSeed = new java.util.Random();
  96.         for (int i = 0; i < size; i++) {
  97.             arr.add(rand(randSeed, -9, 10));
  98.         }
  99.         return arrayListToArray(arr);
  100.        
  101.     }
  102.    
  103.     public static int[] arrayListToArray(ArrayList<Integer> ints)
  104.     {
  105.         int[] r = new int[ints.size()];
  106.        
  107.         for (int i=0; i < r.length; i++)
  108.             r[i] = ints.get(i);
  109.        
  110.         return r;
  111.     }
  112.  
  113. }
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement