0% found this document useful (0 votes)
16 views31 pages

Chapter 4 - Recursion

Uploaded by

mtdeptrai430
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
16 views31 pages

Chapter 4 - Recursion

Uploaded by

mtdeptrai430
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 31

Chapter 4

Induction and Recursion


Recursion - Objectives
• Recursive definitions
– Sequences, functions
– Sets
• Recursive Algorithms
– Fibonacci numbers
– Binary search
– Merge sort
– More exercises (optional)
Recursion - Introduction
• Fibonacci numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34,

n 1 2 3 4 5 6 7
Fn 1 1 2 3 5 8 13
F8 = ?
F20 = ?
F19 = 4181 F20 = F19 + F18 = 6765
F18 = 2584
Recursive definition of Fibonacci
numbers
procedure fibo(n: positive integer)
if n=1 or n = 2
return 1
• basis step else recursively
F1 = 1, F2 = 1
return fibo(n-1) + fibo(n-2)
• recursive step
Fn = Fn-1 + Fn-2, for n > 2
Recursive definition
• a[n] = 5n – 2, n = 1, 2, 3, …
• a[1] = ?
• a[10] = ?
• Recursive:
compute a[n] from a[n-1]
Recursive definition for
a[n] = 5n – 2, n = 1, 2, 3, …
• a[n] = 5n – 2
• a[n-1] = 5(n-1) – 2 = 5n – 7
Try to find a rule for computing a[n] from a[n-1]
a[n] = a[n-1] + 5 if n > 1 # recursive step
a[1] = 3 # basis step
Recursive algorithm
a[1] = 3 if n = 1
a[n] = a[n-1] + 5 if n > 1

procedure tinh(n: positive integer)


if n=1
return 3
else recursively

return tinh(n-1) + 5
Do yourself
• Find a recursive definition of the function/sequence:
1. a[n] = an = n, n =1, 2, 3, …
2. f[n] = f(n) = 1 + 2 + 3 + … + n, n = 1, 2, 3, …
f(n) = 1 + 2 + 3 + … + n, n = 1, 2,
• f(n) = 1+2 + … + (n-1) + n
• f(n-1) = 1+2 + … + (n-1)
 f(n) = f(n-1) + n, if n > 1
and f(1) = 1
procedure tinhf(n: positive integer)
if n = 1
return 1
else
return tinhf(n-1) + n

What is the result after calling tinhf(5)?


procedure thuattoan(a: real number, n: non-negative
integer) Recursive definition of an
if n = 0 an = 1 if n = 0
an = a*an-1 if n > 0
return 1
else
return a*thuattoan(a,n-1)

What is the result after calling thuattoan(2,3)?


Binary search
procedure bs(x,a,id,ic)
if (id > ic)
return -1
else
im : = (id+ic) div 2
if (a[im] = x) then
return im
else if (x > a[im])
return bs(x,a,im+1,ic)
else
return bs(x,a,id,im-1)
Recursively Defined Sets and Structures
The set A is defined recursively by:
• 3A
procedure check(x: positive integer)
• x  A if x-3  A
if (x=3)
A = {3, 6, 9, 12, 15, …} return 'OK'
else if (x<3)
return 'Not OK'
else
return check(x-3)
Algorithm to check if a positive integer belongs to A
Sets and Structures
S is the set of bit strings defined recursively by
– The string 10  S
– String 1w0  S if string w  S

What is S?
S = {10, 1100, 111000, …}
Do yourself
Define the sets recursively
Ex. A = {3, 6, 9, …}
• 3A
• B = {4, 12, 36, 108, …} • x  A  x +3  A
• C = {1,1/3,1/9,1/27,…}
• S = {100, 110000, 111000000, 111100000000,
…}
Ex. S = {10, 1100, 111000, …}
• 10  S
• w  A  1w0  A
The merge sort - idea
mergesort(4,7,2,6,9,3,1,8,5)

4 7 2 6 9 3 1 8 5
mergesort(4,7,2,6) mergesort(9,3,1,8,5)
 return 2 4 6 7 return 1 3 5 8 9

merge(2 4 6 7 and 1 3 5 8 9)
 return 1 2 3 4 5 6 7 8 9 sorted
472893165

4728 93165

