1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Permutation Algorithm

Discussion in 'PHP' started by XandroZ, Jan 12, 2009.

  1. #1
    I need help, an idea for a permutation algorithm.It is not a simple permutation algorithm because I have series(sequences).The series don't have the same length.
    I don't want permutations of elements from the same sequence.

    Example :

    KP,KT,KU,KL - sequence K
    TM,TT,TU,TG,TF,TK -sequence T
    PI,PIP,PIN,PAM,PAC -sequence P

    Needed results

    KP TM PI
    KP TM PIP
    KU TU PAC
    ............

    I don't want permutations of elements from the same sequence(serie):

    Example:
    KP KT TU - WRONG
    KP TT TU PIS - WRONG

    Thanks
     
    XandroZ, Jan 12, 2009 IP
  2. yoavmatchulsky

    yoavmatchulsky Member

    Messages:
    57
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #2
    You want every available permutations (4*6*6 in your example), or a single one?

    You could insert the values to an array of array:
    $values = array(
      array( 'KP', 'KT', 'KU', 'KL' ),
      array( 'TM', 'TT', 'TU', 'TG', 'TF', 'TK' ),
      array( 'PI', 'PIP', 'PIN', 'PAM', 'PAC')
    )
    
    PHP:
    and now use random numbers to select a single permutation, or loops to iterate on all the permutations:
    
    foreach ($values[0] as $v1)
    {
      foreach ($values[1] as $v2)
      {
        foreach ($values[2] as $v3)
        {
           echo $v1 . ', ' . $v2 . ', ' . $v3 . "\n<br />";
        }
      }
    }
    
    PHP:
     
    yoavmatchulsky, Jan 12, 2009 IP
  3. XandroZ

    XandroZ Peon

    Messages:
    395
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I want all the permutations , but in a permutation it will not be more than one element from the same sequence

    EXAMPLE - all are correct

    KP TM PI
    PI KP TM
    TM KP PI

    In your idea I will have only KP TM PI.
    This series are only an example. Thank you for the idea, but I can't use your example because the number of series and the length of series is variable n and for that I can't use n foreach (in the example are 3, but in fact is n) so I need something like a recurrent function.

    wrong:

    KP KT PI - 2 from the same sequence
     
    XandroZ, Jan 12, 2009 IP
  4. XandroZ

    XandroZ Peon

    Messages:
    395
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    So, basically I have :

    array[1](vector)*array[1](vector) = array[2] 2 dimensions
    array[2]* array[1] = array[3] 3 dimensions

    The result will be an array of n dimensions with m lines and k columns , where n,m,k are variables
     
    XandroZ, Jan 12, 2009 IP
  5. yoavmatchulsky

    yoavmatchulsky Member

    Messages:
    57
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #5
    this will give you a random permutation:

    <?php
    
    function get_item_from_series($values, $series)
    {
      if (empty($values[$series]))
      {
        return '';
      }
      
      $rand_index = rand(0, count($values[$series]) - 1);
      return $values[$series][$rand_index];
    }
    
    // assume your data is in the format of:
    $values = array (
      array('a','b','c'),
      array('d','e'),
      array('f','g'),
      array('z','y'),
      // so on..
    );
    
    $result_permutation = array();
    
    for ($i = 0; $i < count($values); ++$i)
    {
      $result_permutation[] = get_item_from_series($values, $i);
    }
    
    var_dump($result_permutation);
    
    ?>
    PHP:
    it can be easily tweaked so to produce all permutations.
    i think ;)
     
    yoavmatchulsky, Jan 12, 2009 IP