algorithm - All permutations of n numbers in depth first manner -


how can generate permutations (order matters) replacements , order matters set of n numbers, contain m elements in each combination?

i don't want use more memory, generate combination, want use it.

first, note if order matters , replacements allowed, have n options choose each element (m of those) - , amount of choices have remains constant.

this means have n*n*...*n = n^m possible combinations.

to generate them, can use recursion:

python code: (if don't know python, read pseudo code, it's pretty clear).

def generateall(numbers, m, currentpermutation=[]):     if m == 0:         dosomething(currentpermutation)         return     x in numbers:         currentpermutation.append(x)         generateall(numbers, m-1, currentpermutation)         currentpermutation.pop() 

for example, if define

def dosomething(l):     print str(l) 

and run

generateall([1,2,3], 4) 

output print combinations repalcements, order matters, of size 4 [1,2,3]:

[1, 1, 1, 1] [1, 1, 1, 2] [1, 1, 1, 3] [1, 1, 2, 1] [1, 1, 2, 2] [1, 1, 2, 3] [1, 1, 3, 1] [1, 1, 3, 2] [1, 1, 3, 3] [1, 2, 1, 1] [1, 2, 1, 2] [1, 2, 1, 3] [1, 2, 2, 1] [1, 2, 2, 2] [1, 2, 2, 3] [1, 2, 3, 1] [1, 2, 3, 2] [1, 2, 3, 3] [1, 3, 1, 1] [1, 3, 1, 2] [1, 3, 1, 3] [1, 3, 2, 1] [1, 3, 2, 2] [1, 3, 2, 3] [1, 3, 3, 1] [1, 3, 3, 2] [1, 3, 3, 3] [2, 1, 1, 1] [2, 1, 1, 2] [2, 1, 1, 3] [2, 1, 2, 1] [2, 1, 2, 2] [2, 1, 2, 3] [2, 1, 3, 1] [2, 1, 3, 2] [2, 1, 3, 3] [2, 2, 1, 1] [2, 2, 1, 2] [2, 2, 1, 3] [2, 2, 2, 1] [2, 2, 2, 2] [2, 2, 2, 3] [2, 2, 3, 1] [2, 2, 3, 2] [2, 2, 3, 3] [2, 3, 1, 1] [2, 3, 1, 2] [2, 3, 1, 3] [2, 3, 2, 1] [2, 3, 2, 2] [2, 3, 2, 3] [2, 3, 3, 1] [2, 3, 3, 2] [2, 3, 3, 3] [3, 1, 1, 1] [3, 1, 1, 2] [3, 1, 1, 3] [3, 1, 2, 1] [3, 1, 2, 2] [3, 1, 2, 3] [3, 1, 3, 1] [3, 1, 3, 2] [3, 1, 3, 3] [3, 2, 1, 1] [3, 2, 1, 2] [3, 2, 1, 3] [3, 2, 2, 1] [3, 2, 2, 2] [3, 2, 2, 3] [3, 2, 3, 1] [3, 2, 3, 2] [3, 2, 3, 3] [3, 3, 1, 1] [3, 3, 1, 2] [3, 3, 1, 3] [3, 3, 2, 1] [3, 3, 2, 2] [3, 3, 2, 3] [3, 3, 3, 1] [3, 3, 3, 2] [3, 3, 3, 3] 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -