CSI 32. Lecture 15. Chapter 11 Recursion Functional Recursion 11.4 Binary Search

Similar documents
0LOOLRQV (QGLQJÃ%DODQFHVÃKDYHÃEHHQÃUHGXFHG PHHWÃGHEWÃVHUYLFHÃDQGÃRSHUDWLQJ FDVKÃIORZÃUHTXLUHPHQWV \HDUV SURMHFWHGÃLQÃ )LVFDOÃ<HDU

Representations of Palindromic, Prime and Number Patterns

You should know the first sum above. The rest will be given if you ever need them. However, you should remember that,, and.

Instructors: Daniel Deutch, Amir Rubinstein, Teaching Assistants: Amir Gilad, Michal Kleinbort

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

recursive algorithms 1

Recursion defining an object (or function, algorithm, etc.) in terms of itself. Recursion can be used to define sequences

Algorithms in Systems Engineering ISE 172. Lecture 1. Dr. Ted Ralphs

61A LECTURE 6 RECURSION. Steven Tang and Eric Tzeng July 2, 2013

CS 161 Intro to CS I. Finish Pointers/Start Recursion

Emil Sekerinski, McMaster University, Winter Term 16/17 COMP SCI 1MD3 Introduction to Programming

CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion

Announcements. Project 5 is on the street. Second part is essay questions for CoS teaming requirements.

RECURSION. Data Structures & SWU Rachel Cardell- Oliver

1. Find f(1), f(2), f(3), and f(4) if f(n) is defined recursively by f(0) = 1 and for n = 0, 1, 2,

Recursion continued. Programming using server Covers material done in Recitation. Part 2 Friday 8am to 4pm in CS110 lab

15110 Principles of Computing, Carnegie Mellon University - CORTINA. Binary Search. Required: List L of n unique elements.

QUIZ. 0] Define arrays 1] Define records 2] How are arrays and records: (a) similar? (b) different?

Functions and Recursion

RECURSION: n. SEE RECURSION 3

CS103L SPRING 2017 UNIT 8: RECURSION

CISC 3115 TY3. C21a: Recursion. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/6/2018 CUNY Brooklyn College

CSc 110, Spring 2017 Lecture 39: searching

More Complicated Recursion CMPSC 122

Algorithms and Programming

Outline: Search and Recursion (Ch13)

Recursion. COMS W1007 Introduction to Computer Science. Christopher Conway 26 June 2003

Algorithm Analysis CISC4080 CIS, Fordham Univ. Instructor: X. Zhang

UNIT 5B Binary Search

Chapter Summary. Recursively defined Functions. Recursively defined sets and Structures Structural Induction

Solving problems by recursion

17 February Given an algorithm, compute its running time in terms of O, Ω, and Θ (if any). Usually the big-oh running time is enough.

Outline. Simple Recursive Examples Analyzing Recursion Sorting The Tower of Hanoi Divide-and-conquer Approach In-Class Work. 1 Chapter 6: Recursion

Recursion. Chapter 11. Chapter 11 1

2: Complexity and Recursion

Recursion. Example R1

General Instructions. You can use QtSpim simulator to work on these assignments.

Computer Science 1001.py. Lecture 11: Recursion and Recursive Functions

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #19. Loops: Continue Statement Example

ALGORITHM DESIGN DYNAMIC PROGRAMMING. University of Waterloo

Last week. Another example. More recursive examples. How about these functions? Recursive programs. CSC148 Intro. to Computer Science

Recursion defining an object (or function, algorithm, etc.) in terms of itself. Recursion can be used to define sequences

Topics Applications Most Common Methods Serial Search Binary Search Search by Hashing (next lecture) Run-Time Analysis Average-time analysis Time anal

Recursion: The Beginning

ECE 242 Fall 14 Exam II Profs. Gao and Zink

Recall from Last Time: Big-Oh Notation

CS/CE 2336 Computer Science II

15-110: Principles of Computing, Spring 2018

Module 05: Types of recursion

CSCI 121: Recursive Functions & Procedures

Test Bank Ver. 5.0: Data Abstraction and Problem Solving with C++: Walls and Mirrors, 5 th edition, Frank M. Carrano

C/C++ Programming Lecture 18 Name:

