Introduction to Computer Systems

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Recursion.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Recursion Chapter 7. Spring 2010CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn how.
Recursion. Binary search example postponed to end of lecture.
Recursion Introduction to Computing Science and Programming I.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Fall 2007CS 2251 Recursion Chapter 7. Fall 2007CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method.
Algorithms. Problems, Algorithms, Programs Problem - a well defined task. –Sort a list of numbers. –Find a particular item in a list. –Find a winning.
Recursion.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 14 Recursion.
Section Section Summary Recursive Algorithms Proving Recursive Algorithms Correct Recursion and Iteration (not yet included in overheads) Merge.
Unit 181 Recursion Definition Recursive Methods Constructing Recursion Benefits and Usage Infinite Recursion Recursion Removal Examples Exercises.
More on Recursion Averting Program Crashes CSC 1401: Introduction to Programming with Java Week 9 – Lecture 3 Wanda M. Kunkle.
The Fundamentals: Algorithms, the Integers & Matrices.
Algorithms. Problems, Algorithms, Programs Problem - a well defined task. –Sort a list of numbers. –Find a particular item in a list. –Find a winning.
Introduction Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily.
Introduction to Computer Systems
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Recursion Review.
Chapter 14: Recursion Starting Out with C++ Early Objects
25 November 2014Birkbeck College1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems
Recursion.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
Sudeshna Sarkar, IIT Kharagpur 1 Functions : Recursion Lecture
Computer Science and Software Engineering University of Wisconsin - Platteville 9. Recursion Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
CP104 Introduction to Programming Recursion 2 Lecture 29 __ 1 Recursive Function for gcd Recursive formula for the greatest common divisor of m and n,
6 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
CSIS 123A Lecture 9 Recursion Glenn Stevenson CSIS 113A MSJC.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
UNIT-I INTRODUCTION ANALYSIS AND DESIGN OF ALGORITHMS CHAPTER 1:
17 November 2015Birkbeck College1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
13 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
3 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
17 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
Chapter 2: Algorithm Analysis Application of Big-Oh to program analysis Logarithms in Running Time Lydia Sinapova, Simpson College Mark Allen Weiss: Data.
Recitation 8 Programming for Engineers in Python.
23 February Recursion and Logarithms CSE 2011 Winter 2011.
Concepts of Algorithms CSC-244 Unit 5 and 6 Recursion Shahid Iqbal Lone Computer College Qassim University K.S.A.
24 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
4 March 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Maitrayee Mukerji. Factorial For any positive integer n, its factorial is n! is: n! = 1 * 2 * 3 * 4* ….* (n-1) * n 0! = 1 1 ! = 1 2! = 1 * 2 = 2 5! =
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
11 March 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Recursion Powerful Tool
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Algorithms.
Chapter 19: Recursion.
Recursion The programs discussed so far have been structured as functions that invoke one another in a disciplined manner For some problems it is useful.
Introduction to Computer Systems
Introduction to Programming
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Introduction to Computer Systems
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Computer Science — An Overview J. Glenn Brookshear
Revision of C++.
And now for something completely different . . .
Introduction to Programming
Presentation transcript:

Introduction to Computer Systems Lecturer: Steve Maybank School of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk Spring 2015 Week 9b: Recursion 24 November 2015 Birkbeck College, U. London

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

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

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

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

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

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

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

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

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

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

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

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 http://www.cs.ucsb.edu/~pconrad/cs8/topics.beta/theStack/06/ 24 November 2015 Birkbeck College, U. London

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

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

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

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

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

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

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

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

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

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