Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computer Systems

Similar presentations


Presentation on theme: "Introduction to Computer Systems"— Presentation transcript:

1 Introduction to Computer Systems
Lecturer: Steve Maybank School of Computer Science and Information Systems Spring 2015 Week 9b: Recursion 24 November 2015 Birkbeck College, U. London

2 Birkbeck College, U. London
Factorial Function 0! = 1 1! = 1 2! = 2x1 = 2 3! = 3x2x1 = 6 4! = 4x3x2x1 = 24 n! = nx((n-1)!) 24 November 2015 Birkbeck College, U. London

3 Birkbeck College, U. London
Pseudocode Version def fact(n): if(n==0): return 1 else: return n*fact(n-1) 24 November 2015 Birkbeck College, U. London

4 Birkbeck College, U. London
Example Call fact(3): value returned is 3xfact(2) Call fact(2): value returned is 2xfact(1) Call fact(1): value returned is 1xfact(0) Call fact(0): value returned is 1 24 November 2015 Birkbeck College, U. London

5 Properties of Recursive Algorithms
The code for fact() is recursive because it includes a call to fact() The included call, fact(n-1) is simpler than the original call fact(n) The sequence of calls to fac() ends at the base case, fact(0), which returns 1 24 November 2015 Brookshire Section 5.6

6 Non-Recursive Version of fact()
def factNR(n): i=1 f=1 while(i <= n): f = i*f i = i+1 return f 24 November 2015 Birkbeck College, U. London

7 Second Recursive Function
def printTriangle(n): i = 1 while(i <= n): print(‘*’) i = i+1 print(newline) if(n > 1): printTriangle(n-1) 24 November 2015 Birkbeck College, U. London

8 Birkbeck College, U. London
Printed Output Call printTriangle(4): **** and call printTriangle(3) Call printTriangle(3): *** and call printTriangle(2) Call printTriangle(2): ** and call printTriangle(1) Call printTriangle(1): * 24 November 2015 Birkbeck College, U. London

9 Informal Definition Recursion occurs when a set of instructions is repeated as a subtask of the original set. 24 November 2015 Brookshear Section 5.5

10 Example of Recursion: Web Pages
readPage(URL) Page 2 B2 A3 Page 3 24 November 2015 Birkbeck College, U. London

11 Recursive Reading of Web Pages
On encountering a link, follow it immediately. On reaching the end of a page, return to the previous page. Order: A1, A2, A3, B2, B1. 24 November 2015 Birkbeck College, U. London

12 Iterative Reading of Web Pages
On encountering a link, remember it. On reaching the end of a page, choose a remembered link and go to the appropriate page. Order: A1, B1, A2, B2, A3. 24 November 2015 Birkbeck College, U. London

13 Why does Recursion Matter?
Certain algorithms can be written down very efficiently using recursion. Many computer languages allow recursion. The usual implementation of function calls using a stack makes recursion straightforward. See 24 November 2015 Birkbeck College, U. London

14 Brookshear Ch 5 revision problem 42
Example Produce a recursive algorithm to print the daily salary of a worker who each day is paid twice the previous day’s salary, starting with one penny for the first day’s work. 24 November 2015 Brookshear Ch 5 revision problem 42

15 Greatest Common Divisor
Let m, n be integers such that m >= 0, n >= 0 and (m, n) ≠ (0, 0). Then GCD(m, n) is the largest integer which divides both m and n GCD(m, n) = GCD(n, m) GCD(m, 0) = m Example: GCD(56, 21) = 7 24 November 2015 Birkbeck College, U. London

16 Recursive Property of GCD
Suppose that m >= n and let q be any integer such that q >= 1 and m >= q*n. Then GCD(m, n) = GCD(m-q*n, n) Example: GCD(56, 21) = GCD(56-2*21, 21) = GCD(14, 21) 24 November 2015 Birkbeck College, U. London

17 Recursive Algorithm for GCD
# assume m >= n > 0 RGCD(m, n) r = m%n; # r=m-q*n if (r == 0) : return n else: return RGCD(n, r) 24 November 2015 Birkbeck College, U. London

18 Birkbeck College, U. London
Example Run of RGCD Call RGCD(256, 120): r = 16, return RGCD(120, 16) Call RGCD(120, 16): r = 8, return RGCD(16, 8) Call RGCD(16, 8): r = 0, return 8 24 November 2015 Birkbeck College, U. London

19 Recursive Algorithm for Binary Search
# test to see if the element a is in the sorted list L def recursiveBinarySearch(L, a): if(Length(L) == 0): return False j = floor(Length[L]/2) if(L[j] == a): return True if(L[j] > a): L1 = sublist of L from index j+1 to Length(L)-1 else: L1 = sublist of L from index 0 to j-1 return recursivebinarySearch(L1) 24 November 2015 Brookshire, Section 5.5

20 quickSort def quickSort(L): if (Length(L) <= 1): return L
split L into sublists L1 and L2 M1 = quickSort L1 M2 = quickSort L2 M = merge(M1, M2) return M 24 November 2015 Brookshear Section 5.5

21 Birkbeck College, U. London
merge def merge(M1, M2): # compare the merge of two if(Length(M1)==0): # sequential files return M2 if(Length(M2)==0): return M1 if(M1[0] <= M2[0]): m=M1[0]; N1=Drop(M1,1); N2=M2; else: m=M2[0]; N1=M1; N2=Drop(M2,1); return Join({m}, merge(N1, N2)) 24 November 2015 Birkbeck College, U. London

22 Example What sequence of numbers is printed by the following recursive function if it is started with N assigned the value 1? def exercise(N): print(N) if(N < 3): exercise(N+1) 24 November 2015 Brookshire Section 5.5

23 Examples What names are interrogated by the binary search when searching for the name Joe in the list Alice, Brenda, Carol, Duane, Evelyn, Fred, George, Henry, Irene, Joe, Karl, Larry, Mary, Nancy and Oliver? What is the maximum number of entries that must be interrogated when applying the binary search to a list of 200 entries? 24 November 2015 Brookshear, Section 5.5


Download ppt "Introduction to Computer Systems"

Similar presentations


Ads by Google