448,669 Members | 1,671 Online
Need help? Post your question and get tips & solutions from a community of 448,669 IT Pros & Developers. It's quick & easy.

# How to compute Permutation of 4P3?

 P: 17 Let's say i create a String array that store 4 Alphabets {"A","B","C","D"} How can i get the result if i need permutation of 4P3 and 4P2? I had refer to many examples from the internet, but those examples cannot compute n selection from m elements. They only able to computer permutation of m elements without selection e.g. 4P4. Hence i need guideline in how to compute this kind of permutation. If i use manual calculation of 4P3 formula i get following result: A B C A B D A C B A C D A D B A D C B A C B A D B C A B C D B D A B D C C A B C A D C B A C B D C D A C D B D A B D A C D B A D B C D C A D C B If i use manual calculation of 4P2 formula i get following result: A B B A A C C A A D D A B C C B B D D B C D D C I need help in how to create an algorithm that can perform permutation of n selection from m elements. Any enlighten/help will be appreciate. Mar 18 '08 #1
7 Replies

 100+ P: 539 Maybe this is what you are looking for.... Correct me if im wrong... Sukatoa Mar 18 '08 #2

 P: 17 I'm not sure is this the one i searching for because after compile and run that example i get following error message: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at Permutation.main(Permutation.java:20) Is there any way to fix it so i able to run and view the output? Expand|Select|Wrap|Line Numbers /*************************************************************************  *  Compilation:  javac Permutation.java  *  Execution:    java Permutation N  *    *  Prints a pseudorandom permution of the integers 0 through N.  *  *    % java Shuffle 6  *    5 0 2 3 1 4   *    . * . . . .   *    . . . . * .   *    . . * . . .   *    . . . * . .   *    . . . . . *   *    * . . . . .   *  *************************************************************************/   public class Permutation {     public static void main(String[] args) {        int N = Integer.parseInt(args[0]); <= THIS LINE CONTAIN ERROR       int[] a = new int[N];         // insert integers 0..N-1       for (int i = 0; i < N; i++)          a[i] = i;         // shuffle       for (int i = 0; i < N; i++) {          int r = (int) (Math.random() * (i+1));     // int between 0 and i          int swap = a[r];          a[r] = a[i];          a[i] = swap;       }      // print permutation    for (int i = 0; i < N; i++)       System.out.print(a[i] + " ");    System.out.println("");      // print checkerboard visualization    for (int i = 0; i < N; i++) {       for (int j = 0; j < N; j++) {          if (a[j] == i) System.out.print("* ");          else           System.out.print(". ");       }       System.out.println("");    }      } }   Mar 18 '08 #3

 100+ P: 539 I'm not sure is this the one i searching for because after compile and run that example i get following error message: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at Permutation.main(Permutation.java:20) Is there any way to fix it so i able to run and view the output? Expand|Select|Wrap|Line Numbers /*************************************************************************  *  Compilation:  javac Permutation.java  *  Execution:    java Permutation N  *    *  Prints a pseudorandom permution of the integers 0 through N.  *  *    % java Shuffle 6  *    5 0 2 3 1 4   *    . * . . . .   *    . . . . * .   *    . . * . . .   *    . . . * . .   *    . . . . . *   *    * . . . . .   *  *************************************************************************/   public class Permutation {     public static void main(String[] args) {        int N = Integer.parseInt(args[0]); <= THIS LINE CONTAIN ERROR       int[] a = new int[N];         // insert integers 0..N-1       for (int i = 0; i < N; i++)          a[i] = i;         // shuffle       for (int i = 0; i < N; i++) {          int r = (int) (Math.random() * (i+1));     // int between 0 and i          int swap = a[r];          a[r] = a[i];          a[i] = swap;       }      // print permutation    for (int i = 0; i < N; i++)       System.out.print(a[i] + " ");    System.out.println("");      // print checkerboard visualization    for (int i = 0; i < N; i++) {       for (int j = 0; j < N; j++) {          if (a[j] == i) System.out.print("* ");          else           System.out.print(". ");       }       System.out.println("");    }      } }   have you tried to run the code like this: Expand|Select|Wrap|Line Numbers java Permutation 6 ? for example? It needs a value that must store in String args.... Or if you don't like that kind of initializing value, change int N = Integer.parseInt(args[0]); to int N = 6; I've not tried to compile that code.... If the problem persist, i will test it.... Update us... sukatoa.... Mar 19 '08 #4

 Expert 2.5K+ P: 3,652 That program looks like it will make a permutation of all N numbers entered, so in the OP's language, it will produce 4P4, or 6P6, or xPx. You might get what you need using some sort of container, like an ArrayList. When you pick a letter to include in the combination, remove it from the ArrayList. Get 3 letters and do each combination with those 3 letters. Then get a different set of 3 letters and repeat. Note that there are only a few ways of selecting 3 letters from "a", "b", "c", and "d": Expand|Select|Wrap|Line Numbers abc abd acd bcd excluding repeats like acb (in other words, the selection ignores order). Mar 19 '08 #5

 Expert 10K+ P: 11,448 Note that in order to generate nPm (m <= n) you only have to generate nCm and generate mPm for each generated combination. Generating nCm is much easier than generating nPm directly and generating mPm is solved already. kind regards, Jos Mar 19 '08 #6

 P: 14 just so happens i did methods/functions which compute factorial,permutation and combinations of numbers using recursion.i hope they help you since your problem is inline with them. here they are: well good luck,i will try and figure out how to help you print the permutations. Mar 23 '08 #7

 P: 17 just so happens i did methods/functions which compute factorial,permutation and combinations of numbers using recursion.i hope they help you since your problem is inline with them. here they are: well good luck,i will try and figure out how to help you print the permutations. Thanks. I already found a way to compute out permutation with allow repeats. Mar 24 '08 #8