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
  • JAVA - Given two List objects (input and output), write a generic method that 1) clears...

    JAVA - Given two List objects (input and output), write a generic method that 1) clears output list if it contains any element, and 2) copies every element of input list to the output list in the same order and position. Note that the type of output list can be any super type of the input list (e.g. type(output)=Numeric & type(input)=Double). import java.util.List; import java.util.ArrayList; public class Collections { // Modify method signature if needed & implement the body of...

  • 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 *****************************************************/...

  • For this lab, you will be writing search algorithms. I have provided method signatures for you...

    For this lab, you will be writing search algorithms. I have provided method signatures for you to follow. The code I am providing already compiles, so as you write more code you can continually test it as you go. Write all of the methods in order as they appear in the code. DO NOT move on to another method until the previous method is fully tested. 1. The CandyBar class defines an object of type CandyBar. This object has three...

  • 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...

  • 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; /**...

  • Given list.h, main.cpp, i need to write list.cpp. having a lot of problems. please help. list.h:...

    Given list.h, main.cpp, i need to write list.cpp. having a lot of problems. please help. list.h: ////////////////////////////////////////////////////////////////////////// #ifndef LIST_H #define LIST_H ////////////////////////////////////////////////////////////////////////// namespace CS170 { struct node { int value; node *next; }; class list { public: // Constructor for list. Creates an empty list list(); /* Destructor for list. Empty the list and release the allocated memory */ ~list(); // Prints out the values contained in the list void print_list() const; // Returns the current size of the list...

  • 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...

  • 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...

  • 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...

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