Question

// enumerate the different combinations // for select k objects from the set of objects given...

// enumerate the different combinations
// for select k objects from the set of objects given in list L
public static List<List<String>> comb(List<String> L, int k) {
if (k == 0) return new ArrayList<List<String>>(){{add(new ArrayList<String>( ));}};
if (L.size() == k) return new ArrayList<List<String>>(){{add(new ArrayList<String>(L));}};
List<List<String>> L1 = comb(L.subList(1,L.size()),k);
List<List<String>> L2 = comb(L.subList(1,L.size()),k-1);
for (List<String> list : L2) list.add(L.get(0));
L1.addAll(L2);
return L1;
}

Q.Study the given Java codes. Explain in plain English the logic for enumerating combinations of a certain size for a given set.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The logic is:

While choosing K objects from a list of say N objects in the list L, we are excluding the first object and from the remaining N-1 objects, we are choosing K objects in each list of L1, and K-1 objects in each list of L2, and then we are including the first object which we excluded from L into each list of L2 (think of when L2 will be empty), which completes the K objects in each list of L2.
After this, we are concatenating all the objects from L1 and L2 to form a single List<List<String> > Object in which each List<String> consists of K objects.

Let's take a small example for this:

Let L = {"a","b","c"} and K = 2, then our answer should be {{"a","b"},{"b","c"},{"a","c"}} right?
Let's work on the code to find out what the answer is:

Case 1:

L={"a","b","c"}, K=2

K!=0

L.size()=3!=K

L1=comb({"b","c"},2)

Case 1.a:
L={"b","c"},K=2

K!=0

L.size()=2=K therefore this function will return {{"b","c"}}

Therefore, L1={{"b","c"}}

L2=comb({"b","c"},1)

Case 1.b

L={"b","c"},K=1

K!=0
L.size()=2!=K

L1=comb{"c",1)

Case 1.b.a

L={"c"}, K =1

K!=0

L.size()=1=K

Therefore this will return {{"c"}}

Therefore L1={{"c"}}

L2=comb({"c"},0)

Case 1.b.b

K==0, therefore this will return {{}}

Therefore L2={{}}

for(List<String> list: L2) list.add(L.get(0))

Therefore this for loop will modify L2 to {{"b"}}

L1.addAll(L2)

Therefore L1 will become {{"c","b"}}

Therefore L1={{"c","b"}}

Now L2=comb({"b","c"},1)

By similar logic L2 would be {{"b"},{"c"}}

Now we will add L[0] so L2 will become {{"a","b"},{"a","c"}}

Then L1.addAll(L2)

Therefore L1 will return {{"a","b"},{"a","c"},{"b","c"}} which is our required answer.
Hence done.

