Induction and Recursion
Induction and Recursion
Induction and Recursion
we c Sis: ., ·te 4! = 4.
ea.tecllY tD -.i
e J1ecwrsive steP :r,ep ,..:,.g back through t
, - 1 and wor- 4 d 51
· g the value of O. - _: 41 == 4.3! == 4.6 = 2 , an ·
6
. - , 2! = = =
2.1! 2, 3! 3.2! 3·2 - ' .
=
Example: Give a recursive algorithm for computing the greatest common divisor of two
Solution
nonnegative integers a and ·b with a< b.
We can bas1
Solution: We can base a recursive algorithm on the reduction gcd(a, b) = gcd(b mod a, a) and
I
j'
the condition gcd(O, b) = b when b > 0. For example, gcd(14, 21) = g~d(7, 14) = gcd(O, 7) = 7. the initial 0
Algorithm: However, 1
procedure gcd(a, b: nonnegative integers with a< b)
Pseudocode
if a = 0 then return b Pro
else return gcd(b mod a, a) ~n
{output is gcd(a, b)}
An algorithm is called recursive if it solves a problem by reducing
· • h
. . 1t into 1·nsta ce oft e
same problem with smaller mput. n
Example
An algorithm to fmd the factorial of given number
Input: an inter number n
AJcerithm:
ladoria1 (n) //name of algorithm
f=n*factorial(n-1)
3, Stop
Example
A recursive algorithms for an
.
The power function can be defined recursively as:
a) Base case: ao=l ·
Example
Devise a recursive algorithm for computing bn mod m, where b, n, and mare integers with
m ~ 2, n ~ 0, and 1 ~ b < m.
Solution
1
We can base a recursive algorithm on the fact that bn mod m = (b. (bn- mod m)) mod m and
the initial condition b 0 and m = 1.
However, we can devise a much more efficient recursive algorithm, which we describe in
pseudocode as Algorithm 2.
procedure mpower(b, n, m: integers with m ~ 2, n ~ O; 1 ~ b < m)
if n = 0 then
mpower (b, n, m) = 1
else if n is even then
mpower(b, n, m) = mpower(b, n/2, m)2 mod m
ch algo rith m.
Con stru ct a recu rsiv e vers ion of a bina ry sear
Sol utio n: We
a1, a2, ....,an. To perf orm a bina ry sear ch,
Sup pos e we wan t. to loca te x in the sequ ence e if l ,
in by com pari ng x with the mid dle term , 8(_(n+1)t2J. Our algo rith m will term inat
beg ly, ~.
erw ise, we redu ce the sear ch to a sma ller sear ch sequ ence , name
equ als this term . Oth
than the mid dle term of the orig inal sequence
,1
the firs t half of the sequ ence if x is sma ller to the J.
redu ced the solu tion of the sear ch prob lem
and the seco nd half othe rwis e. We hav e
app roxi mat ely half as long.
solu tion of the sam e prob lem with a sequ ence
proc edu re bina ry sear ch (x, i, j)
m := LCI + j)12 J
ifx = am then
loca tion := m
else if (x < am and i < m) then
bina ry sear ch(x , i, m - 1)
else if (x > am and j > m) then
I .
bina ry sear ch (x, m + 1, j)
else loca tion := 0.
i Nu mb er
Recursive Algorithm for Finding Fibonacc
proc edu re fibo nacc i(n: non neg ativ e inte ger)
if n = 0 then fibo nacc i(0) := 0
else if n = 1 then fibo nacc i(l) : = 1
i(n _ 2)
!. , else fibo nacc i(n) := .fibo nacc i(n - 1) + fibo nacc
Exa mp le
Wri te a recu rsiv e algo rith m for mer ge sort
Sol utio n:
pro ced ure mer ges ort (L = a1a2aa .... an)
{
lfn >l the n
m = n/2;
L1 = a1a2aa .... am;
L2 = am+ lam +2 .. ..an
e merge (L1,~ : sorte d list}
L = "emp ty list"
.. t y
,vbile L1 and L2 are both .none...&41,p
begin:
e small er of first e1emen t of L 1 a d I.a fr .
· remov
. . n
. . . .
om the list 1t 1s m and put 1t at the L
if remov al of this el · t
emen t make s one 1Is empty then remove all eleme nts from the
other list and appen d them to L
end
Exam ple
Write an recur sive algor ithm for .
t·
using math emati cal induc Ion. comp utmg an and prove that this algor ithm is corre ct
Structural Induction
emati cal induc tion called
While provi ng the recur sively defin ed sets ~e use a form of math
structural induc tion. This meth od consi sts two parts .
in the basis step of the
Basis Step: Show that the resul t holds for all eleme nts specified
recursive defin ition to be in the set.
of the eleme nts used to
Recursive Step : Show that if the statem ent is true for each
resul t holds for these new
construct new elem ents in the recur sive step of the defin ition, the
elements.
13. Use mathematical induction to prove that for every positive integer n,
n(n+ l)(n+2)(n+3)
1.2.3 + 2.3.4 + .... + n(n + l)(n + 2) - ' 4
14. Prove that 2 divides n2+n whenever n is a positive integer usmg mathematical
induction
16. Prove that divides n3+2n whenever n is a positive integer using mathematical
3
induction
. . • • · teger using mathe111a..:11
e·that 5 diVJ.des n 6 - n whenever n is a positive in "
jf.duction ..
Prove that 6 divides ns - n whenever n is a positive integer using mathematicaJ.
induction
18. Prove that n2 _ 1 is divisible by 8 whenever n is odd positive integer using
mathematical induction
19. Find f(2), f(3), f(4) and f(5) if f is defined recursively by f(O) = - 1, f(l) = 2 and for n::: 1
2, . . . . ,
(a) f(n + 1) = f(n) + 3f(n - 1)
(b) f(n + 1) ::= f(n)2 f(n- 1)
(c) f(n + 1) = 3f(n) 2 - 4f(n - 1)2
(d) f(n + 1) = f(n-1)/f(n).
20. Give a recurs·ive d efin1tion
· · of the sequence {an}, n = 1, 2, 3, ..... if
(a) an·=: 4n - 2
(b) an= 1 + (-l)n
(c) an= n(n + 1)
(d) an= n2.
□□□