General Computer Science for Engineers CISC 106 Lecture 06 James Atlas Computer and Information Sciences 06/24/2009 Lecture Overview Recursion ◦ ◦ ◦ ◦ - Definition Characteristics Details Examples Recursive Function Definition Definition ◦ The process where a function can call itself. Characteristics ◦ Allows breaking big problems into smaller and smaller problems. ◦ One must ensure recursion stops When you call a function… Matlab creates a ‘record’ to store the variables from the new function ◦ Record is pushed on the “stack” When function done, result passed back to calling function ◦ Record is popped off stack. When you call a function… function bar2 executing function bar2 function foo2 calls bar2 function foo2 function bar1 calls foo2 function bar1 function foo1 calls bar1 function foo1 function main calls foo1 function main In recursion these would all same function!! Why Can a Function Call Itself? Calling a function creates a an ‘instance’ of the function! When a function calls itself it creates a new ‘instance’ of itself Each instance pushed on stack ◦ At top of stack is instance of function currently executing Recursion requires 3 things Terminating condition to stop! ◦ Force creation of new instances of a function Function must call itself ◦ With different inputs! Terminating condition must eventually be true Example How many CEO’s at this table? A recursive approach…… Taken from http://cache.daylife.com/imageserve/0dgm32v6aYgv6/610x.jpg Example How many CEO’s at this table? A recursive approach…… How many CEOs to my left? Taken from http://cache.daylife.com/imageserve/0dgm32v6aYgv6/610x.jpg Example How many CEO’s at this table? A recursive approach…… I don’t know how many to your left? Taken from http://cache.daylife.com/imageserve/0dgm32v6aYgv6/610x.jpg Example How many CEO’s at this table? A recursive approach…… I don’t know either how many to your left? Taken from http://cache.daylife.com/imageserve/0dgm32v6aYgv6/610x.jpg Example How many CEO’s at this table? A recursive approach…… None…. Just angry senators…. Taken from http://cache.daylife.com/imageserve/0dgm32v6aYgv6/610x.jpg Example How many CEO’s at this table? A recursive approach…… It’s just Bob next to me Taken from http://cache.daylife.com/imageserve/0dgm32v6aYgv6/610x.jpg Example How many CEO’s at this table? A recursive approach…… It’s just Bob and Jim next to me Taken from http://cache.daylife.com/imageserve/0dgm32v6aYgv6/610x.jpg Example How many CEO’s at this table? A recursive approach…… Why am I stuck with Bob, Jim and Frank? Taken from http://cache.daylife.com/imageserve/0dgm32v6aYgv6/610x.jpg Example How many CEO’s at this table? A recursive approach…… Just the 4 of us… Taken from http://cache.daylife.com/imageserve/0dgm32v6aYgv6/610x.jpg Sequence, selection, repetition • • • • Sequence (statements are ordered, 1, 2, 3…etc.) Selection (logical control, IF statement) Repetition (iteration and recursion) With these three control structures, you can program anything Subarrays It is possible to select and use subsets of MATLAB arrays. arr1 = [1.1 -2.2 3.3 -4.4 5.5]; arr1(3) is 3.3 arr1([1 4]) is the array [1.1 -4.4] arr1(1 : 2 : 5) is the array [1.1 3.3 5.5] Linear search Searching, extremely important in computer science If we have a list of unsorted numbers, how could we search them? Iterative strategy Given [5 4 2 10 6 7] find which position 6 occupies Alternatively, does the array contain the number 6? Recursive strategy Break Binary search Now, what if the list is sorted, can we search it faster? Group exercise: Speed up our search process if we know the list is already sorted (smallest to greatest) Binary Search Find N in list Pick a number X halfway between the start and end of the list If X == N we are done else if X < N search top half of list else search bottom half of list Let’s do this in Matlab Recursive solution Binary Search Flowchart diagram of the algorithm Note the two stopping conditions (Exits) Note the one loop (Note: flowchart is equivalent to pseudocode) How much faster is this than linear search? If linear search takes roughly n steps for n things, Then binary search takes roughly log2(n) steps for n things. This is because we divide the n things by 2 each time.