Presentation is loading. Please wait.

Presentation is loading. Please wait.

General Computer Science for Engineers CISC 106 Lecture 06 James Atlas Computer and Information Sciences 06/24/2009.

Similar presentations


Presentation on theme: "General Computer Science for Engineers CISC 106 Lecture 06 James Atlas Computer and Information Sciences 06/24/2009."— Presentation transcript:

1 General Computer Science for Engineers CISC 106 Lecture 06 James Atlas Computer and Information Sciences 06/24/2009

2 Lecture Overview Recursion ◦ -Definition ◦ -Characteristics ◦ -Details ◦ -Examples

3 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

4 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.

5 When you call a function… function foo1 function bar1 function foo2 function bar2 function main function main calls foo1 function foo1 calls bar1 function bar1 calls foo2 function foo2 calls bar2 function bar2 executing In recursion these would all same function!!

6 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

7 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

8 Example How many CEO’s at this table? A recursive approach…… Taken from http://cache.daylife.com/imageserve/0dgm32v6aYgv6/610x.jpg

9 Example How many CEO’s at this table? A recursive approach…… Taken from http://cache.daylife.com/imageserve/0dgm32v6aYgv6/610x.jpg How many CEOs to my left?

10 Example How many CEO’s at this table? A recursive approach…… Taken from http://cache.daylife.com/imageserve/0dgm32v6aYgv6/610x.jpg I don’t know how many to your left?

11 Example How many CEO’s at this table? A recursive approach…… Taken from http://cache.daylife.com/imageserve/0dgm32v6aYgv6/610x.jpg I don’t know either how many to your left?

12 Example How many CEO’s at this table? A recursive approach…… Taken from http://cache.daylife.com/imageserve/0dgm32v6aYgv6/610x.jpg None…. Just angry senators….

13 Example How many CEO’s at this table? A recursive approach…… Taken from http://cache.daylife.com/imageserve/0dgm32v6aYgv6/610x.jpg It’s just Bob next to me

14 Example How many CEO’s at this table? A recursive approach…… Taken from http://cache.daylife.com/imageserve/0dgm32v6aYgv6/610x.jpg It’s just Bob and Jim next to me

15 Example How many CEO’s at this table? A recursive approach…… Taken from http://cache.daylife.com/imageserve/0dgm32v6aYgv6/610x.jpg Why am I stuck with Bob, Jim and Frank?

16 Example How many CEO’s at this table? A recursive approach…… Taken from http://cache.daylife.com/imageserve/0dgm32v6aYgv6/610x.jpg Just the 4 of us…

17 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

18 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]

19 Linear search Searching, extremely important in computer science If we have a list of unsorted numbers, how could we search them?

20 Iterative strategy Given [5 4 2 10 6 7] find which position 6 occupies Alternatively, does the array contain the number 6?

21 Recursive strategy

22 Break

23 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)

24

25 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

26 Let’s do this in Matlab Recursive solution

27 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.


Download ppt "General Computer Science for Engineers CISC 106 Lecture 06 James Atlas Computer and Information Sciences 06/24/2009."

Similar presentations


Ads by Google