Question

In: Computer Science

Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of...

Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of integers. Assume that the List type has members:

int List.length returns the length of the list.
void List.push(T n) pushes an element n to the front of the list
T List.pop() pops an element from the front of the list.
List$$ List$$.concat(List$$ other) returns the concatenation of this list with other.

Explain in plain English the reasoning behind your algorithm.

Power Lists should be able to print ([],[1],[2], [3], [1,2], [1,3], [2,1], [2,3], [3,1],[3,2])
NOTE: [1,2] DOES NOT EQUAL [2,1]

Solutions

Expert Solution

The power list basically means we need to take all different combinations of numbers in the list and also consider all its permutations. So we will divide the task into two parts, getting all different combinations of numbers from the list of integers and then taking all the different permutation of that combination.

//the basic idea of generating all the combinations is to initially take an empty list at first
//and then pushing the numbers one in a recursion and calling for the next recursion using backtracking

list curr;   //for storing the current list of interger
list number; //given list of numbers

//curr is initially empty

integer index = -1;

//we will declare a recursive function here which will be called for generating all the
//combinations

powerList(index) 
    if index == number.lenght //if the index value is equal to length of the given list
        return;       //close the function here. you have permuted every number in the list
   
    //permute all the possible arrangements for the curr list 
    permute(curr,0,curr.lenght)
   
    //now we will append the ith number from the given list and call the recursion again
    for i = index+1, i< number.lenght ,i++
        curr.push(number[i])
        powerList(i)
        curr.pop()
   
    return;

//we have used the permute function so we will have to define that too
//this function is used to generate all the permutation of the combinations generated
//The logic of this permute function is to print everything if the first and the last index are same
// if they are not same swap the first number with the ith number and again call permutation on the next index
permutation(list curr, integer s, integer e) 
    if s == e
    print curr
       return;

    for i = s i <= e i++ 
        swap(curr[s], curr[i])
        permutation(curr, s + 1, e)
        swap(curr[s], curr[i])
    
     return;

Related Solutions

Give a recursive algorithm to compute a list of all permutations of a given set S....
Give a recursive algorithm to compute a list of all permutations of a given set S. (That is, compute a list of all possible orderings of the elements of S. For example, permutations({1, 2, 3}) should return {〈1, 2, 3〉, 〈1, 3, 2〉, 〈2, 1, 3〉, 〈2, 3, 1〉, 〈3, 1, 2〉, 〈3, 2, 1〉}, in some order.) Prove your algorithm correct by induction.
Write a pseudo code for an O (n7log3n) algorithm. Please write in C++.
Write a pseudo code for an O (n7log3n) algorithm. Please write in C++.
1. Write an algorithm to calculate the Matrix multiplication (or write with pseudo code) 2. Write...
1. Write an algorithm to calculate the Matrix multiplication (or write with pseudo code) 2. Write an algorithm to calculate the recursive Matrix multiplication (or write with pseudo code) 3. Find the time complexity of your pseudo code and analyze the differences
Write an algorithm in pseudo code to find one element and delete it in a doubly...
Write an algorithm in pseudo code to find one element and delete it in a doubly linked list. Your algorithm will print the original list, request the user to put in an element to be deleted, then print the final list after the deletion is done. If the element doesn’t exist in the list, print "XXX is not in the list" where "XXX" should be the one you received from the user. Use the following as your test cases to...
Develop an algorithm for INSERTION SORT. Give the pseudo-code version. Convert your pseudo-code into a Java...
Develop an algorithm for INSERTION SORT. Give the pseudo-code version. Convert your pseudo-code into a Java program.
In pseudo-code, design an algorithm for finding baking a cake.
In pseudo-code, design an algorithm for finding baking a cake.
Given a BST and a sum, write pseudo code to determine if the tree has a...
Given a BST and a sum, write pseudo code to determine if the tree has a root- to-leaf path such that adding up all the values along the path equals the given sum. Given the below BST and sum = 49, the array is (8, 4, 10, 1, 0, 3, 9, 15, 16). Return true, as there exist a root-to-leaf path 8− > 10− > 15− > 16 which sum is 49.
Write the pseudo code for longest common subsequence algorithm in Java. Alg lCS( X , n,...
Write the pseudo code for longest common subsequence algorithm in Java. Alg lCS( X , n, Y, m) Input: String X of length n, String Y of length m Output: return the length of the longest common subsequence between X and Y
Consider the recursive formulation of the Binary Search algorithm. Given a sorted and non-decreasing list of...
Consider the recursive formulation of the Binary Search algorithm. Given a sorted and non-decreasing list of comparable objects L, and a new item x, find the location (index) of x or indicated that x is not in L. 5a) Give a recursive algorithm for binary search. No, while, for, repeat etc statements are allowed. Only if, if else and assignment statements are allowed. 5b) Write a difference equation for the running time of your Binary Search algorithm. Solve your equation...
Given a positive integer n, write a recursive algorithm that returns the number of the digits...
Given a positive integer n, write a recursive algorithm that returns the number of the digits in n. For example, if the given number n is 12345, the algorithm should return 5. What is the time complexity of your algorithm? use java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT