0% found this document useful (0 votes)
175 views17 pages

Chapter 3

Chapter 3

Uploaded by

Mr. RAVI KUMAR I
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
175 views17 pages

Chapter 3

Chapter 3

Uploaded by

Mr. RAVI KUMAR I
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Chapter 3. Recurrence Relation [8 hrs.

][8 Marks]

3. Recurrence Relation
3.1.Recursive Definition Sequences
3.2.Solution of Linear recurrence relations
3.3.Solution of Nonlinear recurrence relations
3.4.Application to algorithm analysis

Recurrence Relations
A sequence can be defined by giving a general formula for its nth term by writing a few of its
terms. An alternative approach is to represent the sequence by finding a relationship among its
terms. Such relations are referred as recurrences.
Recursively defined sequences are often referred to as recurrence relations.
A recurrence relation for the sequence {an} is an equation that expresses a n in terms of one or
more of the previous terms of the sequence namely a0,a1,a2,……an-1 for all integers n with n≥n0
where n0 is a non-negative integer.
A sequence is called a solution of a recurrence relation if its term satisfy the recurrence relation.
Example
Consider the following instructions for generating a sequence
1) start with 3
2) Given any term, add 3 to get the next term.
The term of sequence obtained is
3,6,9,12,15,18,…..
If we denote the sequence as a1, a2,….
a1=3 ............. (1)
Instruction (2) can be rephrased as
an= an-1+3 n≥2 ........... (A)
By using a1 and an, we can compute any term in the sequence.
Equation (A) is an example of recurrence relation (1)

Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 80
Example
2) Let {an} be a sequence that satisfies the recurrence relation. an=an-1-an-2 for n=2,3,4 and
suppose that a0=3 and a1=5 what are a2 and a3?

From the recurrence relation.


an = an-1 - an-2
a2 =a1-a0=5-3=2
a3=a2-a1=2-5=-3

Similarly a4, a5 and also each successive terms. The sequence forms in such a way is 3,5,2,-3,….

3) Determine whether the sequence {an} is a solution of the recurrence relation an=2an-1-an-2 for
n=2,3,4,… where an=3n for every non negative integer n. Answer the same question where
an=2n and where an=5.

Suppose that an=3n for every n. therefore n≥2, 2an-1-an-2


=2(3(n-1)-3(n-2))
=6n-6-3n+6
=3n
=an
Since {an}, where an=3n, is a solution of the recurrence relation.
Suppose an=2n for every n. note a0=20-1
And a1=21=2 and a2=22=4
2an-1 - an-2, (n=2)
=2a1-a0
=2.2-1=3 ≠ a2, we see that {an}, where an=2n is not a solution of recurrence relation. an=5

4) A customer deposits 10,000 in a bank and he gets 11% interest per annual and the interest
amount is accumulated every year. Find the recurrence relation to compute total amount
customer receive after 10 year. And also calculate amount that customer receive after 30 years.
p0=10,000
Interest=11%
p1=p0+0.11 *p0
=1.11p0
=11100

p2=p1 *0.11+p1

Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 81
=1.11p0 * 0.11+p0 *1.11
=1.11p0(0.11+1)
=(1.11)p0 *1.11
=(1.11)2p0
p3=1.11p2=(1,11)3p0
pn=(1,11)pn-1=(1.11)np0
pn=(1.11)n 10,000.

Solution of Recurrence Relation


A linear homogeneous recurrence relation of degree k with constant coefficient is a recurrence
relation of the form
an=c1an-1+c2an-2+ …............… +ckan-k
Where c1, c2, ….Ck are real numbers and ck ≠ 0.
The recurrence relation in the definition is linear since R.H.S is a sum of multiples of the
previous terms of the sequence.
The recurrence relation is homogeneous since no term occur that are not multiple of the a js.
The degree is k because an is expressed in terms of the previous k term of the sequence.

-If f(n)≠0 for the recurrence relation


an=c1an-1+c2an-2+….+ckan-k+f(n)
Then it is called non-homogeneous recurrence relation.
If f(n)=0 then it is called linear homogeneous recurrence relation.

Solving linear homogeneous recurrence relation


