1

I'm trying to understand recursion with the help of few examples. I found this example of printing all possible combinations of r elements in a given array of size n using recursion.

Print all possible combinations of r elements in a given array of size n.

They are using the idea behind the expression:

enter image description here

What i'm trying to understand here is the conceptual meaning of this expression. I have read different articles but couldn't find a satisfactory explanation.

A mathematical or practical example which uses this expression would be really helpful.

MarianD
  • 13,096
  • 12
  • 42
  • 54
Ankush Dutt
  • 143
  • 8

1 Answers1

2

First, there are different notations for combinations in math:

enter image description here

Using the first of them, your formula is

enter image description here

The left-hand side of it means: The number of ways we can select r elements from the set of n elements.

Let S be a set of n elements. Let x be the last element of it, so the set S is for example

+-------------+---+
| a b c d e f | x |
+-------------+---+

Let C is an arbitrary combination of r elements from the set S.

(Particularly, to follow just introduced example, you may imagine that r = 3, and n = 7 - as the set is {a, b, c, d, e, f, x}.)

There are only 2 possibilities:

  1. C contains x (e. g. C = {a, d, x}), or
  2. C does not contain x (e. g. C = {a, d, e}).

If C contains x, then remaining (r - 1) elements (i. e. 2 in our example) are chosen from remaining (n - 1) elements (i. e. from {a, b, c, d, e, f} in our example) - so there are

enter image description here

ways how to select such combination.

If C does not contain x, then all r elements are chosen from remaining (n - 1) elements - so there are

enter image description here

ways how to select such combination.

MarianD
  • 13,096
  • 12
  • 42
  • 54
  • Thanks for this great explanation!! Can you expand this one level more. Say if c contains x and i expand C(n-1,r-1) meaning I have to choose r=2 now from n-1=6 elements now. And say if c doesn't contain x and i expand C(n-1,r) and i'm choosing r=3 from n-1=6 elements now. Does that make sense or am i missing something and would x be same for this expansion as well. – Ankush Dutt Feb 13 '19 at 11:01
  • @AnkushDutt, yes, you are right. `x` is always the same element. By other words, you either select *trinities* from `{a, b, c, d, e, f}`, or *`x` plus pairs* from the same set `{a, b, c, d, e, f}`. - Please consider to accept my answer if it was useful for you. – MarianD Feb 13 '19 at 14:34