How to compute Permutation of 4P3?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xirowei
    New Member
    • Jun 2007
    • 17

    How to compute Permutation of 4P3?

    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.
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    Maybe this is what you are looking for....

    Correct me if im wrong...
    Sukatoa

    Comment

    • xirowei
      New Member
      • Jun 2007
      • 17

      #3
      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.Array IndexOutOfBound sException: 0
      at Permutation.mai n(Permutation.j ava:20)

      Is there any way to fix it so i able to run and view the output?

      Code:
      /*************************************************************************
       *  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]); [B]<= THIS LINE CONTAIN ERROR[/B]
            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("");
         }
      
         }
      }

      Comment

      • sukatoa
        Contributor
        • Nov 2007
        • 539

        #4
        Originally posted by xirowei
        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.Array IndexOutOfBound sException: 0
        at Permutation.mai n(Permutation.j ava:20)

        Is there any way to fix it so i able to run and view the output?

        Code:
        /*************************************************************************
         *  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]); [B]<= THIS LINE CONTAIN ERROR[/B]
              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:
        Code:
        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.parseIn t(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....

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          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":

          Code:
          abc
          abd
          acd
          bcd
          excluding repeats like acb (in other words, the selection ignores order).

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            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

            Comment

            • Doegon
              New Member
              • Feb 2008
              • 14

              #7
              just so happens i did methods/functions which compute factorial,permu tation 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.

              Comment

              • xirowei
                New Member
                • Jun 2007
                • 17

                #8
                Originally posted by Doegon
                just so happens i did methods/functions which compute factorial,permu tation 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.
                Last edited by xirowei; Mar 24 '08, 09:35 AM. Reason: correct typo error

                Comment

                Working...