Method of characteristic equation
We know that the linear homogeneous recurrence relation of degree k with constant coefficients
is of the form.
an=c1an-1+c2an-2+…….+ckan-k
Let an=rn is the solution of this equation then

Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 82
rn=c1rn-1+c2rn-2+……+ckan-k
Dividing both by rn-k
rk=c1rk-1+c2rk-2+…..+ck
rk-c1rk-1-c2rk-2-…..-ck=0
This is the characteristic equation of degree k of the recurrence relation.
Let us consider the liner homogeneous recurrence relations of degree 2.
i.e.
an=c1an-1+c2an-2
Its characteristic equation is of the form
r2-c1r-c2=0

The roots of this equation can be


1) Distinct roots, say r1 and r2 (i.e. r1 ≠ r2)
If roots are distinct then {an} is the solution of the given linear homogeneous recurrence relation
if and only if an is of the form
an=α1r1n+α2r2n where α1 and α2 are constant

1) Repeated roots, say r0 (i.e. r1=r2=r0)


If roots are repeated root, say r0 then {an} is the solution of the given linear homogeneous
recurrence relation if and only if a n is of the form
an= α1r0n+ α2nr0n

Theorem:
Let c1 and c2 be real numbers suppose that r2-c1r-c2=0 has two distinct roots r1 and r2, then the
sequence {an} is a solution of the recurrence relation an=c1an-1+c2an-2 iff an= α1r1n+ α2r2n for
n=0,1,2,….. where α1 and α2 are constant. To find α1 and α2 initial condition is used.

1) What is the solution of recurrence relation


an=an-1+2an-2 with a0=2 and a1=7
Let an=rn
Substituting in the equation a n=an-1+2an-2
we get,
rn=rn-1+2rn-2............(i)

Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 83
Dividing equation (i) by rn-2, we get
r2-r-2=0
r2-(2-1)r-2=0
r2-2r+r-2=0
r(r-2)+1(r-2)=0
(r-2)(r+1)=0

Its roots are r=2 and r=-1. Hence the sequence {an} is a solution to the recurrence relation iff
an= α1r1n+ α2r2 n
an= α12n+ α2(-1)n

For some constants α1 and α2. From initial conditions it follows that,
a0= α1 + α2 =2
a1= α12+ α2 (-1)
7= α12+ α2 (-1)
i.e. α1+ α2 =2 ........... (ii)
2 α1- α2 =7 ................(iii)
Solving these two equation
α1= 3 and α2 =1
Hence the solution to the recurrence relation and initial conditions is the sequence {a n} with
an=3.2n-(-1)n

2) Find the explicit formula for the fibonacci numbers.


OR
Find the solution of recurrence relation of fibonacci series.
a0=0
a1=1
an=an-1+an-2
The characteristic equation of a n=an-1+an-2 is
rn=rn-1+rn-2
(since assume an=rn then rn=rn-1+rn-2)
rn/rn-2=rn-1/rn-2+rn-2/rn-2
r2-r-1=0
+1±√(−1)2−4(1)(−1)
r= 2∗1
1±√5
r= 2
1+√5
r1 = 2
1−√5
r2 = 2
Then fibonacci numbers are given by
an=α1r1n+ α2r2n
= α1((1+√5)/2)n+ α2((1+√5)/2)n

Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 84
For some constants α1 and α2 , using a0=0 and a1=1
When n=0, a0=0
a0= α1 + α2
α1= -α2
When n=1,a1=1
a1= α1 ((1+√5)/2)1+ α2((1+√5)/2)1
1 = -α2 ((1+√5)/2)+ α2((1+√5)/2)
= α2 (-1/2-√5/2+1/2-√5/2)
= α2 (-2√5/2)
α2=-1//√5
α1=1//√5

Hence, an=1/√5 ((1+√5)/2)n+ (-1/√5 )((1-√5)/2)n

3) What is the solution of recurrence relation


an=6an-1+9an-2 with a0=3 and a1=7
Solution,
an=6an-1+9an-2
Let an=rn
we get,
rn=6rn-1+9rn-2 ................. (1)
Dividing equation (1) by rn-2, we get
r2-6r-9=0
Solving,
We get....
....

Theorem :
Let c1 and c2 be real number with c2≠0. Suppose that r2-c1r-c2=0 has only one root r0. A sequence
{an} is a solution of recurrence relation a n=c1an-1+c2an-2 iff an= α1r0n+ α2nr0n, for n=0,1,2,….
Where α1 and α2 are constant.

