Quote:
Originally Posted by Gabethebabe
Br0s
I need efficient algorithms for generating all permutations of a list of 5 items, where duplicates are possible.
Heaps algorithm helps me only to generate all 120 permutations of a list of 5 different items. However, I want only a list of 5 results if the list is AAAAB or 30 results if the list is AABBC
- AAAAA (1)
- AAAAB (5)
- AAABB (10)
- AAABC (20)
- AABBC (30)
- AABCD (60)
- ABCDE (120)
These are the 7 scenarios I need to solve efficiently. 1 and 2 are easy. 7 I can use Heaps. How to generate all permutations of scenario 3-6?
The simplest method I can think of is to just generate all 5! permutations, sort them and save only the first occurrence.
There is probably a faster way to do this but if all you care about is sets of 5 items and you've identified all the possible ways they can repeat it doesn't really matter what method you use (see below).
Quote:
I am programming in visual basic and it needs to be efficient, because it will have to run millions of times and it needs to finish before I die
Once you've generated each of the above you probably want to create lookup tables of indexes, eg:
[[1, 2, 3, 4, 5], [2, 1, 3, 4, 5], [[3, 2, 1, 4, 5], ...]
and then use these when you need them.
Depending on the inputs you might also need to sort your 5 elements (using a hard-coded sorting network) and then have an if statement to decide on the correct lookup table.
Juk
PS: If I've not understood you correctly then maybe add a few more details of what you want and I can likely help then too