By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,669 Members | 1,671 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
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
  1. /*************************************************************************
  2.  *  Compilation:  javac Permutation.java
  3.  *  Execution:    java Permutation N
  4.  *  
  5.  *  Prints a pseudorandom permution of the integers 0 through N.
  6.  *
  7.  *    % java Shuffle 6
  8.  *    5 0 2 3 1 4 
  9.  *    . * . . . . 
  10.  *    . . . . * . 
  11.  *    . . * . . . 
  12.  *    . . . * . . 
  13.  *    . . . . . * 
  14.  *    * . . . . . 
  15.  *
  16.  *************************************************************************/
  17.  
  18. public class Permutation { 
  19.    public static void main(String[] args) { 
  20.       int N = Integer.parseInt(args[0]); <= THIS LINE CONTAIN ERROR
  21.       int[] a = new int[N];
  22.  
  23.       // insert integers 0..N-1
  24.       for (int i = 0; i < N; i++)
  25.          a[i] = i;
  26.  
  27.       // shuffle
  28.       for (int i = 0; i < N; i++) {
  29.          int r = (int) (Math.random() * (i+1));     // int between 0 and i
  30.          int swap = a[r];
  31.          a[r] = a[i];
  32.          a[i] = swap;
  33.       }
  34.  
  35.    // print permutation
  36.    for (int i = 0; i < N; i++)
  37.       System.out.print(a[i] + " ");
  38.    System.out.println("");
  39.  
  40.    // print checkerboard visualization
  41.    for (int i = 0; i < N; i++) {
  42.       for (int j = 0; j < N; j++) {
  43.          if (a[j] == i) System.out.print("* ");
  44.          else           System.out.print(". ");
  45.       }
  46.       System.out.println("");
  47.    }
  48.  
  49.    }
  50. }
  51.  
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
  1. /*************************************************************************
  2.  *  Compilation:  javac Permutation.java
  3.  *  Execution:    java Permutation N
  4.  *  
  5.  *  Prints a pseudorandom permution of the integers 0 through N.
  6.  *
  7.  *    % java Shuffle 6
  8.  *    5 0 2 3 1 4 
  9.  *    . * . . . . 
  10.  *    . . . . * . 
  11.  *    . . * . . . 
  12.  *    . . . * . . 
  13.  *    . . . . . * 
  14.  *    * . . . . . 
  15.  *
  16.  *************************************************************************/
  17.  
  18. public class Permutation { 
  19.    public static void main(String[] args) { 
  20.       int N = Integer.parseInt(args[0]); <= THIS LINE CONTAIN ERROR
  21.       int[] a = new int[N];
  22.  
  23.       // insert integers 0..N-1
  24.       for (int i = 0; i < N; i++)
  25.          a[i] = i;
  26.  
  27.       // shuffle
  28.       for (int i = 0; i < N; i++) {
  29.          int r = (int) (Math.random() * (i+1));     // int between 0 and i
  30.          int swap = a[r];
  31.          a[r] = a[i];
  32.          a[i] = swap;
  33.       }
  34.  
  35.    // print permutation
  36.    for (int i = 0; i < N; i++)
  37.       System.out.print(a[i] + " ");
  38.    System.out.println("");
  39.  
  40.    // print checkerboard visualization
  41.    for (int i = 0; i < N; i++) {
  42.       for (int j = 0; j < N; j++) {
  43.          if (a[j] == i) System.out.print("* ");
  44.          else           System.out.print(". ");
  45.       }
  46.       System.out.println("");
  47.    }
  48.  
  49.    }
  50. }
  51.  
have you tried to run the code like this:
Expand|Select|Wrap|Line Numbers
  1. 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

Ganon11
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
  1. abc
  2. abd
  3. acd
  4. 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:


<Code removed by MODERATOR. Please read our posting guidelines forr our policy on simply posting answers.>

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:


<Code removed by MODERATOR. Please read our posting guidelines forr our policy on simply posting answers.>

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

Post your reply

Sign in to post your reply or Sign up for a free account.