Theorem :
Let c1, c2, …….. ck be real numbers. Suppose that the characteristic equation.
rk-c1 rk-1 …….-ck=0
has k distinct roots r1, r2,……rk then sequence {an} is a solution of recurrence relation.
an=c1(an-1)+c2an-2+……+ckan-k iff
an= α1r1n+ α2r2n+ ….. + αkrknfor n=0,1,2.... where α1, α2, α3,......... αk are constants.

Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 85
Theorem :
Let c1, c2,…....ck be real numbers, suppose that the characteristics equation
rk-c1rk-1 ….. –ck=0
has t distinct roots r1, r2...........rk with multiplication m1, m2, m3,…..mt respectively so that mi≥1
for 1,2,….t and m1+m2+… +mk=k. Then a sequence {an} is a solution of the recurrence relation
an=c1an-1+c2an-2+…...+ckan-k iff
an=( α1,0+ α1,1n+..........+ α1,m1-1nm1-1)r1n+
(α2,0+ α2,1n+..........+ α2,m2-1nm2-1)r2n+....
.....+ ( αt,0+ αt,1n+..........+ αt,mt-1nmt-1)rtn

For n=0,1,2,…. Where αi,j are constants for 1≤ i ≤ t and 0 ≤ j ≤ mi-1

Suppose that roots of the characteristics equation of a linear homogeneous recurrence relation are
2, 2, 2, 5, 5 and 9 (that is there are three roots, the root 2 with multiplicity two three, the root 5
with multiplicity two and the root 9 with multiplicity one.
What is the form of general solution?
(α1,0+ α1,1 n+ α1,2 n2)2n+( α2,0+ α2,1 n)5n+ α3,09n
1) an=6an-1-9an-2 with a0=1, a1=6
Find the solution of given recurrence relation.
Solution:
an=6an-1-9an-2
Let an=rn, we get
rn=6rn-1-9rn-2, we get,
r2=6r-9
r2-6r+9=0 ................(1)

This is a characteristic equation,


Solving the equation (1)
r=3,3
Hence,
an= α13n+ α2n3n for some constants , applying initial condition.
Here, a0=1
a0= α1
α1=1

Here a1=6
a1= α13+ α23
6=3+ α23
α2=1
Hence the solution is an = 3n+n3n

(2) Find the solution to the recurrence relation an=6an-1 - 11an-2+6an-3


With the initial conditions a0=2, a1=5 and a2=15.

Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 86
Solution:
The characteristic equation is given by
r3-6r2+11r-6=0
Or, r3-r2-5r2+5r+6r-6=0
Or, r2(r-1)-5r(r-1)+6(r-1)=0
Or, (r-1)(r2-5r+6)=0
Or, (r-1)(r2-3r-2r+6)=0
Or, (r-1)[r(r-3)-2(r-3)]=0
Or, (r-1)(r-2)(r-3)=0

r1=1, r2=2, r3=3.


The solution is then given by
an= α1.1n+ α2.2n+ α3.3n

For some constants α1, α2, and α3


Finding α1, α2 and α3 using initial conditions
a0=2= α1+ α2+ α3.............(1)
a1=5= α1+ α2.2+ α3.3........(2)
a2=15= α1+ α2.4+ α3.9......(3)

Solving equation (1),(2), and (3), we get


α1=1, α2=-1, α3=2

Hence the solution is given by


an=1-2n+2.3n

3) Find the solution to the recurrence relation an=-3an-1=3an-2-an-3 with initial conditions a0=1,
a1=-2 and a2=-1
The characteristic of this recurrence relation is
r3+3r2+3r+1=0
(r+1)3=0
r1=r2=r3=-1 i.e. there is a single root r=-1 of multiplicity three of characteristic equation.
The solution is given by,
an=( α1,0+ α1,1n+ α1,2n2)(-1)n
=( α1,0(-1)n + α1,1n(-1)n + α1,2n2 (-1)n
To find the constants α1,0, α1,1 and α1,2 use the initial condition. This gives
a0=1= α1,0....................................(1)
a1=-2= α1,0(-1)+ α1,1(-1)+ α1,2(-1)
a1=-2= -α1,0- α1,1- α1,2........................(2)
a2=-1=( α1,0(-1)2 + α1,12(-1)2 + α1,222 (-1)2
a2=-1= α1,0 + 2α1,1 + 4α1,2.........................(3)

Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 87
Solving (1),(2) and (3), we get,
α1,0=1, α1,1=3, α1,2=-2
Hence, the solution is given by,
an=1(-1)n+3n(-1)n+(-2)n2(-1)n
an=(1+3n-2n2)(-1)n

Tower of Hanoi Puzzle


There are 3 pegs and n circular disks of increasing on one peg, with the largest on the bottom.
These disks are to be transferred one at a time onto the another peg, with the provision that larger
disk is not allowed to place on a smaller disk. The problem is to determine the number of moves
necessary for the transfer.

Solution:
Let hn be the number of moves required to transfer n disk. We verify that h0=0, h1=1 and h2=3.
We have to find a recurrence relation that is satisfied by h n.

Peg 1 Peg 2 Peg 3


To transfer n disks to another peg, we first transfer the top n-1 disks to a peg, transfer the largest
disk to the vacant peg and then transfer all n-1 disks to the peg, which now contain the largest
disk, this hn satisfies hn=2hn-1+1(n≥1) where h0=0
This is a recurrence relation of order 1 with constant coefficients, but it is not homogeneous,
because of the presence of the quantity.
1. To find hn, we iterate the above equation.
hn=2hn-1+1
=2(2hn-2+1)+1
=22hn-2+1
=22(2hn-3+1)+2+1
=2n-1(2h0+1)+2n-2+….+22+2+1
=2n-1+2n-2+…..+22+2+1
1(2𝑛 −1) 𝑎(𝑟 𝑛−1)
= ( since sn= )
2−1 𝑟−1
n
=2 -1
Therefore, the no. hn are the partial sums of the geometric sequence 1,2,2 2,…2n and hence
satisfy.
2𝑛 −1
hn= 2−1 =2n-1
=2n-1
We have to verify this formula for hn and it can be easily verified by mathematical induction.

Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 88
Since h0=0, it holds for n=0
Assume that it holds for n. we then show it holds with n replaced by n+1
i.e. hn+1=2hn+1
=2(2n-1)+1
=2n+1-1
Since it also holds for n+1, hence the formula is verified.

Linear non-homogeneous recurrence relations

To solve the non-homogeneous recurrence relation with constant coefficient, we need to solve its
associated liner homogeneous equation and to find a particular solution for the given non-
homogeneous equation.
A recurrence relation of the form
an=c1an-1 +c2an-2+….+ckan-k+ F(n)
Where c1,c2, ….. ck are real numbers and F(n) is a function not identically zero depending only
on n is linear non-homogeneous recurrence relation. The recurrence relation
an=c1an-1+c2an-2+….. ckan-k
is called the associated homogeneous recurrence relation.

For example,
1) an=an-1+2n
2) an=an-1+an-2+n2+n+1
3) an=3an-1+n3n
4) an=an-1+an-2+an-3+n

These are examples of linear non-homogeneous recurrence relation with constant coefficients
corresponding associated linear homogeneous recurrence relation are
1) an=an-1
2) an=an-1+an-2
3) an=3an-1
4) an=an-1+an-2+an-3

Theorem :
If {an(p)} is a particular solution of the non-homogeneous liner recurrence relation with constant
coefficients.
an=c1an-1+c2an-2+…..+ckan-k+f(n)
Then every solution is of the form {a n(p)+an(h)} where an(h) is a solution of the associated
homogeneous recurrence relation.
an=c1an-1+c2an-2+…..+ckan-k

Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 89
Theorem:
Suppose {an} satisfies the linear recurrence relation.
an=c1an-1+c2an-2+….+ckan-k+f(n)
Where c1,c2,…..ck are real numbers and
f(n)=(btnt+bt-1nt-1+……+b1 n+b0)sn
Where b0, b1............bt and s are real numbers. When s is not a root of the characteristics equation
of the associated linear homogeneous recurrence relation there is a particular solution of the
form.
(ptnt+pt-1nt-1+…..+p1n+p0)sn

