Generating all permutations with repetition

2019-06-13 19:16发布

问题:

How could we generate all possible permutations of n (given) distinct items taken r at a time where any item can be repeated any number of times?

Combinatorics tell me that there will be n^r of them, just wondering how to generate them with C++/python?

回答1:

Treat your permutation as a r-digit number in a n-based numerical system. Start with 000...0 and increase the 'number' by one: 0000, 0001, 0002, 000(r-1), 0010, 0011, ...

The code is quite simple.



回答2:

Here is a possible implementation in C++, along the lines of the standard library function std::next_permutation

//---------------------------------------------------------------------------
// Variations with repetition in lexicographic order
// k: length of alphabet (available symbols)
// n: number of places
// The number of possible variations (cardinality) is k^n (it's like counting)
// Sequence elements must be comparable and increaseable (operator<, operator++)
// The elements are associated to values 0÷(k-1), max=k-1
// The iterators are at least bidirectional and point to the type of 'max'
template <class Iter>
bool next_variation(Iter first, Iter last, const typename std::iterator_traits<Iter>::value_type max)
{
    if(first == last) return false; // empty sequence (n==0)

    Iter i(last); --i; // Point to the rightmost element
    // Check if I can just increase it
    if(*i < max) { ++(*i); return true; } // Increase this element and return

    // Find the rightmost element to increase
    while( i != first )
       {
        *i = 0; // reset the right-hand element
        --i; // point to the left adjacent
        if(*i < max) { ++(*i); return true; } // Increase this element and return
       }

    // If here all elements are the maximum symbol (max=k-1), so there are no more variations
    //for(i=first; i!=last; ++i) *i = 0; // Should reset to the lowest sequence (0)?
    return false;
} // 'next_variation'

And that's the usage:

std::vector<int> b(4,0); // four places initialized to symbol 0
do{
   for(std::vector<int>::const_iterator ib=b.begin(); ib!=b.end(); ++ib)
      {
       std::cout << std::to_string(*ib);
      }
   std::cout << '\n';
  }
while( next_variation(b.begin(), b.end(), 2) ); // use just 0-1-2 symbols


回答3:

Here's an example of @Inspired's method with n as the first three letters of the alphabet and r = 3:

alphabet = [ 'a', 'b', 'c' ]

def symbolic_increment( symbol, alphabet ):
    ## increment our "symbolic" number by 1
    symbol = list(symbol)
    ## we reverse the symbol to maintain the convention of having the LSD on the "right"
    symbol.reverse()
    place = 0;
    while place < len(symbol):
        if (alphabet.index(symbol[place])+1) < len(alphabet):
            symbol[place] = alphabet[alphabet.index(symbol[place])+1]
            break
        else:
            symbol[place] = alphabet[0];
            place+=1
    symbol.reverse()
    return ''.join(symbol)

permutations=[]
r=3
start_symbol = alphabet[0] * (r)
temp_symbol = alphabet[0] * (r)
while 1:
    ## keep incrementing the "symbolic number" until we get back to where we started
    permutations.append(temp_symbol)
    temp_symbol = symbolic_increment( temp_symbol, alphabet)
    if( temp_symbol == start_symbol ): break

You can also probably do it with itertools:

from itertools import product

r=3
for i in xrange(r-1):
    if (i==0):
        permutations = list(product(alphabet, alphabet))
    else:
        permutations = list(product(permutations, alphabet))
    permutations = [ ''.join(item) for item in permutations ]