Sequences: Examples (CSCI 2824, Spring 2015)

In the following we consider some examples on sequences and summations.

We would be just guessing closed form/summation expressions in the following examples. We will learn in Chapter 2 how to prove that a guess is correct.

Examples on sequences

  • Example 1: finding a simple formula

  • Example 2: compound interest

Example on summation

  • Example 1: using summation notation

  • Example 2: Ex 24(a) from textbook Sect 1.2

  • Example 3: bubble sort — worst case complexity

Examples on Sequences

Example 1: finding a simple formula

How do we find a simple formula that gives the terms of a sequence that begins with the following list?

    5,9,17,33,65,129,ldots

It is not too difficult to notice that

 5xrightarrow{+4}9xrightarrow{+8}17xrightarrow{+16}33 xrightarrow{+32}65xrightarrow{+64}129cdots

We look at two different expression: the recursive relation and the closed form expression.

Method 1: using recursive relation

A simpler way in this example is to use recursive relation.

Step 1: define the initial term. (A recursion always starts with some initial term(s)!)

    a_1 = 5.

Step 2: (optional, i.e., if the sequence is too difficult) assign indices to each term in the sequence by drawing a table:

 begin{array}{l|c|c|c|c|c|c} mbox{index } k & 1 & 2& 3& 4& 5& 6  hline a_k & 5 & 9 & 17 & 33 & 65 & 129  end{array}

Step 3: identify the pattern — how does the next term depend on the previous one? For example, we see that the increment is always some power of 2:

 a_2 = a_1+4=a_1+2^2,  a_3 = a_2+8=a_2+2^3,  a_4 = a_3+16=a_3+2^4.

After writing out a few terms, we can guess a formula! In this case, the recursive relation would be

 a_k = a_{k-1} +2^k, mbox{ for all }kgeq2.

Therefore the final answer is:

 begin{cases} a_1=5&  a_k = a_{k-1} +2^k,& mbox{for all } kgeq2. end{cases}

It is very important to define the initial term if you choose to use recursive relation! Otherwise the recursive relation would not be well-defined — if I don't know what a_1 is, how would I know what a_2=a_1+4 is, right?

Method 2: closed form expression

Using the recursive relation, you could actually obtain a closed form expression by substitution.

 begin{array}{rl}    a_1=& 5 = 3 + 2^1     a_2=& a_1 + 2^2 = (3+2^1)+2^2     a_3=& a_2 + 2^3 = (3+2^1 + 2^2) + 2^3     a_4=& a_3 + 2^4 = (3+2^1+ 2^2+2^3) + 2^4     vdots&     a_k=& a_{k-1}+2^k = 3+2^1+2^2+2^3+cdots 2^k = 3+sum_{j=1}^k 2^j. end{array}

Hence the final answer is:

 a_k= 3+sum_{j=1}^k 2^j, mbox{ for all } k=1,2,ldots.

Note that for closed form expression, we do not need to specify the initial case, since each term is expressed directly in terms of the index and the expression is independent of the previous term.

Example 2: compound interest

Suppose Tom put $5000 in his savings account, which pays 5% interest once a year into his account. Assuming that Tom does not deposit or withdraw any money from the account, how much money would there be in the account 7 years after the $5000 deposit?

Step 1: figuring out the recursive relation. Let M_j be the amount of money in the account after j years of deposit. Obviously, M_0=5000 and for jgeq1 (i.e., after at least one year) we have

 M_j=underbrace{M_{j-1}}_{mbox{capital}} + underbrace{M_{j-1}times 5%}_{mbox{interest}} = 1.05M_{j-1},

which is a recursive relation.

Step 2: using substitution to get the closed form expression. But what is the numeric amount after 7 years? This would require substitution, like this:

 begin{array}{rl} M_0=& 5000,  M_1=& 1.01 M_0  M_2=& 1.01 M_1 = 1.01 (1.01 M_0) = 1.01^2 M_0  M_3=& 1.01 M_2 = 1.01 (1.01^2 M_0) = 1.01^3 M_0. end{array}

In general it looks like we have the following closed form expression:

 M_k=1.01^k M_0   mbox{for all }kgeq0.

Step 3: take k=7. So a good guess of the numeric amount in the savings account after 7 years would be

 M_7=1.01^7times M_0 = 1.01^7times 5000.

Summation

Example 1

How do we express 3^0+3^1+3^2+ldots+3^k in summation notation?

First we can draw a table matching the terms in the summation with appropriate indices:

 begin{array}{|l|l|l|l|l|l|} hline j & 0 & 1 & 2 & cdots & k  hline mbox{j-th term}  & 3^0 & 3^1 & 3^2 & cdots & 3^k  hline end{array}

We see that the index j runs from 0 to k, so we know that the answer would be in the form sum_{j=0}^k ? (i.e., the lower limit is given by the first index 0 and the upper limit is given by the last index k). It remains to determine the summand, which is easy: it is simply 3^j (noting how the j-th term depends on the index j in the table). Therefore the answer is

 sum_{j=1}^k 3^j.

Example 2: Exercise 24(a) from Section 1.2 of textbook

How do we express the following sum using summation notation?

    4+8+12+16+20+24+28+32+36

We observe that the k-th term in the summation is 4 times k. Alternatively, by drawing the table:

 begin{array}{l|c|c|c|c|c|c|c|c|c} mbox{index k } & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9  hline mbox{k-term in} & 4times 1 & 4times 2 & 4times 3 & 4times 4 &4times 5 & 4times 6 & 4times 7 & 4times 8 & 4times 9   mbox{the summation} & =4 & =8 & =12 & =16 &=20 & =24 & =28 & =32 & =36 end{array}

we see that the summand takes the form 4k, and the lower and upper limits are given by 1 and 9, respectively. Hence the sum equals

 sum_{k=1}^9 4k.

Example 3: Bubble sort

One way of algorithmically sorting a finite sequence of numbers in ascending (or descending) order is to use the (not so efficient) bubble sort algorithm. (For illustration, see the gif example from Wikipedia page on bubble sort.) A pseudocode of the bubble sort algorithm is as follows:

Bubble sort
INPUT: a(1),...,a(n)
for (i=1; i<=n; i++){
   for(j=1; j<i, j++){
      if(a(j)>a(j+1)){
         swap a(j) and a(j+1);
      }
   }
}
OUTPUT: a(1),...,a(n) sorted in ascending order

The nontrivial operation involved in the bubble sort is comparing two numbers (i.e., checking the ‘‘if’’ condition in the inner for-loop). How many comparisons need to be done if the input finite sequence is of length n?

In the bubble sort algorithm, one comparison occurs within each iteration of the inner for-loop. Since the inner for-loop is executed i-1 times at the i-th iteration of the outer for-loop, and the outer for-loop is executed n times, the total number of comparison made is

 begin{array}{rl}    &  displaystyle    sum_{i=1}^n # mbox{ comparisons in the }     imbox{-th iteration of the outer for-loop}     =& displaystyle    sum_{i=1}^n (i-1)    = 1+2+cdots+(n-1)    = frac{n(n-1)}{2}    . end{array}

Note: In the lecture, we consider the following version of bubble sort that attempts to lower the number of comparison needed.

Bubble sort
INPUT: a(1),...,a(n)
flag=true;
for (i=1; i<=n; i++){
   if(flag){
      for(j=1; j<i, j++){
         flag=false;
         if(a(j)>a(j+1)){
            swap a(j) and a(j+1);
            flag=true;
         }
      }
   }
}
OUTPUT: a(1),...,a(n) sorted in ascending order

And we asked the following question: what is the worst-case complexity of bubble sort in terms of number of comparisons? That is, for any fixed length n of input finite sequence, what is the largest possible number of comparisons performed by the bubble sort?