When s is a root of this characteristic equation and its multiplicity is m, there is a particular
solution is the form
nm(ptnt+pt-1nt-1+…..+p1n+p0)sn

1) what form does a particular solution of the linear non-homogeneous recurrence relation
an=6an-1-9an-2+f(n).
have when f(n)=3n
f(n)=n3n
f(n)=n22n
f(n)=(n2+1)3n
Solution,
The associated linear homogeneous recurrence relation is an=6an-1-9an-2. Its characteristics
equation, r2-6r+9=(r-3)2=0 has a single root 3, of multiplicity two.

For f(n)=3n
Because s=3 is a root with multiplicity 2, particular solution has the form p 0n23n if f(n)=3n

For f(n)=n3n
The form is n2(p1n+p0)3n if f(n)=n3n

For f(n) =n22n


The form is (p2n2+p1n+p0)2n if f(n)=n22n

For f(n) =(n2+1)3n


The form is n2(p2n2+p1n+p0)3n if f(n)=(n2+1)3n

2) Find all solutions of the recurrence relation an=3an-1+2n. what is the solution with a1=3 ?
Solution,
The associated linear homogeneous equation is an=3an-1
Its solution are an(h)= α3n
Where α is constant
Now, finding particular solution
f(n)=2n is a polynomial of degree one

Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 90
Comparing with f(n)= p(n) sn
we get, s=1
The root of the characteristic equation is 3
Here, s is not the root of characteristic equation of the associated linear homogeneous recurrence
relation, there is a particular solution of the form
(p0+p1n+…..+pt-1 nt-1+ ptnt)sn
=(p0+p1n)sn
=(p0+p1n)
New, equation an=3an-1 + 2n becomes
(p0+p1n)= 3(p0+p1(n-1))+2n
or, (p0+p1n)= 3p0+3p1n-3p1+2n
or, n(p1-3p1-2)= 2p0-3p1
or, n(-2p1-2)= 2p0-3p1
or, n(2p1+2)+ 2p0-3p1=0

It follows that cn+d is a solution if and only if 2+2p1=0 and 2p0-3p1=0


∴ p1=-1, p0= -3/2
an(p)= - n- 3/2
an=an(p) + an(h)
=-n-3/2+α.3n
To find solution with a1=3
n=1, a1=3
3=-1-3/2+3α
6=-2-3+6α
α=11/6
an=-n-3/2+(11/6)3n

3) Find the solution of recurrence relation an=5an-1- 6an-2+7n

Solution,

The solution of its associated homogeneous recurrence relation

an=5an-1- 6an-2

Here,

f(n)=7n

Comparing with f(n)=p(n) sn

s=7

Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 91
Here s is not the root of characteristic equation, there is particular solution in the form

p0=7n

Now, equation an=5an-1-6an-2+7n becomes

p07n=5.p07n-1-6.p07n-2+7n

p0=49/20

an(p)=(49/20)* 7n is a particular solution

an=an(p)+an(h)

=(49/20)* 7n+α13n+α22n

4) Let an be the sum of the first n positive integer so that


an=∑𝑛𝑘=1 𝑘
an satisfies the linear non-homogeneous recurrence relation
an=an-1+n ............ (1)
The associated linear homogeneous recurrence relation are given by
an(h)= c(11)n = c,
Where c is constant
Here,
We have,
f(n)=n
Comparing with f(n)=p(n)sn
s=1
Here, s is the root of characteristic equation. The particular solution is in the form
n(p1n+p0)
=p1n2+p0n (here m=1)
Now,
Equation (1) becomes
p1n2+p0n=p1(n-1)2+p0(n-1)+n
Simplifying we see,
n(2p1-1)+(p0-p1)=0 which means
2p1-1=0 and p0-p1=0
p1=p0=1/2
Hence,
an(p)=n2/2+n/2=n(n+1)/2 is a particular solution
an=an(p)+an(h)
an=n(n+1)/2+c
We have, a1=1, 1=a1=c+(1/2)*2=c+1
c=0
an=n(n+1)/2

Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 92
Solving recurrence relation using substitution
Solve recurrence relation
an2-2a2n-1=1 for n≥1 where a0=1
This non-linear equation can be converted into linear equation by using substitution b n=an2
Now equation become bn-2bn-1=1 with b0=a02=12=1
Solving bn-2bn-1=1 with b0=1, the homogeneous solution is α.2n and particular solution is c.
So, bn=a2n+c where a and c are constants.
Using fact b0=1 and b1=3 we get a+c =1 and 2a+c=3
∴a=2 and b=-1.
Hence the solution is bn=2.2n-1
=2.2n-1
=2n+1-1
Substituting back an2=2n+1-1
an=√2𝑛+1 − 1

