Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

# Saving this for later?

### Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Standard text messaging rates apply

# CPSC 125 Ch 2 Sec 4

486
views

Published on

Recursion

Recursion

Published in: Education

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• Be the first to comment

• Be the first to like this

Views
Total Views
486
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
18
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. Proofs, Recursion and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co. and modiﬁed by David Hyland-Wood, UMW 2010 Proofs, Recursion and Analysis of Algorithms Wednesday, February 10, 2010
• 2. Recursive Sequences ● Deﬁnition: A sequence is deﬁned recursively by explicitly naming the ﬁrst value (or the ﬁrst few values) in the sequence and then deﬁning later values in the sequence in terms of earlier values. ● Examples: S(1) = 2 ■ S(n) = 2S(n-1) for n ≥ 2 ■ • Sequence S ⇒ 2, 4, 8, 16, 32,…. ■ T(1) = 1 ■ T(n) = T(n-1) + 3 for n ≥ 2 • Sequence T ⇒ 1, 4, 7, 10, 13,…. ■ Fibonaci Sequence ■ F(1) = 1 ■ F(2) = 1 ■ F(n) = F(n-1) + F(n-2) for n > 2 • Sequence F ⇒ 1, 1, 2, 3, 5, 8, 13,…. Section 2.4 Recursive Deﬁnitions 2 Wednesday, February 10, 2010
• 3. Fibonaci Sequence ● Related mathematically to fractals ■ …and thus to computer graphics. Section 2.4 Recursive Deﬁnitions 3 Wednesday, February 10, 2010 Thanks to http://www.flickr.com/photos/pagedooley/2216914220/
• 4. Recursive function ● Ackermann function n+1 m=0 A(m,n) = A(m-1, 1) for m > 0 and n = 0 A(m-1, A(m,n-1)) for m > 0 and n > 0 ■ Find the terms A(1,1), A(2,1), A(1,2) A(1,1) = A(0, A(1,0)) = A(0, A(0,1)) = A(0,2) = 3 A(1,2) = A(0, A(1,1)) = A(0, 3) = 4 A(2,1) = A(1, A(2,0)) = A(1, A(1,1)) = A(1, 3) = A(0, A(1,2)) = A(0, 4) = 5 Using induction it can be shown that A(1,n) = n + 2 for n = 0,1,2… A(2,n) = 2n + 3 for n = 0,1,2… Section 2.4 Recursive Deﬁnitions 4 Wednesday, February 10, 2010
• 5. Recursively deﬁned operations ● Exponential operation ■ a0 = 1 ■ an = aan-1 for n ≥ 1 ● Multiplication operation ■ m(1) = m ■ m(n) = m(n-1) + m for n ≥ 2 ● Factorial Operation ■ F(0) = 1 ■ F(n) = nF(n-1) for n ≥ 1 Section 2.4 Recursive Deﬁnitions 5 Wednesday, February 10, 2010
• 6. Backus–Naur Form ● A recursive symbolic notation for strings, used to deﬁne computer programming language syntax ● e.g. Section 2.4 Recursive Deﬁnitions 6 Wednesday, February 10, 2010 Originally used to define the syntax for Algol The example is from IETF RFC 2616, HTTP 1.1
• 7. Recursively deﬁned algorithms ● If a recurrence relation exists for an operation, the algorithm for such a relation can be written either iteratively or recursively ● Example: Factorial, multiplication etc. S(1) = 1 S(n) = 2S(n-1) for n ≥ 2 S(integer n) //function that iteratively computes the value S(n) Local variables: integer i ;//loop index CurrentValue ; S(integer n) //recursively calculates S(n) if n =1 then if n =1 then //base case output 2 output 2 j=2 else CurrentValue = 2 return (2*S(n-1)) while j ≤ n end if CurrentValue = CurrentValue *2 end function S j = j+1 end while return CurrentValue end if Section 2.4 Recursive Deﬁnitions 7 Wednesday, February 10, 2010
• 8. Why Write Recursive Functions? ● Because CPU operations are cheap … and getting cheaper, ● But code is expensive to maintain. Section 2.4 Recursive Deﬁnitions 8 Wednesday, February 10, 2010 “Several measures of digital technology are improving at exponential rates related to Moore's law, including the size, cost, density and speed of components.” Thanks to http://en.wikipedia.org/wiki/File:Transistor_Count_and_Moore%27s_Law_-_2008.svg
• 9. Recursive algorithm for selection sort ● Algorithm to sort recursively a sequence of numbers in increasing or decreasing order Function sort(List s,Integer n) //This function sorts in increasing order if n =1 then output “sequence is sorted” //base case end if max_index = n //assume sn is the largest for j = 2 to n do if s[j] > s[max_index] then max_index = j //found larger, so update end if end for exchange/swap s[n] and s[max_index] //move largest to the end return(sort(s,n-1)) end function sort Section 2.4 Recursive Deﬁnitions 9 Wednesday, February 10, 2010 Fixed max_index definition to correct algorithm
• 10. Selection Sort Example ● Sequence S to be sorted: ■ S: 23, 12, 9, –3, 89, 54 ● After 1st recursive call, the sequence is: Swap 89 and 54 ■ S: 23, 12, 9, -3, 54, 89 ■ ● After 2nd recursive call: ■ Nothing is swapped as elements happen to be in increasing order. ● After 3rd recursive call, the sequence is: Swap -3 and 23 ■ S: -3, 12, 9, 23, 54, 89 ■ ● After 4th recursive call: ■ Swap 12 and 9 ■ S: -3, 9, 12, 23, 54, 89 ● After 5th recursive call, the sequence is: ■ Nothing is swapped as elements happen to be in increasing order. ● Final sorted array S: -3, 9, 12, 23, 54, 89 Section 2.4 Recursive Deﬁnitions 10 Wednesday, February 10, 2010 Fixed run of algorithm to be correct
• 11. Recursive algorithm for binary search ● Looks for a value in an increasing sequence and returns the index of the value if it is found or 0 otherwise. Function binary_search(s, j, k, key) if j > k then //not found return 0 end if m = (j+k)/2 if key = sk then // base case return (m) end if if key < sk then k = m-1 else j = m+1 end if return(binary_search(s,j,k,key)) end binary_search Section 2.4 Recursive Deﬁnitions 11 Wednesday, February 10, 2010
• 12. Binary Search Example ● Search for 81 in the sequence 3, 6, 18, 37, 76, 81, 92 ■ Sequence has 7 elements. ■ Calculate middle point: (1 + 7)/2 = 4. ■ Compares 81 to 37 (4th sequence element): no match. ■ Search in the lower half since 81 > 37 ■ New sequence for search: 76 81 92 ■ Calculate midpoint: (5 + 7)/2 = 6 ■ 6th Element: 81 Compares 81 to 81: perfect match ■ Element 81 found as the 6th element of the sequence Section 2.4 Recursive Deﬁnitions 12 Wednesday, February 10, 2010
• 13. Class exercise ● What does the following function calculate? Function mystery(integer n) if n = 1 then return 1 else return (mystery(n-1)+1) end if end function mystery ● Write the algorithm for the recursive deﬁnition of the following sequence ■ a, b, a+b, a+2b, 2a+3b, 3a+5b Section 2.4 Recursive Deﬁnitions 13 Wednesday, February 10, 2010