OVERVIEW. Recursion is an algorithmic technique where a function calls itself directly or indirectly. Why learn recursion?

ECE 2400 Computer Systems Programming Fall 2018 Topic 2: C Recursion

Recursion: The Beginning

CSC Design and Analysis of Algorithms

Algorithm. An algorithm is a computational process for solving a problem. computational: a computer must be able to perform the steps of the process

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

Recursive Functions. Biostatistics 615 Lecture 5

= otherwise W-4 W-1. Overview. CSE 142 Computer Programming I. Overview. Factorial Function

ANS:

Clicker Question What will this print? def blah(x): return x+1 def main(): z = blah(3) print(z) main() A) 3 B) 4 C) 5 D) It causes an error

Lecture. Algorithm Design and Recursion. Richard E Sarkis CSC 161: The Art of Programming

Complexity of Algorithms

Extended Introduction to Computer Science CS1001.py Lecture 7: Basic Algorithms: Sorting, Merge; Time Complexity and the O( ) notation

Algorithm Design and Recursion. Search and Sort Algorithms

A library of recursive functions

Search and Sorting Algorithms

[ 11.2, 11.3, 11.4] Analysis of Algorithms. Complexity of Algorithms. 400 lecture note # Overview

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

Algorithms. MIS, KUAS, 2007 Ding

CSC148-Section:L0301

Measuring Efficiency

Recursive Definitions Structural Induction Recursive Algorithms

6.001 Notes: Section 4.1

Divide-and-Conquer. Dr. Yingwu Zhu

Admin. How's the project coming? After these slides, read chapter 13 in your book. Quizzes will return

What is Recursion? ! Each problem is a smaller instance of itself. ! Implemented via functions. ! Very powerful solving technique.

Algorithmics. Some information. Programming details: Ruby desuka?

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

Sorting and Searching

CSCI-1200 Data Structures Fall 2017 Lecture 7 Order Notation & Basic Recursion

Computer Algorithms. Introduction to Algorithm

Recursion. Let s start by looking at some problems that are nicely solved using recursion. First, let s look at generating The Fibonacci series.

The Complexity of Algorithms (3A) Young Won Lim 4/3/18

Algorithm Analysis and Design

1 Recursion. 2 Recursive Algorithms. 2.1 Example: The Dictionary Search Problem. CSci 235 Software Design and Analysis II Introduction to Recursion

NCS 301 DATA STRUCTURE USING C

Reduction & Recursion Overview

UNIT 5C Merge Sort. Course Announcements

CITS 1401 Problem Solving & Programming

COMP-202 Unit 8: Recursion. CONTENTS: Recursion in Java More complex examples

TAIL RECURSION, SCOPE, AND PROJECT 4 11

Fun facts about recursion

Wednesday January 31, 2:30-4pm ENGMC 103

Binary Search APRIL 25 TH, 2014

There are three questions on this exam. You have 2 hours to complete it. Please indent your program so that it is easy for the grader to read.

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

Recursion. Chapter 5

CSC148-Section:L0301

Transcription:

Lecture 15 Chapter 11 Recursion 11.3 Functional Recursion 11.4 Binary Search

11.3 Functional Recursion Functional recursion is a method of defining functions in which the function being defined is applied within its own definition. Example 1: Fibonacci sequence: F(0)=1 (base case) F(1)=1 (base case) F(n)=F(n-1) + F(n-2) for all integers n>1 (recursive def.) Example 2: Factorial Recall that usually we have this definition: n! = 1*2*3*4*5*...*(n-2)*(n-1)*n

11.3 Functional Recursion Functional recursion is a method of defining functions in which the function being defined is applied within its own definition. Example 1: Fibonacci sequence: F(0)=1 (base case) F(1)=1 (base case) F(n)=F(n-1) + F(n-2) for all integers n>1 (recursive def) Example 2: Factorial: Recall that usually we have this definition: n! = 1*2*3*4*5*...*(n-2)*(n-1)*n Note (n-1)!

