0

I am having a hard time to generate a list of unique collection combinations of different sizes for a set, say ,where n=6 [variable]

set = {2,3,4,5,6,7}

I need to generate all unique combinations for the above set using javascript (Ordered collection) like this

6C1  +  6C2  +  6C3  +   6C4   +   6C5   +   6C6

2       2,3    2,3,4   2,3,4,5  2,3,4,5,6  2,3,4,5,6,7
3       2,4    2,3,5   2,3,4,6  2,3,4,5,7 
4       2,5    2,3,6   2,3,4,7  
5       2,6    2,3,7            2,4,5,6,7
        2,7            2,4,5,6  
.              2,4,5   2,4,5,7  3,4,5,6,7
.       3,4    2,4,6            .
8       3,5    2,4,7   2,5,6,7  .
        3,6                     
        3,7    2,5,6   3,4,5,6 
               2,5,7   3,4,5,7
        4,5              
        4,6    2,6,7   3,5,6,7
        4,7    
               3,4,5   .
        5,6    3,4,6   .
        5,7    3,4,7   .
        .      
        .      3,5,6    
        6,7    3,5,7   4,5,6,7 

               3,6,7
               .       
               .       

               5,6,7 

Now the complication comes when I need to assign each of these values to alphabetical order.The order is small letters first a,b,c.. and then capital letters A,B,C,.. and after that aA,aB,aC and so on

a=2     h=2,3    o=2,3,4   I=2,3,4,5  2,3,4,5,6  2,3,4,5,6,7
b=3     i=2,4    p=2,3,5   J=2,3,4,6  2,3,4,5,7 
c=4     j=2,5    q=2,3,6   K=2,3,4,7  
d=5     k=2,6    r=2,3,7              2,4,5,6,7
        l=2,7              L=2,4,5,6  
.                s=2,4,5   M=2,4,5,7  3,4,5,6,7
.       m=3,4    t=2,4,6            .
g=8     n=3,5    u=2,4,7   N=2,5,6,7  .
        o=3,6                     
        p=3,7    v=2,5,6   O=3,4,5,6 
                 w=2,5,7   P=3,4,5,7
        q=4,5              
        r=4,6    x=2,6,7   Q=3,5,6,7
        s=4,7    
                 y=3,4,5   .
        t=5,6    z=3,4,6   .
        u=5,7    A=3,4,7   .
          .      
          .      B=3,5,6    
          6,7    C=3,5,7   W=4,5,6,7 

                 D=3,6,7
                 .       
                 .       

                 H=5,6,7 

I know that a normal Brute Force way of approach would be the simplest solution to the problem but for large values this would be quite inefficient.

I am looking for a more efficient method of solving this problem. Any good solutions would be of great help.

Thanks in Advance, Neal

4
  • I don't quite understand what you're trying to do... Commented Sep 17, 2012 at 8:01
  • I am generating unique combinations nCr which are ordered and assigning each combination to a variable Commented Sep 17, 2012 at 8:02
  • Don't you think that using an array instead of variables is a better approach of saving these combinations? Commented Sep 17, 2012 at 8:23
  • I need to assign each combination to a variable . How can this be done using arrays? Commented Sep 17, 2012 at 8:35

2 Answers 2

2

Function generate combinations:

function generate(index, array, used, result)
{
    if (index == array.length && used.length > 0)
    {
         var a = new Array();
         for (var i = 0; i < used.length; i++)
            a.push(used[i]);

         result.push(a);
    }
    else {
       generate(index+1, array, used, result);

       used.push(a[index]);
       generate(index+1, array, used, result);
       used.pop();
    }
}

function generateAllCombinations(array)
{ 
    var result = new Array();
    generate(0, array, [], result);
    return result;
}

Functions generateAllCmobinations will return you array of all possible non-empty combinations. If you wish I will write function wich will return all combinations of length K ;-)

And you cannot find better algorithms. Because solution is always has complexity O(C), where C is number of total combinations. So complexity according to the size of array is exponential O(2^N)

Sign up to request clarification or add additional context in comments.

Comments

1

If you want to create variables for each combination, I suggest using an object. In Javascript, elements of an object can be accessed in two ways:

var obj = {};
obj["element"] = "foo";
alert(obj.element);    // Will alert: "foo".

Instead of writing a string each time, you can save you variable names in an array and use this array to assign the names:

var names = ["a", "b", "c", "d"];
var obj = {};
for (var idx = 0; idx < names.length; idx++)
    obj[names[idx]] = idx;

alert(obj.a);          //Will alert: "0".
alert(obj.c);          //Will alert: "2".

If you need to create global variables, you could use the window object instead of obj. Although I do not recommend messing around with the window object.

And the names array, you can see in code above can be created programmatically. So, no need to "brute force hardcoding".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.