Algorithm for generating permutations without repetition

Hy,
I want to generate all the permutations for a list of n objects and I do that by making a tree having the children the elements in the list, and the children for the first node the elements in the list except that node etc.. After that I make a depth-first search and make a list with all the paths to the nodes situated at a certain level p. The algorithm works fine if n = 10 and p =3 for example because there are 10!/7! paths generated. Unfortunately the list of objects can have 30 elements and the level p can be 15.. so the algorithm doesn't help me in this case. I want to ask if anyone has any ideas about how can the algorithm be improved to work better (less than 1 minute) for the later case.
Thanks

bluedragonfly wrote:
I want to know if there is another way to do things..and of course not the impossibleThe only thing you have asked for is 2.03e20 permutations. We don't know how you intend to process the permutations and even if we could guess what you are doing then just processing 2.03e20 anythings is just about impossible.
So, how do we know "if there is another way to do things" ?

Similar Messages

  • Collections.shuffle(list) without repetition.

    Hello every one,
    I am new to Collections. I have an String ArrayList having 3 strings in it. [A, B, C]. Now all i want is 6 permutations.
    [A, B, C]
    [A, C, B]
    [B, A, C]
    [B, C, A]
    [C, A, B]
    [C, B, A]
    Here is the simple code i have written. I hard coded the number of permutations as i know the value to make it simple.
    The problem is in those 6 permutations, few are being repeated which I dont want
    Please help me out with this problem.
    import java.util.*;
    import java.util.Collections;
    import java.util.List;
    import java.util.Random;
    public class sample {
         public static void main(String args[]) {
              ArrayList<String> list = new ArrayList<String>();
              list.add("A");
              list.add("B");
              list.add("C");
              int permutations = 6;
              for ( int i = 0 ; i < permutations; i++)
    Collections.shuffle(list);
              System.out.println(list.toString());
    Thanks in advance
    aady

    hi,
    may be this can help you. i found this code in [http://compsci.ca/v3/viewtopic.php?t=5214|http://compsci.ca/v3/viewtopic.php?t=5214] under the user zylum.
    the code is used for integers. but i managed to change is to Strings too and it shuffles without REPETITION. :) so i hope this helps:
    int[] numbers = new int[52];
    int randNumbers = new int[22];
    for (int i = 0; i < 52; i ++) numbers[i] = i;
    for (int i = 0; i < 52; i ++) {
        int idx = Math.random()*52; //i forgot how to do random in java so im using the action script way which is prolly the same way
        int temp = numbers[idx];
        numbers[idx] = numbers;
    numbers[i] = temp;
    for (int i = 0; i < 22; i ++) randNumbers[i] = numbers[i];
    Edited by: djmlog103 on Apr 2, 2008 9:53 AM
    Edited by: djmlog103 on Apr 2, 2008 9:54 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Generate Permutations of array recurssive function.

    Producing consecutive permutations.Need to develop a method that lists one by one all permutations of the numbers 1, 2, �, n (n is a positive integer).
    (a) Recursive method . Given a verbal description of the algorithm listing all permutations one by one, you are supposed to develop a recursive method with the following header:
    public static boolean nextPermutation(int[] array)
    The method receives an integer array parameter which is a permutation of integers 1, 2, �, n. If there is �next� permutation to the permutation represented by the array, then the method returns true and the array is changed so that it represents the �next� permutation. If there is no �next� permutation, the method returns false and does not change the array.
    Here is a verbal description of the recursive algorithm you need to implement:
    1. The first permutation is the permutation represented by the sequence (1, 2, �, n).
    2. The last permutation is the permutation represented by the sequence (n, �, 2, 1).
    3. If n a ,...,a 1 is an arbitrary permutation, then the �next� permutation is produced by
    the following procedure:
    (i) If the maximal element of the array (which is n) is not in the first position of the array, say i n = a , where i > 1, then just swap i a and i-1 a . This will give you the �next� permutation in this case.
    (ii) If the maximal element of the array is in the first position, so 1 n = a , then to find
    the �next� permutation to the permutation ( ,..., ) 1 n a a , first find the �next�
    permutation to ( ,..., ) 2 n a a , and then add 1 a to the end of thus obtained array of (n-1) elements.
    (iii) Consecutively applying this algorithm to permutations starting from (1, 2, �, n),you will eventually list all n! possible permutations. The last one will be (n, �, 2, 1).For example, below is the sequence of permutations for n = 3 , listed by the described algorithm:
    (0 1 2) ; (0 2 1) ; (2 0 1) ; (1 0 2) ; (1 2 0) ; (2 1 0)
    Please help...i have done the iterative one..but unable to figure out the recursive..
    Thank you in advance..
    Please help

    Thanks for your reply...it would be great if a code is provided..i am try to do but this not working here is my code
    * Permutations.java
    * Created on April 25, 2006, 12:42 PM
    package javaapplication1;
    * @author ismail
    public class Permutations {
        // print N! permutation of the elements of array a (not in order)
        public static boolean nextPermutation(int a[]) {
            int N = a.length;
            int[] t = new int[N];
          /* for (int i = 0; i < N; i++)
               t[i] = a;*/
    return(perm2(a, N));
    private static boolean perm2(int [] a, int n) {
    int i=n-1;
    // start from last but 2
    for (; i>0; i--) if (a[i]<a[i+1]) break;
    if (i <0) return false;
    for (int j = 0; j < n; j++) {
    swap(a, j, n-1);
    perm2(a, n-1);
    // swap(a, j, n-1);
    return true;
    // swap the characters at indices i and j
    private static void swap(int[] a, int i, int j) {
    int c;
    c = a[i]; a[i] = a[j]; a[j] = c;
    private static final int num = 3; // number of elements to permutate
    private static int [] a = new int[num]; // permutations array
    private static int qkCounter = 0; // Counter for Quick sort
    private static int mrgCounter = 0; // Counter for Merge sort
    private static int qkSum = 0; // Total sum for Quick sort
    private static int mrgSum = 0; // Total sum for Merge sort
    public static void main(String s[]){
    int count=1;
    int [] qkArray = new int[num]; // Quick Sort array
    int [] mrgArray = new int[num]; // Merge Sort array
    int avgQuick = 0;
    int avgMerge = 0;
    // Initialize array
    for(int i=0;i<num;i++){
    a[i]=i;
    // call nextPermutation to generate possible permutations - iterative method
    for (int j=0; nextPermutation(a);j++){
    count++; // Count number of records
    // print the permutation array
    for(int i=0;i<num;i++){
    System.out.print(a[i]);
    not working means-- it keeps on printing 012

  • Need An Algorithm That Generates Random Numbers

    Hi, I understand that there is a built-in random number generator. But, I need an algorithm that generator random numbers. Is there one available? Thanks.

    Hi, is the "Seminumerical Algorithms" a book?What
    the numbers 3141592621 and 907633385 stand for?The first is PI without a decimal... the second I
    don't know, it may as well be randomly picked.As far as I know the numbers aren't randomly picked. You want the distribution to be good, and the length of the serie should also be long. Some numbers causes the cycle to be short, or the distribution to be bad. I think there are articles that lists 'good' numbers.
    /Kaj

  • Announce: Generic Algorithms for Java, 0.3 release

    The latest release of jga: Generic Algorithms for Java is now available at
    http://jga.sf.net/
    Included in this release are a collection of algorithms adapted from STL that operate over generic collections and iterations. What's included are the algorithms that do not attempt to modify the collection/iteration that they're given: in other words, all of the find/search/count algorithms. Also included are the complete set of functors and predicates that were present in the last release, with a few modifications for easier use.
    The library is released under the LGPL, and includes the source for the library, the test framework, and a non-genericized version of the test framework that verifies that the library may be used without having the generic environment available.

    A (similar, it seems) parameterized collections library for JSR-14 Java.I had looked at JUtil some time ago, and I'm not sure of the similarity: JUtil looks to provide a number of interesting (if somewhat specialized) collections. JGA provides functors & predicates, and STL-ish algorithms that operate on collections and iterators. Looks to me to be more complimentary than similar.
    Mr. Ananian/Astaire's site is also the source of SinjDoc, the javadoc tool that I used to generate the javadoc at jga.sf.net, and with which I replaced the munge4doc.pl and mungedoclist.pl scripts that I had previously used and posted on this forum.

  • Name TCURC is not in the namespace for generated BI meta objects

    Hi,
    When I try to load data into infocube, I get a short dump during the DTP load with the below message
    UNCAUGHT_EXCEPTION
    Exception: CX_RSR_X_MESSAGE
    Upon analysing the dump analysis I found the below system fields
    SY-MSGTY E
    SY-MSGID R7
    SY-MSGNO 019
    SY-MSGV1 TCURC
    which gives the message text "Name TCURC is not in the namespace for generated BI meta objects"
    If I remove the mapping with Amount KF in the TRFN and load data it works fine without error but on mapping the KF with currency unit I get this error.It is something related to Currency but I'm unable to figure out the problem.
    Any suggestions on what could be the problem?
    Thanks.

    Is this the real error?
    Please, can you check the currency conversion with the function module CONVERT_TO_LOCAL_CURRENCY?
    Have you customize the currency-tables? Use the functions RSA1 -> source system -> context menu -> global settings + exchange rate
    Sven

  • Tcode for Generating ARE-2 document

    Is there any standard for generating ARE-2 Document under export,
    if not what is the round about solution for the same?
    regards
    shilpa

    As I know, Form ARE 2 is just an combined application for removal of goods for export under claim for rebate of duty paid on excisable materials used in the manufacture and packing of such goods and removal of dutiable excisable goods for export under claim for rebate of finished stage Central Excise Duty or under bond without payment of finished stage Central Excise Duty leviable on export goods.
    There is no standard process to handle ARE 2 in SAP.
    Where, rightly mention in one of the previous post that you can very much follow process flow for ARE 1  in SAP.
    So, there will not be any combine process for
    - export under claim for rebate
    - export  under bond without payment
    In SAP, you have to dealt them separately in ARE 1. 
    For further reference/understanding, refer following SDN threads links for:
    - Export Against Under Claim of Rebate
    - Export under No bond?
    Take use of following TCodes:
    TCode
    Decs
    J1IA101
    - Excise Bonding ARE-1 procedure
    J1IA102
    - Excise Bonding ARE-1 procedure
    J1IA103
    - Excise Bonding ARE-1 procedure
    J1IA104
    - Excise Bonding ARE-1 procedure
    J1IANX18
    - Pro Forma of Running Bond Account
    J1IBN01
    - Create Excise Bond
    J1IBN02
    - Change Excise Bond
    J1IBN03
    - Display Excise Bond
    J1IBN04
    - Cancel Excise Bond
    J1IBN05
    - Close Excise Bond
    J1IBONSUM
    - Bond Summary Report
    Regards
    JP

  • Help me to get the algorithm for this assingment.

    I have tried to draw the x-y axis and the rectangle. I hava a problem to draw the rectangle inside of the lablled grid.
    NEED EVERYONE'S HELP HERE!
    Assignment #4
    Goal:
    �     This lab gives students more experience in
    �     developing proper algorithms for real-life problems;
    �     using program constructs � sequential, selection, and repetition.
    Algorithm. An algorithm is a description of steps to solve a problem that is unambiguous, executable, and terminating. In order to solve a problem by computer, you must know an algorithm for finding a solution.
    Problem Description
    Develop an algorithm and then write a java program that can accept commands from the user to draw a rectangle. The program should accept the xy-coordinate pair of the upper-left corner, and the width and height of a rectangle. It should then draw the rectangle pattern on a 40 X 20 labelled grid. For example, if the upper-left corner is (5, 10) with a width of 10 and height of 5, then the program would display:
    ^
    20+
    |     
    |
    |
    |
    |     
    15+
    |
    |
    |
    |     
    10+
    | ++++++++++
    | ++++++++++
    | ++++++++++
    | ++++++++++
    5+ ++++++++++
    |
    |
    |
    |     
    ________________________________>
    0 5 10 15 20 25 30 35 40     
         Upper Left (5,10) width:10, Height: 5
    NOTE:
    �     Your display should include the labelled axis, as shown on the previous page, and also the lines that are drawn horizontally across the page.
    �     Make sure the rectangle will fit on the 40X20 grid.
    �     Appropriate error messages must be displayed for invalid data provided by the user.

    If you format your code, it looks like thispublic class Rectangle {
      public static void main(String[] args) {
        int maxYAxisPoint = 20;
        int maxXAxisPoint = 40;
        int xCoodinate = 5;
        int yCoodinate = 10;
        int width = 5;
        int height = 10;
        int leftLowPoint;
        System.out.println("This program draw a rectangle pattern on a 40*20 labelled grid.");
        System.out.println("Please enter the x-coordinate of the upper left point: ");
        xCoodinate = MyIO.readLineInt();
        System.out.println("Please enter the y-coordinate of the upper left point: ");
        yCoodinate = MyIO.readLineInt();
        System.out.println("Please enter the width of the rectangle: ");
        width = MyIO.readLineInt();
        System.out.print("Enter the height of the rectangle:");
        height = MyIO.readLineInt();
        if (yCoodinate < maxYAxisPoint && yCoodinate + width < 40 ||
            xCoodinate + width < maxXAxisPoint &&
            xCoodinate + height < maxYAxisPoint) {
          System.out.println(" ^");
          for (maxYAxisPoint = 20; maxYAxisPoint >= 0; maxYAxisPoint--) {
            if ( (maxYAxisPoint % 5) == 0) {
              System.out.println(maxYAxisPoint + "+");
              for (leftLowPoint = (maxYAxisPoint - height) + 1;
                   leftLowPoint <= yCoodinate; leftLowPoint--) {
                System.out.println("+");
              //Draw the x-axis
              for (maxXAxisPoint = 0; maxXAxisPoint <= 40; maxXAxisPoint++) {
                System.out.print("+____");
                System.out.println("+>");
              for (maxXAxisPoint = 0; maxXAxisPoint <= 40; maxXAxisPoint++) {
                System.out.print(maxXAxisPoint + " ");
            } else {
              System.out.print(" | \n" + " |\n" + " |\n" + " |");
        } else {
          System.out.print("You entered a wrong x-y coordinate of the rectangle,\n the rectangle is not in the labelled grid.");
        System.out.print("Upper Left(" + xCoodinate + "," + yCoodinate + ")");
        System.out.print("width:" + width);
        System.out.println("height: " + height);
    }You have nested your loops. I'm pretty sure you don't want to do this. Here's an example of the difference. First a nested looppublic class Test2 {
      public static void main(String[] args) {
        for (int i=0; i<3; i++) {
          System.out.println("i="+i);
          for (int j=0; j<4; j++) {
            System.out.println("  j="+j);
    Outputs this
    i=0
      j=0
      j=1
      j=2
      j=3
    i=1
      j=0
      j=1
      j=2
      j=3
    i=2
      j=0
      j=1
      j=2
      j=3Now an unnested looppublic class Test2 {
      public static void main(String[] args) {
        for (int i=0; i<3; i++) {
          System.out.println("i="+i);
        for (int j=0; j<4; j++) {
          System.out.println("  j="+j);
    outputs this
    i=0
    i=1
    i=2
      j=0
      j=1
      j=2
      j=3This means you are printing the x-axis for every point on the graph.

  • Generate XML without header tags in Oracle 9i Release1

    I am a novoice to Oracle. I am using Oracle 9i Release 1.
    I have a table "Attribution" with the following structure.
    SQL> Desc attribution
    Name Null? Type
    REPORT_ID NUMBER
    LABEL VARCHAR2(20)
    VALUE NUMBER(25,10)
    COLOR VARCHAR2(10)
    The inserted values in this table are:
    SQL> select * from attribution;
    REPORT_ID LABEL VALUE COLOR
    24 Allocation .025172879 996699
    24 Selection -.03198432 FF9933
    24 Timing .000016592 8B5A13
    24 Interaction -.0148997 99583D
    24 Derivative Effect -.00026146 CDCD00
    24 Active Return -.02195601 638B45
    24 Active Risk .031745116 800000
    I want to generate an xml without the XML declaration row i.e. <?xml version="1.0"?>
    Also I do not want the <Rowset> tags in my xml.
    My generated XML should be like:
    <set label="Allocation" value="0.4649000000" color="996699"/>
    <set label="Selection" value="-4.3508000000" color="FF9933"/>
    <set label="Timing" value="-0.0022000000" color="8B5A13"/>
    <set label="Interaction" value="-0.4669000000" color="99583D"/>
    <set label="Derivative Effect" value="-0.0184000000" color="CDCD00"/>
    <set label="Active Return" value="-4.3733000000" color="638B45"/>
    <set label="Active Risk" value="1.9901704500" color="800000"/>
    where the value attribute should be equal to values*100.
    Please help me in generating this xml.
    Thanks in Advance
    Jaspreet

    Thanks for your reply.
    Essentially my requirement was to concatinate three sets of xml strings. The application which is parsing my xml requires the XML in the following format:
    XMLStringA + GeneratedXMLfromMyQuery + XMLStringB
    where XMLStringA and XMLStringB are static XML strings and do not change.
    I have generated GeneratedXMLfromMyQuery as from the following query.
    Declare
    qryctx dbms_xmlgen.ctxhandle;
    GeneratedXMLfromMyQuery CLOB;
    Begin
    qryctx := dbms_xmlgen.newcontext('SELECT label "@label", value*100 "@value", color "@color" FROM Attribution order by rownum');
    dbms_xmlgen.setrowtag(qryctx,'set');
    GeneratedXMLfromMyQuery := dbms_xmlgen.getxml(qryctx);
    I can do substring operations on GeneratedXMLfromMyQuery to fetch my required XML and then concatinate it with XMLStringA and XMLStringB,
    XMLOUTPUT := XMLStringA || GeneratedXMLfromMyQuery || XMLStringB ;
    but was keen to know if there is any way by which I can generate GeneratedXMLfromMyQuery without the <?xml version="1.0"?> row and <ROWSET> tags??

  • APiI for generating Quote report as Pdf  in oracle iStore

    hi,
    Any one help me on which API is used for generating quote report in oracle istore...

    Are u sure about  the report version.
    Please do upgrade  while  even in oracle  they almost stopped for new version -Developer suite

  • Goods Receive for Outbound Delivery Without Reference using MIGO

    Dear Colleague ...
    We might need your insight on the case below related to Goods Receive of Outbound Delivery without Reference using MIGO:
    (1) In the current SAP Enjoy screen of MIGO, we are allowed to select the reference document (e.g. = PO, Material Document, Inbound Document, Outbound Document, etc.)
    (2) We did 3 (three) kind of test set: GR for Inbound Delivery, GR for Outbound Delivery with STO and GR for Outbound Delivery without reference.
    (3) The first 2 (two) cases work just find with MIGO.  We believe it is because the receiving plant is pretty much determined in the reference document, which is PO (case 1) or STO (case 3)
    (4) However, the last case came up with the error message "Goods receipt not possible for delivery 8000610236: error code 5".  We believe that this is the standard design of SAP, that the receiving plant is required and it is not defined explicitly in the delivery document.
    (5) Moreover, we also believe that that's the reason why SAP (via the Logistic Execution module) provide the Handling Unit and Shipment, which can be used to perform such receive function, which will imply the MM posting (goods issue and goods receive) IF there is stock transfer involved.
    Appreciate for any thoughts or input on the above.  Many thanks,
    Alvon Sibarani

    use transaction mb0a it will work for the scenario.

  • Have CS5 and CS6.  Need to reduce size of file from 6Mb to 2 Mb for contest purposes without losing original

    Have CS5 and CS6.  Need to reduce size of file from 6Mb to 2 Mb for contest purposes without losing original

    Just save the document to a new jpeg file name using a lower quality setting or re-size the image down in  size the save a high quality smaller new jpeg image.

  • Production Order for a material without BOM and Routing

    Is it possible to create a production order for a given material without routing and BOM, I know we can
    create a production order for a material without a BOM (with a default routing) just wanted to check with
    the above criteria
    Thanks in Advance!!

    Members:
    Thank you for your valuable replies and time
    Santosh:
    Tried what you have mentioned, but system still picks up a default routing
    Kaushik:
    Maintained Default values in OPJG for the given order type, but system still picks up
    default routing
    Mangalraj:
    have no issue in creating a production order without a BOM, but I am trying to see
    is there a way where I can create a production order without a BOM and routing (not
    even default operation )
    Any suggestions on how to create a production order for a material without a BOM
    and routing (not even default one )

  • Can we combine Query for cancelled requisitions and query for internal requisitions without internal sales order into a single query

    Hi All,
    Greetings.
    I have two queries namely,
    1.Query for cancelled requisitions and
    2.Query for Internal Requisitions without Internal Sales Orders.
    I was on a task to combine those two queries..
    Can we do that? if so, please help me do that..
    Thanks in Advance,
    Bhaskar.

    Hi All,
    Greetings.
    I have two queries namely,
    1.Query for cancelled requisitions and
    2.Query for Internal Requisitions without Internal Sales Orders.
    I was on a task to combine those two queries..
    Can we do that? if so, please help me do that..
    Thanks in Advance,
    Bhaskar.

  • Can I use the same iTunes account for 2 iPads without having the same apps on both?

    Can I use the same iTunes account for 2 iPads without having the same apps on both? Everytime I try to sync my new iPad, it syncs all the apps that are on my account. If I can do this, can someone tell me how please???

    Yes you can, easily.  When you plug the first pad in to sync, note the name of the pad on the last pane.  Set up for that device whatever you want to sync.  When you are done, plug the second pad in.  Make sure it has a different name on the left pane.   Then sync to that pad whatever you want.  I tunes will remember what you did on each pad, and will start from there the next time you plug in.

Maybe you are looking for