Question

Write a java code to solve the following question using the backtracking algorithm. Given a set...

Write a java code to solve the following question using the backtracking algorithm.

Given a set of distinct integers, return all possible subsets.

input: new int[] {1,2,3}

output: [], [3], [2], [2,3], [1], [1,3], [1,2], [1,2,3]

What to submit: 1. Your source code in a text document (15pts), 2. A screen copy showing you can run the code and the result of running the code (5 pts). 3. What is the performance of your algorithm (5 pts), give the answer, and very brief discussion.

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

Please refer to the following code below:-

import java.util.ArrayList;
import java.util.List;
public class Backtracking {
    // main function to start the program
    public static void main(String[] args) {
        // Creating object for backtracking
        Backtracking b= new Backtracking();
        // Declaring array
        int[] a= {1, 2, 3};
        // creating list of lists
        List<List<Integer>> sub = b.sub(a);
        // printing all subsets
        System.out.println(sub);
    }
    // sub function
    public List<List<Integer>> sub(int[] a) {
        List<List<Integer>> list = new ArrayList<>();
        // calling function to store elements of array
        subsets(list, new ArrayList<>(), a, 0);
        return list;
    }
    // this method will create subset
    private void subsets(List<List<Integer>> list , List<Integer> result, int [] a, int begin){
        list.add(new ArrayList<>(result));
        // iterating in array
        for(int i = begin; i < a.length; i++){
            // add element
            result.add(a[i]);
            // Explore
            subsets(list, result, a, i + 1);
            // remove
            result.remove(result.size() - 1);
        }
    }

}

Output:-

[[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]]

========================================================

Please Refer to the following images:-

==========================

Time complexity:- O(2n)

===============================================

Please upvote

===============================================

Add a comment
Know the answer?
Add Answer to:
Write a java code to solve the following question using the backtracking algorithm. Given a set...
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
  • In python Simply write the Euclid algorithm in a form that takes two numbers from input...

    In python Simply write the Euclid algorithm in a form that takes two numbers from input and prints the gcd. The code does not need to check for valid input. It must work for any pair of positive integers. Create a separate program that uses the python timeit module to provide test run-times for your code. Provide at-least 5 sample runs. Create a third file that implements another algorithm to find the gcd. Provide a program that uses the timeit...

  • 1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array...

    1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...

  • use java and write in text a. Rewrite the following code segment using if statements. Assume...

    use java and write in text a. Rewrite the following code segment using if statements. Assume that grade has been declared as of type char. char grade= 'B';; switch (grade) { case 'A': System.out.println("Excellent"); break case 'B': System.out.println("Good"); default: System.out.println("you can do better”); } - b. write Java code that inputs an integer and prints each of its digit followed by ** e.gif the integer is 1234 then it should print 1**2**3**4 e.g. if the integer is 85 then it...

  • Part 2 Write Java code for the following and create a word document with the answers....

    Part 2 Write Java code for the following and create a word document with the answers. Test whether or not the following are true by writing the necessary statements in the driver class: 2, (A + B)T-AT + BT 4. (AB)- BA 6. A(BC) (AB)C I. (AT)% A 5. AB # BA // Java code to verify (A+B)C -AC + ВС Matrix anew Matrix(new int[](1,2),(2,0)); Matrix b new Matrix(new int1(11,2),(2,0); Matrix cnew Matrix(new int[1[1(í1,2),(2,0)); System.out.println(a.add(b).mult(c)); System.out.println(a.mult(c).add(b.mult(c))); For the following, write...

  • In a microsoft word document please write an algorithm for the following JAVA code: import java.util.Scanner;...

    In a microsoft word document please write an algorithm for the following JAVA code: import java.util.Scanner; public class SalesData {    public static void main(String[] args) {        int sales[] = new int[12];        int max = -1, index = 0;        Scanner sc = new Scanner(System.in);        int q1 = 0, q2 = 0, q3 = 0, q4 = 0, total = 0;        for (int i = 0; i < 12; i++) {...

  • PLEASE SOLVE THESE QUESTIONS. USE JAVA FOR CODING. Q.5.8 Write short Java code to display the...

    PLEASE SOLVE THESE QUESTIONS. USE JAVA FOR CODING. Q.5.8 Write short Java code to display the following screen (no need to do it practically using a compiler, just basic coding to display the screen): Sample screenshot Input х ? Enter (1) to display the vowel count. Enter (2) to display the non vowel count. Enter (0) to exit. OK Cancel Q.5.9 Write a short Java code to display the following screen (no need to do it practically using a compiler,...

  • Assignment on Java programing 1. Write programs for the following exercises in Java. Each file should...

    Assignment on Java programing 1. Write programs for the following exercises in Java. Each file should have short description of the implemented class and for files with main method the problem it is solving. Make sure your files have appropriate names. Programs should write output to the Console. b) BST: Implement Binary Search Tree ADT with insert(int key), delete(int key), Node find(int key), and in-order traverse() where it prints the value of the key. Your operations should use recursion. The...

  • I am given an input file, P1input.txt and I have to write code to find the...

    I am given an input file, P1input.txt and I have to write code to find the min and max, as well as prime and perfect numbers from the input file. P1input.txt contains a hundred integers. Why doesn't my code compile properly to show me all the numbers? It just stops and displays usage: C:\> java Project1 P1input.txt 1 30 import java.io.*; // BufferedReader import java.util.*; // Scanner to read from a text file public class Project1 {    public static...

  • Selection sort is often the first sorting algorithm covered in introductory computer science courses. Java code...

    Selection sort is often the first sorting algorithm covered in introductory computer science courses. Java code that uses selection sort to place the elements of an integer array into non-decreasing order is shown here: public void swapNumbers(int i, int j) { ​int temp = numbers[i];​ /* put numbers[i] somewhere to keep it safe */ ​numbers[i] = numbers[j]; /* put numbers[j] at index i */ ​numbers[j] = temp;​ /* put numbers[i] at index j */ } public void selectionSort(int[] numbers) {...

  • Write the following Java code for a file called input.txt that has the below text in...

    Write the following Java code for a file called input.txt that has the below text in it. 5 6 1 1 0 0 1 1 0 1 0 0 1 1 0 1 0 0 2 1 0 3 1 1 1 1 1 2 3 4 5 1 2 0 1 hello 2 3 bye Create the Entry class. · Entry should have 3 data members o int x. o int y. o String name. Create a class called...

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