Can someone please explain algorithm for itertools.permutations routine in Python standard lib 2.6? I don’t understand why it works.
Code is:
def permutations(iterable, r=None):
# permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
# permutations(range(3)) --> 012 021 102 120 201 210
pool = tuple(iterable)
n = len(pool)
r = n if r is None else r
if r > n:
return
indices = range(n)
cycles = range(n, n-r, -1)
yield tuple(pool[i] for i in indices[:r])
while n:
for i in reversed(range(r)):
cycles[i] -= 1
if cycles[i] == 0:
indices[i:] = indices[i+1:] + indices[i:i+1]
cycles[i] = n - i
else:
j = cycles[i]
indices[i], indices[-j] = indices[-j], indices[i]
yield tuple(pool[i] for i in indices[:r])
break
else:
return
You need to understand the mathematical theory of permutation cycles, also known as “orbits” (it’s important to know both “terms of art” since the mathematical subject, the heart of combinatorics, is quite advanced, and you may need to look up research papers which could use either or both terms).
For a simpler introduction to the theory of permutations, wikipedia can help. Each of the URLs I mentioned offers reasonable bibliography if you get fascinated enough by combinatorics to want to explore it further and gain real understanding (I did, personally — it’s become somewhat of a hobby for me;-).
Once you understand the mathematical theory, the code is still subtle and interesting to “reverse engineer”. Clearly,
indicesis just the current permutation in terms of indices into the pool, given that the items yielded are always given bySo the heart of this fascinating machinery is
cycles, which represents the permutation’s orbits and causesindicesto be updated, mostly by the statementsI.e., if
cycles[i]isj, this means that the next update to the indices is to swap the i-th one (from the left) with the j-th one from the right (e.g., ifjis 1, then the last element ofindicesis being swapped —indices[-1]). And then there’s the less frequent “bulk update” when an item ofcyclesreached 0 during its decrements:this puts the
ith item ofindicesat the very end, shifting all following items of indices one to the left, and indicates that the next time we come to this item ofcycleswe’ll be swapping the newith item ofindices(from the left) with then - ith one (from the right) — that would be theith one again, except of course for the fact that there will be abefore we next examine it;-).
The hard part would of course be proving that this works — i.e., that all permutations are exhaustively generated, with no overlap and a correctly “timed” exit. I think that, instead of a proof, it may be easier to look at how the machinery works when fully exposed in simple cases — commenting out the
yieldstatements and addingprintones (Python 2.*), we haveRunning this shows:
Focus on the
cycles: they start as 3, 2 — then the last one is decremented, so 3, 1 — the last isn’t zero yet so we have a “small” event (one swap in the indices) and break the inner loop. Then we enter it again, this time the decrement of the last gives 3, 0 — the last is now zero so it’s a “big” event — “mass swap” in the indices (well there’s not much of a mass here, but, there might be;-) and the cycles are back to 3, 2. But now we haven’t broken off the for loop, so we continue by decrementing the next-to-last (in this case, the first) — which gives a minor event, one swap in the indices, and we break the inner loop again. Back to the loop, yet again the last one is decremented, this time giving 2, 1 — minor event, etc. Eventually a whole for loop occurs with only major events, no minor ones — that’s when the cycles start as all ones, so the decrement takes each to zero (major event), noyieldoccurs on that last cycle.Since no
breakever executed in that cycle, we take theelsebranch of thefor, which returns. Note that thewhile nmay be a bit misleading: it actually acts as awhile True—nnever changes, thewhileloop only exits from thatreturnstatement; it could equally well be expressed asif not n: returnfollowed bywhile True:, because of course whennis0(empty “pool”) there’s nothing more to yield after the first, trivial emptyyield. The author just decided to save a couple of lines by collapsing theif not n:check with thewhile;-).I suggest you continue by examining a few more concrete cases — eventually you should perceive the “clockwork” operating. Focus on just
cyclesat first (maybe edit theprintstatements accordingly, removingindicesfrom them), since their clockwork-like progress through their orbit is the key to this subtle and deep algorithm; once you grok that, the wayindicesget properly updated in response to the sequencing ofcyclesis almost an anticlimax!-)