CS3343/3341 Analysis of Algorithms Spring 2012 |
Dynamic Programming
Subset Sum Problem |
10207 11296 11587 12426 12823 13174 15859 15899 16006 16489 16493 17285 18091 18673 19133 19560 20346 20768 20949 22800 22857 23206 23547 24354 24927 25778 27320 27516 29356 30849 32221 32233 32447 32609 32644 33346 34591 35506 36451 37398 38235 40590 41208 42990 44137 44383 44436 44484 46011 46177 46205 46660 47870 48665 50851 51656 52005 53697 54719 56623 56894 57124 59791 60085 60313 62182 64244 65648 66814 66898 67950 68502 69640 70523 72041 73413 73454 76055 76652 77123 77600 77701 78465 81628 81866 82388 84351 84622 87382 88730 90015 90365 90435 91420 91977 94664 96331 97454 99313 99971 |
Possible #s <-------------------- W Array Index ----------------------------------------> in sum 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 Initial: 0 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 {1}: 0 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 {1,9}: 0 1 -1 -1 -1 -1 -1 -1 -1 9 9 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 {1,9,5}: 0 1 -1 -1 -1 5 5 -1 -1 9 9 -1 -1 -1 5 5 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 {1,9,5,3}: 0 1 -1 3 3 5 5 -1 3 9 9 -1 3 3 5 5 -1 3 3 -1 -1 -1 -1 -1 -1 -1 -1 {1,9,5,3,8}: 0 1 -1 3 3 5 5 -1 3 9 9 8 3 3 5 5 8 3 3 -1 8 8 8 8 -1 8 8 |
Building the Work Array W | |
---|---|
// calcWork: build work array W int[] calcWork(int[] A, int wsize) { int[] W = new int[wsize]; // work array W[0] = 0; // empty set yields sum of 0 for (int k = 1; k < W.length; k++) W[k] = -1; // sums not attained for (int j = 0; j < A.length; j++) { // each elt of A in turn int a = A[j]; // simplify notation for (int k = 0; k < W.length; k++) { // through all of W if ( (W[k] != -1) // nothing sums to k && (W[k] != a) // a not already in sum (no repetitions) && (k + a < wsize) // don't go past end of W && (W[k + a] == -1) // look for one sum only ) { W[k + a] = a; } } } return W; } |
10207 11296 11587 12426 12823 13174 15859 15899 16006 16489 16493 17285 18091 18673 19133 19560 20346 20768 20949 22800 22857 23206 23547 24354 24927 25778 27320 27516 29356 30849 32221 32233 32447 32609 32644 33346 34591 35506 36451 37398 38235 40590 41208 42990 44137 44383 44436 44484 46011 46177 46205 46660 47870 48665 50851 51656 52005 53697 54719 56623 56894 57124 59791 60085 60313 62182 64244 65648 66814 66898 67950 68502 69640 70523 72041 73413 73454 76055 76652 77123 77600 77701 78465 81628 81866 82388 84351 84622 87382 88730 90015 90365 90435 91420 91977 94664 96331 97454 99313 99971 |
131743 172851 191333 207683 228574 273208 326094 333462 364517 441372 443833 460112 466602 486659 508518 547196 571243 846222 919774 974546 |