Question

Q1. Consider the problem of generating all the possible permutations of length n. For example, the...

Q1. Consider the problem of generating all the possible permutations of length n. For example, the permutations of length 3 are: {1,2,3}, {2,1,3},{2,3,1}, {1,3,2}, {3,1,2}, {3,2,1}. In this question, you will provide:

b) Well documented pseudocode of a non-recursive algorithm that computes all the permutations of size n. The only data structure allowed is a stack (you may use maximum up to two stacks). Any other memory usage should be O(1). Calculate the time complexity of your algorithm using the big-Oh notation. Show all calculations

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

Note: Here i am Providing pseudocode of a non-recursive algorithm using Stack for generating all the possible permutations of length n and i am also provided java program for better understanding.

Copiable code:

Algorithm findPermutations(n)

Input: n is integer that represents length of the permutation

Output: Displaying all possible permutations

     startStr ← "";

     //create inital string of length n

     for i: 1 to n do

          startStr ← startStr+ "" + i

end for  

     // Create a stack using to store permutations

     Stack<String> stack;

     // Add first characeter of the startStr into stack

stack.push(String.valueOf(startStr.charAt(0)));    

     // Add all the characters into queue for permutations

     // do for every character of the specified string

     for i ← 1 to n do

          /* consider previously constructed stack

          * permutation one by one*/

          for x ← stack.size() – 1 to 0 do

               //remove current stack permutation

               //from the stack

               String str ← stack.remove(x);

               /*Insert next character of the specified string

               * in all possible positions of current stack

               * permutation. Then insert each of these

               * newly constructed string in the list*/

               for y ← 0 to str.length() do

                     /*Please note that String concatenation*/

                     stack.push(str.substring(0, y) +

                     startStr.charAt(i) +

                     str.substring(y));

               end for

          End for

     End for  

//Print All permutations of the string length n

     Print(stack)

Screenshots of the java program:

Sample output:

Code to copy: Generat_All_Permutations.java

import java.util.Scanner;
import java.util.Stack;

public class Generat_All_Permutations
{
   /*function name: permutations
   * parameters: n- size
   * non-recursive algorithm that computes all
   * the permutations of size n
   */
   public static void findPermutations(int n)
   {
       String startStr = "";
       //create inital string of length n
       for (int i = 1; i <= n; i++)
       {
           startStr += "" + i;
       }
       // create an empty Stack to store permutations
       // and initialize it with first character of the string
       Stack<String> stack = new Stack<>();
       stack.push(String.valueOf(startStr.charAt(0)));

       // do for every character of the specified string
       for (int i = 1; i < n; i++)
       {
           /* consider previously constructed stack
           * permutation one by one(we're iterating
           * backwards in the list to avoid
           * ConcurrentModificationException)*/
           for (int x = stack.size() - 1; x >= 0; x--)
           {
               //remove current stack permutation
               //from the stack
               String str = stack.remove(x);

               /*Insert next character of the specified string
               * in all possible positions of current stack
               * permutation. Then insert each of these
               * newly constructed string in the list*/
               for (int y = 0; y <= str.length(); y++)
               {
                   /*Please note that String concatenation*/
                   stack.push(str.substring(0, y) +
                           startStr.charAt(i) +
                           str.substring(y));
               }
           }
       }
       //call method to print permutations
       printPermutations(stack);
   }
   public static void printPermutations(Stack<String> stack)
   {
       // Print All permutations of the string length n
       while (!stack.isEmpty())
       {
           // remove current partial permutation from the stack
           String str = stack.pop();
           System.out.print("{");
           for (int k = 0; k < str.length(); k++)
           {
               if (k == str.length() - 1)
                   System.out.print(str.charAt(k) + "} ");
               else
                   System.out.print(str.charAt(k) + ",");
           }
       }
   }

   /* program to generate all permutations of a String
   * using single Stack and non-recursive
   */
   public static void main(String[] args)
   {
       //Scanner object to read N value
       Scanner sc=new Scanner(System.in);
       //Prompt n value
       System.out.print("Enter Length of the starting String: ");
       int n = sc.nextInt();//read n value
       System.out.println("\nAll permutations: ");
       //call method findPermutations to
       //print all permutations
       findPermutations(n);
   }
}

Add a comment
Know the answer?
Add Answer to:
Q1. Consider the problem of generating all the possible permutations of length n. For example, the...
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
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