47 2 8 93 165

4 7 2 8 9 3 1 6 5

6 5
6 5

4 7 2 8 9 3 1 5 6

47 2 8 39 156

2478 13569

1 2 3 4 5 6 7 8 9
The Merge Sort Algorithm
procedure mergesort(L = a1,...,an)
if n> 1 then
m := floor(n/2)
recursively
L1 := a1,a2,...,am
L2 := am+1,am+2,...,an
L := merge(mergesort(L1),mergesort(L2))
{L is now sorted}

Time complexity: O(nlogn)


How to merge?
L1: 2 4 6 7  L2: 1 3 5 8 9

smallest of L1 compares with smallest of L2

1< 2 2<3 3<4 4<5 5<6 6<8 7<8

L: 
How to merge?
procedure merge(L1,L2: sorted lists)
L := empty list
while L1 and L2 are both nonempty
remove smaller of first elements of L1
and L2 from its list; put it at the right end of
L
if one list is empty then
remove all elements from the other list and
append them to L
return L {merged}
pseudocode
procedure merge(l1, l2: increasing order)
n := length(l1) m := length(l2)
O(n+m)
i := 1 j := 1 k := 1
while (i  n & j  m ) # both l1 and l2 are not empty
if (l1[i] < l2[j]) # compare
l[k] := l1[i] # remove the smaller one, put it in l[ ]
i := i+1 # update the smallest of l1
else
l[k] := l2[j] # remove the smaller one, put it in l[ ]
j := j+1 # update the smallest of l2
k := k+1
while (i  n ) # if l1 is not empty,
l[k] := l1[i] # append all elements of l1 to l[ ]
i := i+1 k := k+1
while (j  m) # if l2 is not empty,
l[k] := l2[j] # append all elements of l2 to l[ ]
j := j+1 k := k+1
return(l) {l is the final sorted list}
Recursion and Iteration

procedure rfibo (n: nonnegative integer)


if n=0 then rFibo(0)=0
else if n=1 then rFibo(1)=1
else rFibo(n) := rFibo(n-2) + rFibo(n-1)

procedure iFibo (n: nonnegative integer)


if n=0 then y:=0
else if n=1 then y:=1
else
x:=0 ; y:=1
for i:= 2 to n
z:= x+y
x:= y
Recursive algorithm uses far
y:=z
return(z) more computation than
{ iFibo(n) = z} iterative one
Induction - idea
• How to arrange dominos?
p[1] = True
for k:=1 to 10 do
if (p[k] = True) then
p[k+1] = True
print p
Mathematical Induction
• Domino Arrangement and Mathematical
Induction
Mathematical Induction

• Basis step. Verify that P(1) is true


• Inductive step.
Prove that P(k)  P(k+1) is true for all k 
1

• Prove that "n2 + n is an even“ is true for every integer n >


0.
(1) Suppose for every integer k > 0, k2 + k is even
(2) 12 + 1 = 2 is even
Make a correct order!
(3) Therefore, the statement is true.
(4) We have,
(k+1)2 + (k+1) = k2 + 2k + 1 + k + 1
= k2 + k + 2(k+1).
This sum is even.
Induction: Example 1
Prove that 1 + 2 + 3+ …+ n = n(n+1)/2 for all integers
n>0
the smallest value of n (n=1)

• Basis step:
“1 = 1(1+1)/2”  true ?
easy
P(1) is true

• Inductive step: With arbitrary k > 0, suppose


?
easy
“ 1+ 2+…+ k = k(k+1)/2”
is true.
P(k)

• So
1+2+3+…+k+(k+1) = k(k+1)/2 + (k+1)

?
Do some
math
skills
= [k(k+1)+ 2(k+1)]/2

= (k+1)(k+2)/2
P(k+1)

Proved.
1/ Prove that 2n < n!, for integer n  4 using induction.

2/ Given the algorithm


procedure tinh(n : non-negative integer)
if (n = 0) return 1
else return n*tinh(n-1)

Using induction to prove that tinh(n) can be used for


computing n! for n  0.
Summary
• Recursive definitions
– Sequences, functions
– Sets
• Recursive Algorithms
– Fibonacci numbers
– Binary search
– Merge sort
THANKS

You might also like