Answer the following questions In class, we discussed a recursive algorithm for
ID: 3795853 • Letter: A
Question
Answer the following questions In class, we discussed a recursive algorithm for generating the power set of a set T, i.e., the set of all possible subsets of T. In this assignment, you will develop a well-documented pseudocode that generates all possible subsets of a given set of size n with the following requirements: your solution must be non-recursive, and must use a stack and a queue to solve the problem. For example: if set T = {2, 4, 7, 9} then your algorithm would generate: {}, {2}, {4}, {7}, {9}, {2, 4}, {2, 7}, {2, 9}, {4, 7}, {4, 9}, {7, 9}, etc. Calculate the time complexity of your algorithm using the big-Oh notation. Show all calculationsExplanation / Answer
import Java.io.IOExecption //header files
Class code
{
// Code for printing of the subsets
static void printSubsets(int set[])
{
int n=T[].length; // inserting set length into n variable
//Here the loop will run for 2^n times
//(1<<n) is a number with j the bit so when we and them with the subset number we get the number of subsets in given
System.out.println("subsets are");
for(int I=0;I<(1<<n);I++)
{
System.out.print("{");
//Loop will print the current subset
for(j=0;j<n;j++,)
{
if((I&(1<j)>0)
System.out.print(set[j]+"");
System.out.println("}");
}
}
//Programs execution starts from here
Public static void main(String args[])
int T[]={2,4,7,9};. // given superset
printSubsets(T[]); //calling the function to print subsets
}
}
output:
subsets are
{}
{2}
{4}
{7}
{9}
{2 4}
{2 7}
{2 9}
{4 7}
{4 9}
{2 4 7}
{2 7 9}
{4 7 9}