If I am given a sequence X = {x1,x2,....xm}
, then I will have (2^m)
subsequences.
Can anyone please explain how can I arrive at this formula intuitivel
Each subsequence is defined by choosing between selecting or not selecting each of the m elements. As there are m elements, each with two possible states, you get 2^m possibilities.
Each element is either in a subsequence or not. Therefore, starting with the first x1 there are two sets of subsets: those with x1 included and those without. Same can be done with the smaller sub-problem {x2,...,xm}. Therefore you finally yield 2^m.
If you have a sequence S, what happens when you add a new element x to the end of S?
All the subsequences of S are still subsequences of your new sequence. So are all those subsequences with x added at the end.
Voilà! Every time you add an element, you double the number of subsequences.
Basically you will have twice as many subsequences for each new number since you'll have (2^(m-1))
"equivalent" subsequences that are shifted one space to the right (assuming horizontal ordering and adding in the right) plus the subsequence of all elements.
For each element in a sequence of length m
, you can either select it or leave it. Thus, there are 2 ways to deal with each element. Therefore, the total no. of ways to deal with all the m
elements is 2*2*2...... m
times = 2^m
times.
To anyone who is actually looking for a substring (as the title or URL might lead you to believe):
Subset: 2^n (Order doesn't matter in sets)
Subsequence: 2^n (Since we keep the original ordering, this is the same.)
Substring: n(n+1) * 1/2 (Elements must be consecutive)