1 Data Structures CSCI 132, Spring 2014 Lecture 15 Recursion.

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

Lecture Computer Science I - Martin Hardwick Recursion rA recursive function must have at least two parts l A part that solves a simple case of the.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 13 Recursion. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Recursive void Functions Tracing recursive.
Chapter 14 Recursion Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,, E. Reingold.
Algorithm Design approaches Dr. Jey Veerasamy. Petrol cost minimization problem You need to go from S to T by car, spending the minimum for petrol. 2.
Inheritance (2).
Lecture 12 Recursion part 1
void count_down (int count) { for(i=count; i>1; i--) printf(" %d\t", count); } printf("A%d\n", count); if(count>1) count_down(count-1); printf("B%d\n",
Back to Sorting – More efficient sorting algorithms.
1 Data Structures - CSCI 102 CS102 C++ Operator Overloading Prof Tejada.
Strassen's Matrix Multiplication Sibel KIRMIZIGÜL.
CS 206 Introduction to Computer Science II 03 / 02 / 2009 Instructor: Michael Eckmann.
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
Lecture 30 CSE 331 Nov 13, To be strictly enforced For the rest of the semester on Fridays SUBMIT your HOMEWORKS by 1:10 PM.
16/23/2015 9:48 AM6/23/2015 9:48 AM6/23/2015 9:48 AMRecursion Recursion Recursion is when a function calls itself to implement an algorithm. Really a paradigm.
Csci1300 Introduction to Programming Recursion Dan Feng.
Lecture 34 CSE 331 Nov 30, Graded HW 8 On Wednesday.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Unit 1. Sorting and Divide and Conquer. Lecture 1 Introduction to Algorithm and Sorting.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
Lecture 5 Dynamic Programming. Dynamic Programming Self-reducibility.
1 Survey of Computer Science CSCI 110, Spring 2011 Lecture 14 Recursion.
CSCE 3110 Data Structures & Algorithm Analysis Sorting (I) Reading: Chap.7, Weiss.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Lecture-13 Instructor Name: Muhammad Safyan Programming Fundamental.
CSIS 123A Lecture 9 Recursion Glenn Stevenson CSIS 113A MSJC.
CSS106 Introduction to Elementary Algorithms M.Sc Askar Satabaldiyev Lecture 05: MergeSort & QuickSort.
1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City.
Dynamic Programming. Many problem can be solved by D&C – (in fact, D&C is a very powerful approach if you generalize it since MOST problems can be solved.
Lecture 28 CSE 331 Nov 9, Mini project report due WED.
1 Lecture 3 Part 2 Storage Classes, Scope, and Recursion.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
1 Algorithms CSCI 235, Fall 2015 Lecture 6 Recurrences.
LECTURE 20: RECURSION CSC 212 – Data Structures. Humorous Asides.
1 Data Structures CSCI 132, Spring 2014 Lecture 18 Recursion and Look-Ahead.
A Different Solution  alternatively we can use the following algorithm: 1. if n == 0 done, otherwise I. print the string once II. print the string (n.
1 Algorithms CSCI 235, Fall 2015 Lecture 7 Recurrences II.
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
Sorting Quick, Merge & Radix Divide-and-conquer Technique subproblem 2 of size n/2 subproblem 1 of size n/2 a solution to subproblem 1 a solution to.
Quicksort Dr. Yingwu Zhu. 2 Quicksort A more efficient exchange sorting scheme than bubble sort – A typical exchange involves elements that are far apart.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
1 CS1120: Recursion. 2 What is Recursion? A method is said to be recursive if the method definition contains a call to itself A recursive method is a.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Recursion Powerful Tool
CSC 205 Programming II Lecture 8 Recursion.
Lecture 5 Dynamic Programming
Fundamentals of structural programming
Lecture 4 Divide-and-Conquer
Divide and Conquer.
CS 3343: Analysis of Algorithms
Lecture 5 Dynamic Programming
CS 3343: Analysis of Algorithms
Selection Sort Find the smallest value in the array. Put it in location zero. Find the second smallest value in the array and put it in location 1. Find.
CS1120: Recursion.
Recursion.
Merge Sort.
CS148 Introduction to Programming II
Divide & Conquer Algorithms
Algorithms CSCI 235, Spring 2019 Lecture 7 Recurrences II
Algorithms An algorithm is a set of instructions used to solve a specific problem In order to be useful, an algorithm must have the following properties:
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Algorithms CSCI 235, Spring 2019 Lecture 6 Recurrences
CSCE 3110 Data Structures & Algorithm Analysis
Algorithms CSCI 235, Spring 2019 Lecture 8 Recurrences III
Programming Fundamental
Presentation transcript:

1 Data Structures CSCI 132, Spring 2014 Lecture 15 Recursion

2 Recall Divide and Conquer Play DicePoker Roll DiceCompute Rank Find Winner

3 Recursion Skate 4 laps Skate 1 lapSkate 3 laps Skate 1 lapSkate 2 laps When the subproblems are smaller versions of the original problem, we can use recursion to solve the problem.

4 Example: Skate n laps How to skate n laps using recursion: 1) Skate 1 lap 2) Skate (n - 1) laps The first step is trivial, and we can do it in one step The second step is a smaller version of the original problem. A function that can skate n laps can also skate n-1 laps.

5 Recursive Skating void skateLaps( int n) { if ( n < = 0 ) { //do nothing } else { cout << "Skating 1 lap." << endl; skateLaps( n - 1); } } Base Case General Case

6 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making the call. In other words, recursion occurs when a function calls itself! But we need to avoid making an infinite sequence of function calls (infinite recursion).

7 Finding a Recursive Solution A recursive solution to a problem must be written carefully. The idea is for each successive recursive call to bring you one step closer to a situation in which the problem can easily be solved. This easily solved situation is called the base case. Each recursive algorithm must have at least one base case, as well as the general case.

8 General Format for Recursion if (some easily-solved condition) { // base case solution statement(s) } else { // general case (Possibly other statements here, too). recursive function call (Possibly other statements here, too). }

9 Example: Factorial Definition of Factorial n! = 1 * 2 * 3 * 4 *... * (n-1) * n; 0! = 1 In other words: fact(n) = 1, n = 0 n * fact( n-1), n > 0

10 Writing Factorial in C++ int Fact (int n) { }

11 Writing Factorial in C++ int Fact (int n) { if ( n <= 0) { return 1; } else { return n*Fact(n - 1); }

12 Execution Model for Factorial Fact(2) n if( n <= 0) return 1; } else { return n * Fact(n - 1); } n if( n <= 0) return 1; } else { return n * Fact(n - 1); } n if( n <= 0) return 1; } else { return n * Fact(n - 1); } 210

13 Invocation Tree for Factorial Fact( 3) : 6 Fact( 2) : 2 Fact( 1) : 1 Fact( 0) : 1 Trace of Factorial (we will do in class) nFact(n - 1)Fact( n )

14 Invocation Tree for Factorial Fact( 3) : 6 Fact( 2) : 2 Fact( 1) : 1 Fact( 0) : 1 Trace of Factorial (we will do in class) nFact(n - 1)Fact( n ) 3* 2= 6 2* 1= 2 1 * 1=

15 Rabbit Populations: Fibonacci

16 Math Teachers Shouldn't knit From "Frazz" by Jeff Mallett

17 Fibonacci in C++ int Fib( int n ) { }

18 Fibonacci in C++ int Fib( int n ) { if (n <= 1) { return n; } else { return Fib(n-1) + Fib(n-2); }

19 Fibonacci Invocation Tree

20 How long will this take? For large numbers, each time you increment by 1, it takes about twice as long to compute the Fibonacci result: Fib(32) Fib(31)Fib(30) Suppose our computer can process 10 9 operations per second. How long will it take to compute Fib(100)? (Approximate answer will be given in class).