-6

I'm looking for a way to generate combinations of objects from a set of given objects order does not matter (NOT permutations but combinations) eg. say. i have a list of A, B, C, D and I need to choose 3 of these.. then I should get a list of

ABC ACD BCD

is there an efficient logic to do so in objective C?

inforeqd
  • 3,209
  • 6
  • 32
  • 46
  • 2
    Did you use the search facility before posting? (e.g. ["generating combinations"](http://stackoverflow.com/search?q=generating+combinations)) – Oliver Charlesworth Jun 25 '13 at 06:02
  • 1
    http://stackoverflow.com/q/15738807/1468406 – Pratik B Jun 25 '13 at 06:03
  • see this... http://stackoverflow.com/questions/6617253/permutations-anagrams-in-objective-c-i-am-missing-something – DharaParekh Jun 25 '13 at 06:04
  • yes, did use the search, but did not find any efficient solution in objective C.. for iOS 5.1 so it cannot be processing intensive. And need to generate combinations and not permutations. none of these answer my question – inforeqd Jun 25 '13 at 06:29
  • @inforeqd Look again, the one I listed does combinations. What qualifies as "efficient" anyway? – borrrden Jun 25 '13 at 06:42
  • the one you listed assumes that I will choose only 2 from a set of n, the algo needs to work for choosing any given r from n.. nCr if that clarifies? – inforeqd Jun 25 '13 at 06:45
  • It can easily be applied to 3, and with a little more thought it can be applied dynamically. – borrrden Jun 25 '13 at 06:47
  • couldn't come up with one dynamically so thought of asking on the forum.. – inforeqd Jun 25 '13 at 06:49

2 Answers2

0

The key here lies in the technique. The question I linked to in the comment has the basis. The algorithm is as follows:

1) Start at the first n elements
2) Increment the final element until it is at the final position
3) Move the previous element ahead by one, and reset the final element to the position after the previous element
4) Repeat step 2 and step 3 until the previous element and the last element are in the final two positions
5) Move the previous-previous element ahead by 1, and reset the two elements at the end to the position after the previous-previous element
6) repeat this process until all elements are at the end

Simplified it becomes this:

While the first element of the combo array is less than the difference between the original array and the combo array do the following:

While the last element of the combo array is less than the length of the count of the original array increment last element and take the resulting indices from the original array

After that, check the previous element. If it is less than lastElement -1 then increment it and set lastElement to previous element + 1 and repeat the above step. If not, then check the previous element, and so on. If they are all sequential, then you are finished.

To aid in this algorithm, I suggest making an array of indices that matches the size of your combination. Let's say you want the 3-element combinations of a 5-element array. Make a 3 element array of the first indices:

[0,1,2]

Applying the algorithm above first you would increment the final element until the end [0,1,3] [0,1,4]

Then, increment the previous one and reset the final one [0,2,3]

Repeat the above steps until the last two are in the final positions [0,2,4] [0,3,4]

Move along down the line [1,2,3] [1,2,4] [1,3,4] [2,3,4] and now all the indices are in the final position and you are finished.

I'm not going to write this out in Objective-C, though, because implementing an algorithm is something that is required of every programmer. I think this will be sufficient information for you to succeed.

borrrden
  • 33,256
  • 8
  • 74
  • 109
0

Yes, You have. It's not difficult but lengthy. Just you have to take for loops for count of characters. Just see below code.

NSMutableArray *arr_Combinations = [[NSMutableArray alloc] init];


NSString *str_First;
NSString *str_Second;
NSString *str_Third;

for (NSInteger firCount = 0; firCount < 4; firCount++)
{
    NSMutableArray *arr_First = [NSMutableArray arrayWithObjects:@"A", @"B", @"C", @"D", nil];
    str_First = [arr_First objectAtIndex:firCount];

    [arr_First removeObjectAtIndex:firCount];

    for (NSInteger secCount = 0; secCount < [arr_First count]; secCount++)
    {
        NSMutableArray *arr_Second = [[NSMutableArray alloc] initWithArray:arr_First];
        str_Second = [str_First stringByAppendingString:[arr_Second objectAtIndex:secCount]];
        [arr_Second removeObjectAtIndex:secCount];
        for (NSInteger thirCount = 0; thirCount < [arr_Second count]; thirCount++)
        {
            NSMutableArray *arr_Third = [[NSMutableArray alloc] initWithArray:arr_Second];
            str_Third = [str_Second stringByAppendingString:[arr_Third objectAtIndex:thirCount]];
            [arr_Third removeObjectAtIndex:thirCount];
            for (NSInteger fothCount = 0; fothCount < [arr_Third count]; fothCount++)
            {
                NSString *str_Final = [str_Third stringByAppendingString:[arr_Third objectAtIndex:fothCount]];
                NSLog(@"%@", str_Final);
                [arr_Combinations addObject:str_Final];
            }
        }
    }
}
NSLog(@"%d", [arr_Combinations count]);
Duck
  • 34,902
  • 47
  • 248
  • 470
HRD0701
  • 39
  • 1
  • 4