The Subset-Sum Problem

The Subset-Sum Problem

Part A (required) – The Subset Sum Problem For Ints

Using java.util.ArrayLists, create a simple main() that solves the subset sum problem for any ArrayList of ints. Here is an example of the set-up and output. You would fill in the actual code that makes it happen.

import java.util.*;_x000D_
_x000D_
//------------------------------------------------------_x000D_
public class Foothill_x000D_
{_x000D_
   // -------  main --------------_x000D_
   public static void main(String[] args) throws Exception_x000D_
   {_x000D_
      int target = 72;_x000D_
      ArrayList<Integer> dataSet = new ArrayList<Integer>();_x000D_
      ArrayList<Sublist> choices = new ArrayList<Sublist>();_x000D_
      int k, j, numSets, max, kBest, masterSum;_x000D_
      boolean foundPerfect;_x000D_
_x000D_
      dataSet.add(2); dataSet.add(12); dataSet.add(22);_x000D_
      dataSet.add(5); dataSet.add(15); dataSet.add(25);_x000D_
      dataSet.add(9); dataSet.add(19); dataSet.add(29);_x000D_
_x000D_
      // code supplied by student_x000D_
_x000D_
      choices.get(kBest).showSublist();_x000D_
   }_x000D_
}_x000D_

The local variables you see above are not required; they come from my solution. If you want to use different local variables, feel free.

Your output style can be different from mine, as long as it is contains equivalent information and is easy to understand. However, show enough runs to prove your algorithm works, and show at least one run that does not meet target, exactly. Also, provide a special test to quickly dispose of the case in which the target is larger than the sum of all elements in the master set. This way, any target that is too large will not have to sit through a long and painful algorithm. Demonstrate that this works by providing at least one run that seeks a target larger than the sum of all master set elements.

Finally, you are not finding all sub-lists, just one representative solution. There may be other sub-lists that do the job, but our algorithm only finds the first one it detects. (If you want to show all the solutions, that’s fine.)

A run would look like this:

Target time: 72_x000D_
Sublist ----------------------------- _x000D_
  sum: 72_x000D_
  array[0] = 2, _x000D_
  array[2] = 22, _x000D_
  array[3] = 5, _x000D_
  array[4] = 15, _x000D_
  array[6] = 9, _x000D_
  array[7] = 19_x000D_

Notice how the target is precisely met. This is not unusual when you have a varied data set. However, for the above data, if you ran a target of 13, it would not be met precisely, and this is also a good test of your algorithm. Test it well, not just on one or two values.

class Sublist

I have prompted you with all the essential details of the class Sublist that will support your algorithm. I’ll add a couple more hints:

  • To create an empty Sublist (which is the first thing you need to do in order to prime the pump) just instantiate one Sublist object. By definition, it represents an empty set, since it will have no elements in its internal indicesarray list.
  • The addItem() member function is clearly the most interesting of the outstanding instance methods. In that method you will have to create a new Sublist object whose sum is the sum of the calling object plus the value of the new item added. This will require a slightly different syntax for an int data set than it will for aniTunesEntry data set. In the latter case, you’ll need to get specific inside addItem() about what value within iTunesEntry you are adding. This means addItem() is going to have syntax that ties it firmly to the underlying data set. That’s okay for this part and the next, but not for option C.

Part B (required) – The Subset Sum Problem For iTunesEntries

Next create a main() that solves the subset sum problem for any ArrayList of iTunesEntries. You have to replace the term int with the term iTunesEntry in most places in the above program, but don’t do this mindlessly – some intsremain ints. There is a twist, as well: In your first solution you will have an expression similar (or identical) to this:

    ...  choices.get(j).getSum() +  dataSet.get(k)  ..._x000D_

This works fine if dataSet is a list of ints, but if it is a list of iTunesEntries, then it will not work; you can’t add an int(the return type of getSum()) to an iTunesObject. So you need to extract the appropriate number from the iTunesEntry object on the right. Here is your main set up and run:

import cs_1c.*;_x000D_
import java.text.*;_x000D_
import java.util.*;_x000D_
_x000D_
//------------------------------------------------------_x000D_
public class Foothill_x000D_
{_x000D_
   // -------  main --------------_x000D_
   public static void main(String[] args) throws Exception_x000D_
   {_x000D_
      int target = 3600;_x000D_
      ArrayList<iTunesEntry> dataSet = new ArrayList<iTunesEntry>();_x000D_
      ArrayList<Sublist> choices = new ArrayList<Sublist>();_x000D_
      int k, j, numSets, max, kBest, arraySize, masterSum;_x000D_
      boolean foundPerfect;_x000D_
      _x000D_
      // for formatting and timing_x000D_
      NumberFormat tidy = NumberFormat.getInstance(Locale.US);_x000D_
      tidy.setMaximumFractionDigits(4);_x000D_
      long startTime, stopTime; _x000D_
      _x000D_
      // read the iTunes Data_x000D_
      iTunesEntryReader tunesInput = new iTunesEntryReader("itunes_file.txt");_x000D_
_x000D_
      // test the success of the read:_x000D_
      if (tunesInput.readError())_x000D_
      {_x000D_
         System.out.println("couldn't open " + tunesInput.getFileName()_x000D_
            + " for input.");_x000D_
         return;_x000D_
      }_x000D_
_x000D_
      // load the dataSet ArrayList with the iTunes:_x000D_
      arraySize = tunesInput.getNumTunes();_x000D_
      for (k = 0; k < arraySize; k++)_x000D_
         dataSet.add(tunesInput.getTune(k));_x000D_
_x000D_
      choices.clear();_x000D_
      System.out.println("Target time: " + target);_x000D_
      _x000D_
      // code supplied by student_x000D_
_x000D_
      choices.get(kBest).showSublist();_x000D_
   }_x000D_
}_x000D_

Here is a run. Warning – don’t use target times over 1000 until you have debugged the program or you may run out of memory.

Target time: 3600_x000D_
Sublist ----------------------------- _x000D_
  sum: 3600_x000D_
  array[0] = Carrie Underwood | Cowboy Casanova |  3:56, _x000D_
  array[1] = Carrie Underwood | Quitter |  3:40, _x000D_
  array[2] = Rihanna | Russian Roulette |  3:48, _x000D_
  array[4] = Foo Fighters | Monkey Wrench |  3:50, _x000D_
  array[5] = Eric Clapton | Pretending |  4:43, _x000D_
  array[6] = Eric Clapton | Bad Love |  5:08, _x000D_
  array[7] = Howlin' Wolf | Everybody's In The Mood |  2:58, _x000D_
  array[8] = Howlin' Wolf | Well That's All Right |  2:55, _x000D_
  array[9] = Reverend Gary Davis | Samson and Delilah |  3:36, _x000D_
  array[11] = Roy Buchanan | Hot Cha |  3:28, _x000D_
  array[12] = Roy Buchanan | Green Onions |  7:23, _x000D_
  array[13] = Janiva Magness | I'm Just a Prisoner |  3:50, _x000D_
  array[14] = Janiva Magness | You Were Never Mine |  4:36, _x000D_
  array[15] = John Lee Hooker | Hobo Blues |  3:07, _x000D_
  array[16] = John Lee Hooker | I Can't Quit You Baby |  3:02_x000D_
Algorithm Elapsed Time: 0.149 seconds._x000D_

Run requirements stated in Part A are still required, here.

Again, notice how we get a perfect hour’s worth of music in a short list of 72 random tunes. Again, this is typical. If you don’t get perfect targets most of the time, your algorithm is probably wrong.

Another Option

You can create a generic to encapsulate the algorithm. This requires some design thought and decision making. It is tricky so I don’t recommend it unless you have finished the above very early (and I do not have an instructor solution for this, currently). Of course, all run requirements above are still in force.

"You need a similar assignment done from scratch? Our qualified writers will help you with a guaranteed AI-free & plagiarism-free A+ quality paper, Confidentiality, Timely delivery & Livechat/phone Support.


Discount Code: CIPD30



Click ORDER NOW..

order custom paper