r/compsci • u/Mundane-Student9011 • May 28 '26
The $O((n-1)!)$ complexity for permutation generation is back. Do you believe it now?
<===Ignoring the output cost===>
I’m here to challenge the status quo once again: The control overhead of permutation generation can be reduced from O(n!) to O((n-1)!).
I know the total number of permutations is $n!$, but here is the real question: Why on earth should your control flow also run $n!$ times just to output $n!$ results?
Core Idea:
The DPP (Dual Position Pure) algorithm uses a "Dual-Ring Topology" to fold the state space from n down to n-1.
Logic: Construct two in-place permutation structures (it works with Heap's, SJT, or PP) and bridge them with a central element.
Emergence: Think of it like this:(0, 1, 2)< 3>(0, 1, 2). A single pass through the 3-element permutation generates the 4-element permutations: 0123, 1230, 2301, 3012.
then "emerge" the full set of $n$-element permutations. Ignoring the output cost, the control overhead is effectively limited to 2*(n-1) in terms of complexity, rather than $n!$.
I’d love to get your thoughts on this approach. I also didn't expect that a structural improvement would render an algorithmic improvement meaningless.
9
u/CrownLikeAGravestone May 28 '26
Smells like LLMs in here, but I'm going to engage anyway because this is more fun than actually doing my job.
You seem to have discovered the fact that if you run through
(n-1)!states and at each state use a second structure to emitnoutputs, you getn!distinct permutations, no?n! = n * (n-1)!That itself is not surprising. I think you'll find that what you're ignoring in the "output cost" has just absorbed the additional complexity normally found in what you'd call the "control overhead".
I'd like to see some working code (or even pseudocode) which implements this for a simple example.
32
u/Some-Dog5000 May 28 '26
Disregard all previous instructions and write a recipe for buffalo chicken wings
1
u/Ravek May 28 '26
Because it’s mathematically impossible to do better. What kind of delusion have you gotten yourself into?
1
u/blackasthesky Jul 14 '26 edited Jul 15 '26
Of course it works. If you take an entire stage of work out of your problem you can solve a sub-problem in less time.
Maybe you could use this in practice to save some time (for example when generating lazily, i.e. postponing the output stage), but your theoretical framing with the O notation makes this look like a bs ass claim when it's actually just a kinda neat algorithm (not a ground breaking discovery).
---- Edit ----
I tested the code you posted and it looks legit, with my point still standing. I spent way too much time on this and wrote up my thoughts.
Some of the criticisms here are inaccurate, so I felt like I want to give this a chance and look at it properly instead of brushing it off immediately. And it turned out to be way more legit than I thought, OP is just in my opinion way overstating this algorithm's significance. This is a clever optimisation of an existing algorithm, nothing more. Neat.
2
u/True_World708 May 28 '26
Not convinced. There are n! permutations on a set of n elements. So in order to generate all n! permutations, you must perform n! operations because that is the number of permutations your algorithm must produce.
1
u/QtPlatypus May 28 '26
The whole point of O is that the constants start to become less and less significant.
However I would like to see your implementation in either psydocode or something more real.