Search code examples
algorithmcombinations

Is there effective algorithm that will return all different combination?


EDITED: I meant COMBINATION and not PERMUTATIONS

Is there effective algorithm that will return all different permutations from the given array? ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", ...]

e.g.: AB,AC,AD,..,DE,..,HI,..,ABC,ABD,...,DEF,..,CDEFG,...,ABCDEFGHIJK,....

I found some algorithms, but they return ALL permutation and not different ones. By different I mean that:

  1. AB & BA are the same permutations

  2. DEF & FED & EFD & DFE are the same permutations,


Solution

  • The best I can think is sort of a binary counter:

     A B C
    -------
     0 0 0 | Empty Set
     0 0 1 | C
     0 1 0 | B
     0 1 1 | BC
     1 0 0 | A
     1 0 1 | AC
     1 1 0 | AB
     1 1 1 | ABC