Home > Net >  What is N in this given scenario
What is N in this given scenario

Time:08-05

I am trying to implement this code and this website has kindly provided their algorithm but I am trying to Find out what is "N" I understood what "I" and "M" is but not "N", is "N" the Total input(in the below example 5 because there are 5 letters)?

Algorithm:

Combinations are generated in lexicographical order. The algorithm uses indexes of the elements of the set. Here is how it works on example: Suppose we have a set of 5 elements with indexes 1 2 3 4 5 (starting from 1), and we need to generate all combinations of size m = 3.

First, we initialize the first combination of size m - with indexes in ascending order

1 2 3

Then we check the last element (i = 3). If its value is less than n - m i, it is incremented by 1.

1 2 4

Again we check the last element, and since it is still less than n - m

  • i, it is incremented by 1.

1 2 5

Now it has the maximum allowed value: n - m i = 5 - 3 3 = 5, so we move on to the previous element (i = 2).

If its value less than n - m i, it is incremented by 1, and all following elements are set to value of their previous neighbor plus 1

1 (2 1)3 (3 1)4 = 1 3 4

Then we again start from the last element i = 3

1 3 5

Back to i = 2

1 4 5

Now it finally equals n - m i = 5 - 3 2 = 4, so we can move to first element (i = 1) (1 1)2 (2 1)3 (3 1)4 = 2 3 4

And then,

2 3 5

2 4 5

3 4 5

  • and it is the last combination since all values are set to the maximum possible value of n - m i.

Input: A
B
C
D
E

Output:

A B C

A B D

A B E

A C D

A C E

A D E

B C D

B C E

B D E

C D E

CodePudding user response:

N here is the size of the set of set from which you generate the combinations. In the given example, "Suppose we have a set of 5 elements with indexes 1 2 3 4 5 (starting from 1)", N is 5.

CodePudding user response:

Take a look at the very first paragraf of the link you provided.

It states that

This combinations calculator generates all possible combinations of m elements from the set of n elements.

So yes, n is the number of elements or letters that the algorithm needs to use.

CodePudding user response:

Combinations are usually symbolized with nCm, or n choose m. So n is the total set size(in this example 5) and m is the number chosen(3).

  • Related