Thread: How to reduce no of permutations ?

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    45

    How to reduce no of permutations ?

    I am using GSL function to generate permutations, it works fine and result in 4! permutations.

    Code:
    gsl_permutation * p = gsl_permutation_calloc (4);
    gsl_permutation_init (p);
    do {gsl_permutation_fprintf (stdout, p, " %u");
     printf("\n");
    }
    while (gsl_permutation_next(p) == GSL_SUCCESS);
    return 0;
    Output is

    Code:
    0 1 2 3
    1 0 2 3
    ...... n!
    


    My problem is that i want to reduce the no of permutations on the bases of two parallel arrays.

    Code:
    int value=  {0,1,2,3};
    int id  = {1,1,3,3};
    What i want before generating permutations of "value", it will reduce the no of elements as:

    Code:
     
    Psudocode
    If id of value element==id of next value element join that elements like below newValue= {0_1,2_3}so total no of elements are 2 I will then pass this 2 to generate permutation result should be 2! and after permutation if one of the permutation should be {1,0} it will re-split it into {2,3,0,1}
    I don't know how i will do it into C. Need some help plz..Thanks
    Last edited by gevni; 04-04-2013 at 07:44 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm not clear on what you want, but what I do with things like this, is work them through (sometimes several times), by hand (paper and pen), with simple examples of the input.

    And watch the patterns I use to solve it. Sooner or later, the "light" goes on, and I can reduce what I'm doing (perhaps intuitively), into small steps that can easily be put into pseudo code and then to actual code.

    What you've described above, is way too high-intelligence (if I understand it right), for a small-minded computer to handle. Study it more, and break it down.

    Show a complete example, step by step, and help will be easier to find, as well.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    45
    Ok i think i will ask the specific thing
    How can I perform join and split functions on array elements?
    for example
    let say again
    Code:
    struc test{
    int value;
    int id
    } ;
    
    test data[4]={{0,1},{1,1}, {2,3}, {3,3}};
    
    // I want to join the elements of "value"  whoz value of "id" are same.
    In my case 0,1 from "value" has the same "id" value that is 1,1.and 2,3 from "value" has same "id" value 3,3. 
    So it will join {01,23}
    And if i want to perform split function it will give me back{0,1,2,3}
    Is it possible in C. As i did it in perl script, but i don't know how to do it in C

    Quote Originally Posted by Adak View Post
    I'm not clear on what you want, but what I do with things like this, is work them through (sometimes several times), by hand (paper and pen), with simple examples of the input.

    And watch the patterns I use to solve it. Sooner or later, the "light" goes on, and I can reduce what I'm doing (perhaps intuitively), into small steps that can easily be put into pseudo code and then to actual code.

    What you've described above, is way too high-intelligence (if I understand it right), for a small-minded computer to handle. Study it more, and break it down.

    Show a complete example, step by step, and help will be easier to find, as well.
    Last edited by gevni; 04-05-2013 at 07:48 AM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by gevni View Post
    Ok i think i will ask the specific thing
    How can I perform join and split functions on array elements?
    for example
    let say again
    Code:
    struc test{
    int value;
    int id
    } ;
    
    test data[4]={{0,1},{1,1}, {2,3}, {3,3}};
    
    // I want to join the elements of "value"  whoz value of "id" are same.
    In my case 0,1 from "value" has the same "id" value that is 1,1.and 2,3 from "value" has same "id" value 3,3. 
    So it will join {01,23}
    And if i want to perform split function it will give me back{0,1,2,3}
    Is it possible in C. As i did it in perl script, but i don't know how to do it in C
    I have never heard of a "join" or "split" function, like you are describing in C, but of course, you can join and split anything you want, in C - but you need to "roll your own" code for it.

    You have an array of structs. value is a struct member:
    data[i].value

    as is id:
    data[i].id

    Where i is the array index.

    And no, you can't add in other numbers or digits, into the array of structs.

    It sounds like you want to use an array of structs, and then could use an int array to hold these ints you want to join or split off.

    That's trivial, and tell me you need to run through the C tutorial linked at the top of this page. It's really excellent.

    Code:
    int joined[dataNumber]={0};
    
    for(each element i in data[] - 1) {
    
       if(data[i].id == data[i+1].id ) {
          joined[j]=data[i].value
          joined[j+1]=data[i+1].value;
          ++j;
       }
    }
    The above is meant to put you on the right track, possibly, it's not code ready to run - pseudo code and code too. You'd need to set up the variables, initialize them, and then increment i.
    Last edited by Adak; 04-05-2013 at 09:57 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bison/Yacc shift/reduce reduce/reduce
    By AnthonyGould in forum Tech Board
    Replies: 0
    Last Post: 11-20-2009, 08:15 AM
  2. Reduce CPU usage
    By patrick22 in forum Windows Programming
    Replies: 9
    Last Post: 07-10-2009, 02:13 PM
  3. to reduce footprint
    By George2 in forum Tech Board
    Replies: 2
    Last Post: 12-19-2006, 01:31 AM
  4. Yacc: reduce/reduce
    By alphaoide in forum Tech Board
    Replies: 2
    Last Post: 04-27-2005, 08:03 PM
  5. reduce flickering..and more
    By lambs4 in forum Game Programming
    Replies: 7
    Last Post: 01-12-2002, 02:22 PM

Tags for this Thread