Number of sub-sequences in a given sequence

前端 未结 12 649
傲寒
傲寒 2021-01-30 18:18

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

12条回答
  •  醉酒成梦
    2021-01-30 18:50

    To extend the accepted answer, the number of subsequences can be thought in mathematical terms. Let's take an example of a string: 'ABC'.

    Number of subsequences of string 'ABC':

    => C(3, 0) + C(3, 1) + C(3, 2) + C(3, 3) = 1 + 3 + 3 + 1 = 8 (2^3).
    

    (Note: C(m, n) stands for number of subsequences of size 'n' from a string of size 'm')

    It can be easily verified by listing all of them:

    C(3, 0) = '', // Taking 0 letters at a time out of the given string.
    C(3, 1) = 'A', 'B', 'C', // Taking 1 letter at a time.
    C(3, 2) = 'AB', 'AC', 'BC', // Taking 2 letters at a time.
    C(3, 3) = 'ABC'. // Taking 3 letters at a time.
    (Total count = 8)
    

    We keep the sequence of letters same for a subsequence, hence, Combination instead of a Permutation.

    Note: Sum of binomial coefficients, using binomial theorem = 2^n. Proof below:

    From Binomial theorem,
    (x + y)^n = C(n, 0) x^n y^0 + .... + C(n, n) x^0 y^n
    
    Using x = 1, y = 1,
    (1+1)^n = C(n, 0) 1^n 1^0 + .... + C(n, n) 1^0 1^n 
    => 2^n = C(n,0) + C(n,1) + .... + C(n,n)
    

    That's where the '2' comes from, from binomial theorem.

提交回复
热议问题