11.3 Functional Recursion Functional recursion is a method of defining functions in which the function being defined is applied within its own definition. Example 1: Fibonacci sequence: F(0)=1 (base case) F(1)=1 (base case) F(n)=F(n-1) + F(n-2) for all integers n>1 (recursive def.) Example 2: Factorial: Recall that usually we have this definition: n! = 1*2*3*4*5*...*(n-2)*(n-1)*n Therefore we can give a recursive definition: 1! = 1 n! = n*(n-1)!

11.3 Functional Recursion Let's see the program that finds factorial of a number, using recursive definition: 1! = 1 n! = n*(n-1)! Here is a draft of the program: def factorial(n): if n == 1: return 1 else: return n*factorial(n-1) def main(): n=input('please, input n:') main() F=factorial(n) print("%d! = %d"%(n,f))

11.3 Functional Recursion Let's trace the call of factorial(4): def factorial(n): if n == 1: return 1 else: return n*factorial(n-1) factorial(4) n=4 return 4* factorial(3)

11.3 Functional Recursion Let's trace the call of factorial(4): def factorial(n): if n == 1: return 1 else: return n*factorial(n-1) factorial(4) n=4 return factorial(3) 4* n=3 return 3* factorial(2)

11.3 Functional Recursion Let's trace the call of factorial(4): def factorial(n): if n == 1: return 1 else: return n*factorial(n-1) factorial(4) n=4 return factorial(3) 4* n=3 return 3* factorial(2) n=2 return 2* factorial(1)

11.3 Functional Recursion Let's trace the call of factorial(4): def factorial(n): if n == 1: return 1 else: return n*factorial(n-1) factorial(4) n=4 return factorial(3) 4* n=3 return 3* factorial(2) n=2 return 2* factorial(1) n=1 return 1

11.3 Functional Recursion Let's trace the call of factorial(4): def factorial(n): if n == 1: return 1 else: return n*factorial(n-1) factorial(4) n=4 return factorial(3) 4* n=3 return 3* factorial(2) n=2 return 2* factorial(1) n=1 1 return 1

11.3 Functional Recursion Let's trace the call of factorial(4): def factorial(n): if n == 1: return 1 else: return n*factorial(n-1) factorial(4) n=4 return factorial(3) 4* n=3 return 3* factorial(2) n=2 2 return 2* factorial(1) n=1 2*1=2 1 return 1

11.3 Functional Recursion Let's trace the call of factorial(4): def factorial(n): if n == 1: return 1 else: return n*factorial(n-1) factorial(4) n=4 return factorial(3) 4* n=3 6 return 3* factorial(2) n=2 3*2=6 2 return 2* factorial(1) n=1 2*1=2 1 return 1

11.3 Functional Recursion Let's trace the call of factorial(4): def factorial(n): if n == 1: return 1 else: return n*factorial(n-1) factorial(4) 24 n=4 return factorial(3) 4* n=3 4*6=24 6 return 3* factorial(2) n=2 3*2=6 2 return 2* factorial(1) n=1 2*1=2 1 return 1

11.3 Functional Recursion See the program factorial_rec.py

11.3 Functional Recursion Another example of recursive function: Ackermann function or Ackermann Péter function. n+ 1 if m=0 A(m,n) = A(m-1,1) if m>0 and n=0 A(m-1,A(m,n-1)) if m>0 and n>0 This function has only recursive definition, and it grows very fast. Let's see how it works: A(0,4) = 5 A(0,7) = 8 A(1,0) = A(0,1) = 2

11.3 Functional Recursion Another example of recursive function: Ackermann function or Ackermann Péter function. n+ 1 if m=0 A(m,n) = A(m-1,1) if m>0 and n=0 A(m-1,A(m,n-1)) if m>0 and n>0 This function has only recursive definition, and it grows very fast. Let's see how it works: A(0,4) = 5 A(0,7) = 8 A(1,0) = A(0,1) = 2 A(2,0) = A(1,1) = A(0, A(1,0))=A(0,2)=3

11.3 Functional Recursion Another example of recursive function: Ackermann function or Ackermann Péter function. n+ 1 if m=0 A(m,n) = A(m-1,1) if m>0 and n=0 A(m-1,A(m,n-1)) if m>0 and n>0 This function has only recursive definition, and it grows very fast. Let's see how it works: A(0,4) = 5 A(0,7) = 8 A(1,0) = A(0,1) = 2 A(2,0) = A(1,1) = A(0, A(1,0))=A(0,2)=3 A(1,2) = A(0, A(1,1))=A(0,3)=4

