Induction and Recursion

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

d 61 firsi we

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 - ' .
=

preeedure factorial(n: nonnegative integer)


if n =0 then return 1
else return n.factorial(n - 1)
{output is n!}
• n here a is a nonzero real number
Example: Give a recursive algorithm for computing a , w .
'/
and n is a nonnegative integer. . . f
0
Solution: We can base a recursive algorithm on the recursiv~. defiruot~onl T afin. dThis
· ·t· 1 ndition a - · o n a~,
definition states that an+1 = a.an for n > 0 and the 1ru ia co
successively use the recursive step to reduce the exponent until it becomes zero. else
Algorithm retut
procedure power(a: nonzero real number, n: nonnegative integer) 3. End
if n = 0 then return 1 Example
else return a · power(a, n - 1)
Devise a rec
{output is an}
m~2,n~0 1

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 ·

-a.an- 1 £or a>l.


b) Recursive definition·. an-
power (a,n)
1. start
2. if(n==O)
return a;
else
return a*power(a,n-1)
3. End

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

else Ln12J, m)2 mod m. b mod m) mod m


(b
mpower(b, n, m) = (mpower '
{mpower(b, n, m) = bn mod m}
Example · th greatest common divisor of two nonnegative
?ive a recursive algorithm for computing e .
integers a and b with a< b.
. . d( b) = gcd( b mod a, a) and t
ctio n gc a, • Al "thm .
<WLrsjve algo rith m on the redu dure lll gon ·
~ · e
0, bJ = ~ whe n b > O. Thi s prod uces the proc
edu re gcd( a, b: non neg ativ e inte gers with a
< b)
~ a= 0 then gcd( a, b) :=b
else gcd( a, b) :=gc d (b mod a, a)

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 .

using math emati cal induc Ion. comp utmg an and prove that this algor ithm is corre ct

Algorithm for comp uting an


procedure powe r(a: nonze ro real numb e r, n.. nonnegative •
· mtege r)
{
If n==O then powe r(a,n) =l
else ·
powe r (a,n) = a.pow er(a,n -1)
}

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.

Validity of Structural Induction


the math emat ical
The valid ity of struc tural induc tion can be seen as the valid ity of
ed, for all positi ve integ ers
induction. If P(n) deno tes the state ment that is recur sively defin
ence to the basis step of th~~
n. The basis step of the struc tural induc tion meth od corre spond
sive step in the struc turnj
lllathematical induc tion meth od. We can see that the recur
is assum ed alrea dy and th
induction tells if P(k) is true it impli es P(k+ 1), wher e P(k)
emat ical induc tion.
P(k+1) is deriv ed in term s of P(k). Henc e it follows the proof s by math
. al vertices of a full •·
-•setefiaWD .
'th exactly one vertex r. This tree
r ia a 1-af of the full binary tree WI

vertices. . . the union of the set of the lea'I


'Step: The set ofleaves of the tree T = Tl.T2 is the root r of T and the u .ea
the set of leaves of T2. The internal vertices of T ar~ fT Ilion
2 O
aiet ef internal vertices of Tl and the set of internal vertices •

Correctness of Recursive Algorithms


. . d t' can be used to prove that
Mathematical induction and its variant strong m uc ion, a
• . ' . h desired output for all poss·b!
1
recursive algonthm is correct, that is, that 1t produces t e e
input values.
Example 4: Prove that algorithm, explained in example 2, which computes powers of reaj
numbers, is correct.
Solution:
Basis Step: If n = 0, the first step of the algorithm tells us that power (a, 0) = l. This is
correct because a 0 = 1 for every nonzero real number a. This completes the basis step.
Inductive Step: The inductive hypothesis is the statement that power (a, k) = ak for an
arbitrary positive integer k. That is, the inductive hypothesis is the statement that the •
algorithm correctly computes a".
Using this hypothesis, we show that the algorithm correctly computes ak+i. Now, power
(a, k + 1) = a.power (a, k). By the inductive hypothesis, we have power (a, /.,) = ak, so power
(a, k + 1) = a ·· power (a, k) = a · air = a1t+ 1• This completes the inductive step. We have
completed the basis step and the inductive step, so we can conclude that the algorithm
always computes an correctly when a 'I- 0 and n is a nonnegative intc~er.

Recursion and Iteration


Recursion
ltuntion
Function calls itself. A t-wt of ini;tructions repeatedly executed.
For functions. For loopH ,
Through base case, where there
will be no function call. ~h<m the termination condition for th 1'
'"'"'V" lti LC l;;\ t·I HI,..10( 1,
iterator C"''"""
Used when code size needs to be Used wh Pn t1mo · comph•xit.y JH't•d~ to Iit'
small, and time complexity is not bn la nco<l 11 ..,,unsL
an issue.
.,. · ,
11111•x1rnnd1•d cot 11• i,;:i,,,,,i.

Smaller code size I --- _ _ _ -


,nr1-:1~r Code Hizl•. - .
Very high(generally exponuntial) ltt•lntivt'ly ,](it¥
time complexity. ( 1own tinw rollll'11 •I
- - - - -- - - - - - - - l ~K~t-
!
~~[l t•:'.r1~1l~ly~r~io~I , . ),
• Yno1n1nl -lo~11rit hn11<· ,
·----- ·.
.....................
at sum of first n as.:.,
&.)r,.~..1%-W--.u;:i iPJ l .tofAJiC,";.£t.."i'.f."k,ifl(C,'4$1if';tft;tR 4 CWJQ Z.7A.i4&W\F:& L ;

. even positive integer is n(n+ 1)


.e. 2 + 4 + 6 +.......... ............. 2n = n(n + 1)
se mathematical induction t o prove that
1 +2 +22+ 23 + ....................... +2n= 2n+l_ l

3. Use mathematical induction t 0 . . ·


• . prove the inequality n < 2n for all positive integers.
4. Use mathematical induction t
4
112:.
° prove that 2n < n! for every positive integer n with

5. Use mathematical induction to show that

12 + 22 + 32 + .......... n 2 = n(n + 1) (2n + 1) . . . .


6 , where n 1s pos1t1ve mteger

6. Use mathematical induction to prove that


.
. non- negative
l2 + 22 + 52 + .. . . .. . . . . (2n + l). 2 -_ (n + 1) (2n + 1) (2n + 3) , where n 1s
3
integer
7. Using mathematical induction prove that
1
1.2 + 2.3 + 3.4 + ..... + n(n + 1) = 3 n(n + 1) (n + 2) for all neN

8. Prove that 3n < n! whenever n is a positive integer greater than 6.


9. Show that 2n > n 2 whenever n is an integer greater than 4.
10. Use mathematical induction to prove tha n! < nn whenever n is a positive integer
greater than 1.
11. Use mathematical induction to prove that l3+2 3+3 3+43+ ... +n 3 = (n(n+l)/2) 2
12. Use mathematical induction to prove that for every positive integer n,
n(n+ l)(n+ l)(n+2)
1.2 + 2.3 + 3.4 + .... + n(n + 1) = 3

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.

□□□

You might also like