I have an algorithm for generating permutations in Python
from collections import Counter
def permutations(A):
def permutations(A):
if len(A) == 0:
return
if len(A) == 1:
yield A
else:
for i in range(1, len(A)):
for p in permutations(A[i:]): # Pretend slicing is O(1)
for q in permutations(A[:i]): # Pretend slicing is O(1)
yield p + q # Pretend O(1)
yield q + p # Pretend O(1)
return set(permutations(A))
P = permutations((1, 2, 3))
C = Counter(P)
print(P)
print(C)
Which has output
{(3, 1, 2), (1, 3, 2), (3, 2, 1), (2, 3, 1), (1, 2, 3), (2, 1, 3)}
Counter({(2, 1, 3): 1, (1, 3, 2): 1, (3, 1, 2): 1, (3, 2, 1): 1, (2, 3, 1): 1, (1, 2, 3): 1})
Assuming I don't care about the time it takes to slice/concatenate arrays, is this the most efficient algorithm for generating permutations? If not, what can I do to improve it?
set(...)
to deduplicate any time of combinatorial object, you probably don't have the most efficient algorithm. \$\endgroup\$