Divide and conquer recurrence relations


Divide and conquer relation is of the form an=can/d+f(n). Hence this equation can be looked at as
bk=cbk-1+f(dk) using n=dk and solved. Initial condition have to be changed appropriately. For
substituting back, we use k=logdn.

Q.1) Solve the divide and conquer relation an=3an/2+n


Where, n=2k for k≥1 and a1=1
Using change of variables the equation becomes
bk=3bk-1+2k

The homogeneous solution bk(h)=A3k and the particular solution is bk(p)=B2k


We get,
bk=A3k+B2k
Since,
a1=1, b0=1. So A+B=1
a2=b1=5,
We get,
A=3 and B=-2
So, solution is,
bk=3*3k+2*2k
Substituting back,
an=3.3log2n-2.2log2n
=3.3log2n-2n
But,
3log2n=nlog23
So,
an=3nlog23-2n

Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 93
Analysis of algorithm

Binary search:
Binary search looks for a value in a sorted sequence and returns the index of the value if it is
found or 0 if it is not found.

Input : A sequence si, si+1,…..,sj, i≥1 sorted in increasing order, a value key, i and j.

Output : The output is an index k for which sk=key or if key is not in the sequence the output is
the value 0.

1) Procedure binary search (s, i, j, key)


2) If i>j then return(0) //not found.
3) K=(i+j)/2
4) Key=sk then return (k) //found
5) If key <sk then j=k-1 //search left half
6) Else i=k+1 //search right half
7) (repeat the process) return (binary_search (s. i. j. key))
8) End binary search
This algorithm uses the divide and conquer approach. The sequence is divided into two nearly
equal parts. (line 3)
- If the item is found at the dividing point (line 4), the algorithm terminates.
- If the item is not found, because the sequence is sorted, an additional comparison will located the
half of the sequence in which the item appears if it is present.
- We then recursively invoke binary search (line 7) to continue the search.

Example
Input
s1=2, s2=4, s3=5, s4=6 and key 6
- i=1, j=4, k=index
- Since (i>j) (1>4) is false
- k=2
- Since key(6) is not equal to s4(4)
- Key (6)<sk (6<4) is false
- We set, i=k+1 =3
i=3, j=4
s3=5, s4=6

Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 94
i>j (3>4) is false
k=(3+4)/2=3

- Since key (6) is not equal to s3(5)


- Key (6)<s3 (6<5) is false
We set
i=k+1=3+1=4
i=4, j=4, s4=6

- i>j (4>4) is false


- Set k= (4+4)/2=4
Key (6) is equal to s4 (6). So we return 4, the index of key in the sequence s.

Thus, binary search reduces the search of an element in a search sequence of size n to the binary
search sequence of the element in a search sequence of size n/2 when n is even. Hence the
problem of size n has reduce to one problem of size n has reduced to one problem of size n/2.
Two comparison are needed to implement this reduction ( one to determine which half of the list
to use and other to determine whether any term of the list remain). Hence if f(n) is number of
comparison required to search for an element in a search sequence of size n, then
f(n)=f(n/2)+2

Selection sort
This algorithm sorts the sequence
s1,s2,….,sn
in increasing order by first selecting the largest item and placing it last and then recursively
sorting the remaining elements.
Input : s1, s2, ……, sn and length n of the sequence
Output ; s1, s2, ….., sn arranged in increasing order

1) Procedure selection sort (s, n)


2) Base case, if n=1 then return
3) // find largest value
max_index=1 // assime
4) For i=2 to n do
If si>smax-index then // found larger so up date max_index = i

Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 95
5) // more largest to end
Swap (sn, smax_index)
6) Call selection_sort (s, n-1)
7) End selection_sort
bn=n-1+bn-1

Discrete Structure, Shreekrishna Sulu, Senior Lecturer, Kathmandu Engineering College, Kalimati Page 96

You might also like