11.3 Functional Recursion Another example of recursive function: Ackermann function or Ackermann Péter function. n+ 1 if m=0 A(m,n) = A(m-1,1) if m>0 and n=0 A(m-1,A(m,n-1)) if m>0 and n>0 This function has only recursive definition, and it grows very fast. Let's see how it works: A(0,4) = 5 A(0,7) = 8 A(1,0) = A(0,1) = 2 A(2,0) = A(1,1) = A(0, A(1,0))=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,3)=A(0, A(1,2))=A(0,4)=5

11.3 Functional Recursion Another example of recursive function: Ackermann function or Ackermann Péter function. n+ 1 if m=0 A(m,n) = A(m-1,1) if m>0 and n=0 A(m-1,A(m,n-1)) if m>0 and n>0 This function has only recursive definition, and it grows very fast. Let's see how it works: A(0,4) = 5 A(0,7) = 8 A(1,0) = A(0,1) = 2 A(2,0) = A(1,1) = A(0, A(1,0))=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,3)=A(0, A(1,2))=A(0,4)=5 A(3,2) = A(2,A(3,1))=A(2,A(2,A(3,0)))=A(2,A(2,A(2,1)))= A(2,A(2,5))=A(2,A(1,A(2,4)))=A(2,A(1,A(1,A(2,3))))= A(2,A(1,A(1,A(1,A(2,2))))=A(2,A(1,A(1,A(1,A(1,A(2,1))))))= A(2,A(1,A(1,A(1,A(1,5)))))=A(2,A(1,A(1,A(1,A(0,A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0,A(0,A(0,A(1,8)...)= A(2,A(0,A(0,A(0,A(0,A(1,7)...)=A(2,A(0,A(0,A(0,A(0,A(0,A(1,6)...)= A(2,A(0,A(0,A(0,A(0,A(0,A(0,A(1,5)...)= A(2,A(0,A(0,A(0,A(0,A(0,A(0,A(0,A(1,4)...)= A(2,A(0,A(0,A(0,A(0,A(0,A(0,A(0,A(0,A(1,3)...)=

