Sequences and Strings
Sequences
A sequence is a special case of function, where domain of the function is a subset of
N={1,2,3,4 …}. Let s or {sn} be a sequence of values s1 , s2 , s3 , s4 ,….we use the notation sn to denote value s(n).
Sequence of all positive even integers:
s1 =2, s2=4 s3 =6 … sn =2n …
Depending on domain sequence can be finite or
Example
Consider the sequence bn = (1)n.
{bn} = 1, 1, 1, 1, …
{bn} denotes an infinite sequence of 1’s and
Recognizing Sequences
Sometimes, you’re given the first few terms
of a sequence, and you are asked to find the sequence’s generating function, or a
Recognizing Sequences
Examples: What’s the next number?
1,2,3,4,… 1,3,5,7,9,… 2,3,5,7,11,...
5 (the 5th smallest number >0)
11 (the 6th smallest odd number >0)
Sequence Notation
Given a sequence {an} defined by rule
an= n2 – 1, for all n1
values of the function are :
a1=0, a2= 3, a3= 8, a4=15 …
another notation of this sequence is {n2 – 1} n1
We call this representation explicit . Each value an can be computed directly for arbitrary value n1.
Sequence Notation
We can obtain member an of this sequence in a different form
a1=0, an+1= an +2n + 1, for all n1
values of the sequence are the same as before :
a1=0, a2= 3, a3= 8, a4=15 …
We call this representation recurrent. Each value an can be computed from the previous value an-1 .
Sequence Notation
A recurrence relation for the sequence {an} is an
equation that expresses an in terms of one or more of the previous terms of the sequence, namely,
a0, a1, . . . , an−1, for all integers n with n ≥ n0, where
n0 is a nonnegative integer. A sequence is called a
solution of a recurrence relation if its terms satisfy the recurrence relation
Example
Let {an} be a sequence that satisfies the
recurrence relation an = an−1 + an−2 for n =2, 3, 4, . . . , and suppose that a0 = 0 and a1 = 1. What are a2 , a3, a4 , a5,?
We see from the recurrence relation that
a2 = a1+a0 = 1+0 = 1, a3 = a2 +a1 = 1+1 = 2,
a4 = a3+a2 = 2+1 = 3, a5 = a4 +a3 = 3+2 = 5.
Sequence Notation
Given a sequence {am} defined on set MN a sequence {ak} defined on set KM is a
subsequence of {am}.
Sequence of all positive even numbers is a
subsequence of all positive numbers.
sequence 3,5,7,8 is a subsequence of
2,3,3,4,5,6,7,8,9 , but it is not a subsequence of
Summation Notation
Given a sequence {an}, the summation of {an}
from j to k is written and defined as follows:
Here, i is called the index of summation. Let {an}= 1,2,3,4
k j j k j i
i a a a
a
: 1 ...10 4 3 2 1 : 4
Generalized Summations
For an infinite series, we may write:
To sum a function over all members of a set
X={x1, x2, …}:
Or, if X={x|P(x)}, we may just write:
... ) ( ) ( : )
( 1 2
x f x f x f X x ... ) ( ) ( : ) (
f x f x f x... : 1
j j ji
i a a
Simple Summation Example
4
2 2 2 2
2
( 1) (2 1) (3 1) (4 1) (4 1) (9 1) (16 1) 5 10 17
32
i
i
More Summation Examples
An infinite series with a finite sum:
Using a predicate to define a set of elements to
sum over: 87 49 25 9 4 7 5 3
22 2 2 2
10 prime)
is (
2
x x x 2 ... 1 ... 2 22 41
2 1 1 0 0
i iFinding the sum of 1,2,…n
Consider the sum (assume n is even):
1+2+…+(n/2)+((n/2)+1)+…+(n-1)+n
n/2 pairs of elements, each pair summing to
n+1, for a total of (n/2)(n+1)=n(n+1)/2.
…
n+1
n+1
Example: Geometric Series
A geometric series is a series of the form
a, ar, ar2, ar3, …, ark, where a,rR.
The sum of such a series is given by:
We can reduce this to closed form via clever
manipulation of summations...
0 0
k k
i i
i i
S ar a r
Geometric Series closed formula
(1+r+r2+r3+…rk)(1-r)= (1-r+r-r2+r2+…-rk+1)=
=(1-rk+1)
(1+r+r2+r3+…rk)=(1-rk+1)/(1-r)=
=a(1-rk+1)/(1-r)
0 0
k k
i i
i i
S ar a r
Example: Infinite Geometric Series
Let x be a real number with |x| < 1. Find summation From the previous slide we take a=1 and r=x,
we have
Then we have Special case: = 2
Strings
A string over X, where X is a finite set , is a
finite sequence of elements from X.
Example : Let X={a,b,c} then sequence =
b,a,a,c,c,c is a string. We use notation as
=baaccc or shortly =ba2c3.
The length of a string is the number of its
Strings
A concatenation of strings =ac2 and =ba2c3
is string = ac2ba2c3 ,||=||+||
A substring is a subsequence of a string where
elements are consecutive. =acc is a
substring of , but =bcc is not a substring of