Add a comment
Know the answer?
Add Answer to:
// enumerate the different combinations // for select k objects from the set of objects given...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Given a class called Student and a class called Course that contains an ArrayList of Student....

    Given a class called Student and a class called Course that contains an ArrayList of Student. Write a method called getDeansList() as described below. Refer to Student.java below to learn what methods are available. I will paste both of the simple .JAVA codes below COURSE.JAVA import java.util.*; import java.io.*; /****************************************************** * A list of students in a course *****************************************************/ public class Course{     /** collection of Students */     private ArrayList<Student> roster; /***************************************************** Constructor for objects of class Course *****************************************************/...

  • In Java: Executable Class create an array of Employee objects. You can copy the array you...

    In Java: Executable Class create an array of Employee objects. You can copy the array you made for Chapter 20. create an ArrayList of Employee objects from that array. use an enhanced for loop to print all employees as shown in the sample output. create a TreeMap that uses Strings for keys and Employees as values. this TreeMap should map Employee ID numbers to their associated Employees. process the ArrayList to add elements to this map. print all employees in...

  • I have currently a functional Java progam with a gui. Its a simple table of contacts...

    I have currently a functional Java progam with a gui. Its a simple table of contacts with 3 buttons: add, remove, and edit. Right now the buttons are in the program but they do not work yet. I need the buttons to actually be able to add, remove, or edit things on the table. Thanks so much. Here is the working code so far: //PersonTableModel.java import java.util.List; import javax.swing.table.AbstractTableModel; public class PersonTableModel extends AbstractTableModel {     private static final int...

  • Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to...

    Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to both classes: public void reverseThisList(), This method will reverse the lists. When testing the method: print out the original list, call the new method, then print out the list again ------------------------------------------------------------------------- //ARRAY LIST class: public class ArrayList<E> implements List<E> { /** Array of elements in this List. */ private E[] data; /** Number of elements currently in this List. */ private int size; /**...

  • Need help with this Java. I need help with the "to do" sections. Theres two parts...

    Need help with this Java. I need help with the "to do" sections. Theres two parts to this and I added the photos with the entire question Lab14 Part 1: 1) change the XXX to a number in the list, and YYY to a number // not in the list 2.code a call to linearSearch with the item number (XXX) // that is in the list; store the return in the variable result 3. change both XXX numbers to the...

  • JAVA 3files seprate 1.Classroom.java 2.ClassroomTester.java (provided) 3.Student.java(provided) Use the following files: ClassroomTester.java import java.util.ArrayList; /** *...

    JAVA 3files seprate 1.Classroom.java 2.ClassroomTester.java (provided) 3.Student.java(provided) Use the following files: ClassroomTester.java import java.util.ArrayList; /** * Write a description of class UniversityTester here. * * @author (your name) * @version (a version number or a date) */ public class ClassroomTester { public static void main(String[] args) { ArrayList<Double> grades1 = new ArrayList<>(); grades1.add(82.0); grades1.add(91.5); grades1.add(85.0); Student student1 = new Student("Srivani", grades1); ArrayList<Double> grades2 = new ArrayList<>(); grades2.add(95.0); grades2.add(87.0); grades2.add(99.0); grades2.add(100.0); Student student2 = new Student("Carlos", grades2); ArrayList<Double> grades3 = new...

  • For this assignment, you will write a program to work with Huffman encoding. Huffman code is...

    For this assignment, you will write a program to work with Huffman encoding. Huffman code is an optimal prefix code, which means no code is the prefix of another code. Most of the code is included. You will need to extend the code to complete three additional methods. In particular, code to actually build the Huffman tree is provided. It uses a data file containing the frequency of occurrence of characters. You will write the following three methods in the...

  • What if you had to write a program that would keep track of a list of...

    What if you had to write a program that would keep track of a list of rectangles? This might be for a house painter to use in calculating square footage of walls that need paint, or for an advertising agency to keep track of the space available on billboards. The first step would be to define a class where one object represents one rectangle's length and width. Once we have class Rectangle, then we can make as many objects of...

  • The names of the two input files as well as the output file are supposed to be provided as arguments. Could you please fix it? Thanks. DPriorityQueue.java: // Import the required classes import java....

    The names of the two input files as well as the output file are supposed to be provided as arguments. Could you please fix it? Thanks. DPriorityQueue.java: // Import the required classes import java.io.*; import java.util.*; //Create the class public class DPriorityQueue { //Declare the private members variables. private int type1,type2; private String CostInTime[][], SVertex, DVertex; private List<String> listOfTheNodes; private Set<String> List; private List<Root> ListOfVisitedNode; private HashMap<String, Integer> minimalDistance; private HashMap<String, Integer> distOfVertices; private PriorityQueue<City> priorityQueue; // prove the definition...

  • import java.util.Iterator; import java.util.LinkedList; import java.util.TreeMap; import java.util.ArrayList; public class MultiValuedTreeMap<K, V> extends TreeMap<K, LinkedList<V>> implements...

    import java.util.Iterator; import java.util.LinkedList; import java.util.TreeMap; import java.util.ArrayList; public class MultiValuedTreeMap<K, V> extends TreeMap<K, LinkedList<V>> implements Iterable<Pair<K, V>> {    private static final long serialVersionUID = -6229569372944782075L;       public void add(K k, V v) {        if (!containsKey(k)) { put(k, new LinkedList<V>()); } get(k).add(v);    }       public V removeFirst(K k) {               if (!containsKey(k)) { return null;        } V value = get(k).removeFirst(); if (get(k).isEmpty()) { super.remove(k); } return value;    }...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT