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

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

General Computer Science for Engineers CISC 106 Midterm 2 Review James Atlas Computer and Information Sciences 11/06/2009.
8 Algorithms Foundations of Computer Science ã Cengage Learning.
HST 952 Computing for Biomedical Scientists Lecture 9.
Factorial Recursion stack Binary Search Towers of Hanoi
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. Binary search example postponed to end of lecture.
Recursion Chapter 11 Chapter 11.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 16: Recursion.
General Computer Science for Engineers CISC 106 James Atlas Computer and Information Sciences 10/23/2009.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Program Design and Development
General Computer Science for Engineers CISC 106 Lecture 10 Roger Craig Computer and Information Sciences 3/06/2009.
General Computer Science for Engineers CISC 106 Lecture 08
Recursion.
General Computer Science for Engineers CISC 106 Lecture 07 James Atlas Computer and Information Sciences 9/18/2009.
Algorithm Efficiency and Sorting
CS107 Introduction to Computer Science Lecture 7, 8 An Introduction to Algorithms: Efficiency of algorithms.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
General Computer Science for Engineers CISC 106 Lecture 07 James Atlas Computer and Information Sciences 06/29/2009.
Recursion, Complexity, and Sorting By Andrew Zeng.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors Data Abstraction & Problem Solving.
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.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
CSIS 123A Lecture 9 Recursion Glenn Stevenson CSIS 113A MSJC.
CSC 211 Data Structures Lecture 13
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Recursive Solutions Recursion is an extremely powerful problem-solving.
Functions-Recall 1. 2 Parameter passing & return void main() { int x=10, y=5; printf (“M1: x = %d, y = %d\n”, x, y); interchange (x, y); printf (“M2:
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
ALGORITHMS.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
General Computer Science for Engineers CISC 106 Lecture 2^4 Roger Craig Computer and Information Sciences 03/23/2009.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Loop Invariants and Binary Search Chapter 4.4, 5.1.
General Computer Science for Engineers CISC 106 Lecture 14 James Atlas Computer and Information Sciences 08/10/2009.
Sorting Algorithms. Algorithms, revisited What is an algorithm? Wikipedia Definition: an algorithm is a definite list of well-defined instructions for.
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.
General Computer Science for Engineers CISC 106 Lecture 09 Dr. John Cavazos Computer and Information Sciences 03/04/2009.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion (Continued) Prof. Katherine Gibson Prof. Jeremy Dixon Based on slides from UPenn’s.
Recursion Powerful Tool
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
CMSC201 Computer Science I for Majors Lecture 20 – Recursion (Continued) Prof. Katherine Gibson Based on slides from UPenn’s CIS 110, and from previous.
COP 3503 FALL 2012 Shayan Javed Lecture 15
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
Recursion- Fibonacci mpack.
Introduction to the Design and Analysis of Algorithms
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Algorithm design and Analysis
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Introduction to Algorithms and Programming
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Last Class We Covered Recursion Stacks Parts of a recursive function:
Recursive Algorithms 1 Building a Ruler: drawRuler()
Chapter 13 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

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 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!!

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

Example How many CEO’s at this table? A recursive approach…… Taken from How many CEOs to my left?

Example How many CEO’s at this table? A recursive approach…… Taken from I don’t know how many to your left?

Example How many CEO’s at this table? A recursive approach…… Taken from I don’t know either how many to your left?

Example How many CEO’s at this table? A recursive approach…… Taken from None…. Just angry senators….

Example How many CEO’s at this table? A recursive approach…… Taken from It’s just Bob next to me

Example How many CEO’s at this table? A recursive approach…… Taken from It’s just Bob and Jim next to me

Example How many CEO’s at this table? A recursive approach…… Taken from Why am I stuck with Bob, Jim and Frank?

Example How many CEO’s at this table? A recursive approach…… Taken from Just the 4 of us…

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 = [ ]; arr1(3) is 3.3 arr1([1 4]) is the array [ ] arr1(1 : 2 : 5) is the array [ ]

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

Iterative strategy Given [ ] 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.