A(2,A(0,A(0,A(0,A(0,A(0,A(0,A(0,A(0,5)...)= A(2,A(0,A(0,A(0,A(0,A(0,A(0,A(0,6)...)= A(2,A(0,A(0,A(0,A(0,A(0,A(0,7)...)=A(2,A(0,A(0,A(0,A(0,A(0,8)...)= A(2,A(0,A(0,A(0,A(0,9)...)=A(2,A(0,A(0,A(0,10)...)=A(2,A(0,A(0,11)...)= A(2,A(0,12)...)=A(2,13)...)=A(1,A(2,12))=A(1,A(1,A(2,11)))= A(1,A(1,A(1,A(2,10))))=A(1,A(1,A(1,A(1,A(2,9)))))= A(1,A(1,A(1,A(1,A(1,A(2,8)...)=A(1,A(1,A(1,A(1,A(1,A(1,A(2,7)...)= A(1,A(1,A(1,A(1,A(1,A(1,A(1,A(2,6)...)= = 29

11.4 Binary Search A binary search algorithm locates the position of an element (a number, a letter, a word,...) in a sorted list. It inspects the middle element of the sorted list: if equal to the sought value, then the position has been found; otherwise, the lower(left) half or upper(right) half is chosen for further searching based on: whether the sought value is less than or greater than the middle element. This method reduces the number of elements needed to be checked by a factor of two each time, and finds the sought value if it exists in the list or if not determines "not present". Complexity: logarithmic i.e. if we start with n elements, the algorithm will terminate in at most k log 2 n steps, where k is a constant.

11.4 Binary Search Our book has a nice example with lexicon (please, take a look at it). We will deal with numbers in class. Input: a sorted list of numbers (integers of floating point numbers), a number to find (may be not present in the list) Output: location of the element in the list (if present), 'Not in the list' (if not present)

11.4 Binary Search Example: binary search algorithm on the following input: [1,7,9,12,17,19,23,45,67,123,167] find: 7

11.4 Binary Search Example: binary search algorithm on the following input: [1,7,9,12,17,19,23,45,67,123,167] find: 7 start stop Number of elements if the array: 11, start index = 0, stop index = 10

11.4 Binary Search Example: binary search algorithm on the following input: [1,7,9,12,17,19,23,45,67,123,167] find: 7 start stop Number of elements if the array: 11, start index = 0, stop index = 10 Middle element: (start+stop) 2 = (0+10) 2 =5 or (0+10) // 2 =5 th

11.4 Binary Search Example: binary search algorithm on the following input: [1,7,9,12,17,19,23,45,67,123,167] find: 7 start stop Number of elements if the array: 11, start index = 0, stop index = 10 Middle element: (start+stop) 2 = (0+10) 2 =5 or (0+10) // 2 =5 th 5 th element 19 = 7 the number we are looking for? No

11.4 Binary Search Example: binary search algorithm on the following input: [1,7,9,12,17,19,23,45,67,123,167] find: 7 start stop Number of elements if the array: 11, start index = 0, stop index = 10 Middle element: (start+stop) 2 = (0+10) 2 =5 or (0+10) // 2 =5 th 5 th element 19 = 7 the number we are looking for? No 19 7? True

11.4 Binary Search Example: binary search algorithm on the following input: [1,7,9,12,17,19,23,45,67,123,167] find: 7 start stop Number of elements if the array: 11, start index = 0, stop index = 10 Middle element: (start+stop) 2 = (0+10) 2 =5 or (0+10) // 2 =5 th 5 th element 19 = 7 the number we are looking for? No 19 7? True therefore take the left part (with 19)

11.4 Binary Search [1,7,9,12,17,19] find: 7 start stop Number of elements in the new array (sub-array): 6, start = 0, stop = 5

11.4 Binary Search [1,7,9,12,17,19] find: 7 start stop Number of elements in the new array (sub-array): 6, start = 0, stop = 5 Middle element: (0+5) 2 =2 or (0+5)//2 = 2 nd 2 nd element 9 = 7 the number we are looking for? No

11.4 Binary Search [1,7,9,12,17,19] find: 7 start stop Number of elements in the new array (sub-array): 6, start = 0, stop = 5 Middle element: or (0+5)//2 = 2 nd 2 nd element 9 = 7 the number we are looking for? No 9 7? True (0+5) 2 =2

11.4 Binary Search [1,7,9,12,17,19] find: 7 start stop Number of elements in the new array (sub-array): 6, start = 0, stop = 5 Middle element: (0+5) 2 =2 or (0+5)//2 = 2 nd 2 nd element 9 = 7 the number we are looking for? No 9 7? True therefore take the left part (with 9)

11.4 Binary Search [1,7,9] find: 7 start stop Number of elements in the sub-array: 3, start = 0, stop = 2

11.4 Binary Search [1,7,9] find: 7 start stop Number of elements in the sub-array: 3, start = 0, stop = 2 Middle element: (0+2) =1 or (0+2) // 2 = 1 st 2

11.4 Binary Search [1,7,9] find: 7 start stop Number of elements in the sub-array: 3, start = 0, stop = 2 Middle element: (0+2) =1 or (0+2) // 2 = 1 st 2 1 st element 7 = 7 the number we are looking for? Yes Stop, return index 1.

11.4 Binary Search See the program binary_search.py

Homework Assignment Recall Fibonacci numbers/sequence : F(0)=1 (base case) F(1)=1 (base case) F(n)=F(n-1) + F(n-2) for all integers n>1 (recursive definition) Write a program, that for a given non-negative integer n, displays n first Fibonacci numbers. Print all the Fibonacci function calls. Here is a sketch of the program (Fibonacci calls are not printed): def Fibonacci(n): if n == 0:... elif n == 1:... else:... def main(): n = eval(input("please, input any non-negative integer:")) for i in range(n): print(fibonacci(i)) next slide

Homework Assignment Similar to example with Factorial Function (recursive definition) (slides 6 13 ) draw a figure of Fibonacci Function call on n=5 Suggestion: it is probably worth doing it as a tree: F(5) return F(4) + F(3) F(4) F(3) return... return...