Saturday, January 12, 2008

Permutation generator !!!!

Unintentionally I visited this site and I felt cool, so thought of sharing (or keeping it for me to use later)!! Never thought generating permutation is so easy and they say this is the fastest permutation generating algorithm.. when algorithms get efficient, always coding gets tougher.. but not this time.. Though I didn't try to write it in any programming language, but the algorithms itself looks pretty simple(especially if i do it JAVA)

Let try to find the complexity of it..

To find the permutation of 'n', it needs to know the permutation of 'n-1', which in turn needs 'n-2'
Once we have n-1 permutation for 'n', we need to duplicate each entry 'n' times and then weave 'n'.
so totally it takes 2n assuming we already know 'n-1'

for n-1 it will take 2(n-1)...

so for n : 2n + 2(n-1) + 2(n-2) + ... 2 = 2(n+n-1+...1) = 2n(n+1)/2 = n(n+1) = O(n^2).. is it right? I think so!!!

Happy analyzing !!!

No comments: