Lecture 10 Recursion CSE225: Data Structures. 2 A Look Back at Functions #include double distance(double x1, double y1, double x2, double y2) { double.

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

CS102 Algorithms and Programming II1 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive.
Recursion. Binary search example postponed to end of lecture.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursion. Recursive Solutions Recursion breaks a problem into smaller identical problems – mirror images so to speak. By continuing to do this, eventually.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
CENG 7071 Recursion. CENG 7072 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive function.
Recursion In general there are two approaches to writing repetitive algorithms. One uses loops(while, do while and for): the other uses recursion. Recursion.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Programming Principles II Lecture Notes 5 Recursion Andreas Savva.
M180: Data Structures & Algorithms in Java
Functions and an Introduction to Recursion.  Recursive function ◦ A function that calls itself, either directly, or indirectly (through another function)
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
CIS 068 Welcome to CIS 068 ! Stacks and 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.
Lecture 12 Recursion part 1 Richard Gesick. Recursion A recursive method is a method that calls itself. A recursive method is capable of solving only.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Advanced Computer Architecture and Parallel Processing Rabie A. Ramadan http:
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
W1-1 University of Washington Computer Programming I Recursion © 2000 UW CSE.
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Sections 4.1 & 4.2 Recursive Definitions,
Recursion CENG 707.
Chapter Topics Chapter 16 discusses the following main topics:
Recursion CENG 707.
Recursion The programs discussed so far have been structured as functions that invoke one another in a disciplined manner For some problems it is useful.
Recursion DRILL: Please take out your notes on Recursion
RECURSION.
Java 4/4/2017 Recursion.
To understand recursion, you have to understand recursion!
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Important Notes Remaining Schedule: recall the 2 bad weather days (Jan 8th, Jan 17th) we will need to make up. In providing the 2 weeks needed for the.
Lecture 17 Recursion part 1 Richard Gesick.
Recursion Data Structures.
Functions Recursion CSCI 230
Module 1-10: Recursion.
Lecture 12 Recursion part 1 CSE /26/2018.
CSC 143 Recursion.
Dr. Sampath Jayarathna Cal Poly Pomona
ICS103 Programming in C Lecture 11: Recursive Functions
Recursion.
Recursive Function Prepared by Harprith ICT2102 Introduction to Data Structure.
Recursion: The Mirrors
Presentation transcript:

Lecture 10 Recursion CSE225: Data Structures

2 A Look Back at Functions #include double distance(double x1, double y1, double x2, double y2) { double dist; dist = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); return dist; } int main() { double milage = distance(0, 0, 3, 4); printf("Milage: %lf\n",milage); return 0; }

3 A Look Back at Functions #include double distance(double x1, double y1, double x2, double y2) { double dist; dist = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); return dist; } int main() { double milage = distance(0, 0, 3, 4); printf("Milage: %lf\n",milage); return 0; } main

4 A Look Back at Functions #include double distance(double x1, double y1, double x2, double y2) { double dist; dist = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); return dist; } int main() { double milage = distance(0, 0, 3, 4); printf("Milage: %lf\n",milage); return 0; } maindistance (0,0,3,4)

5 A Look Back at Functions #include double distance(double x1, double y1, double x2, double y2) { double dist; dist = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); return dist; } int main() { double milage = distance(0, 0, 3, 4); printf("Milage: %lf\n",milage); return 0; } maindistancesqrt (0,0,3,4) (25)

6 A Look Back at Functions #include double distance(double x1, double y1, double x2, double y2) { double dist; dist = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); return dist; } int main() { double milage = distance(0, 0, 3, 4); printf("Milage: %lf\n",milage); return 0; } maindistancesqrt (0,0,3,4) (25) (5)

7 A Look Back at Functions #include double distance(double x1, double y1, double x2, double y2) { double dist; dist = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); return dist; } int main() { double milage = distance(0, 0, 3, 4); printf("Milage: %lf\n",milage); return 0; } maindistance (0,0,3,4) (5)

Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. Recursion can be implemented using a recursive function (a function invoking itself, either directly or indirectly). Recursion can be used as an alternative to iteration. It is an important and powerful tool in problem solving and programming. It is a programming technique that naturally implements the divide- and-conquer problem solving methodology.

n! = n * (n-1) * (n-2) * … * 2 * 1for any integer n>0 0! = 1 Iterative Definition in C: fval = 1; for (i = n; i >= 1; i--) fval = fval * i; Factorial – Iterative Definition

To define n! recursively, n! must be defined in terms of the factorial of a smaller number. Observation (problem size is reduced): n! = n * (n-1)! Base case:0! = 1 We can reach the base case, by subtracting 1 from n if n is a positive integer. Recursive Definition: n! = 1 if n = 0 n! = n*(n-1)! if n > 0 Factorial – Recursive Definition

1.One or more simple cases of the problem (called the stopping cases or base case) have a simple non-recursive solution. 2.The other cases (general cases) of the problem can be reduced (using recursion) to problems that are closer to stopping cases. 3.Eventually the problem can be reduced to base cases only, which are relatively easy to solve. In general: if (base case) solve it else reduce the problem using recursion // general case The Nature of Recursion

Factorial of 4 (Recursive) Calculate 4! 4! = 4 * (4-1)! 4! = 4 * 3! 4! = 4 * __

Factorial of 4 (Recursive) Calculate 4! 4! = 4 * (4-1)! 4! = 4 * 3! 4! = 4 * __ Calculate 3! 3! = 3 * (3-1)! 3! = 3 * 2! 3! = 3 * __

Factorial of 4 (Recursive) Calculate 4! 4! = 4 * (4-1)! 4! = 4 * 3! 4! = 4 * __ Calculate 3! 3! = 3 * (3-1)! 3! = 3 * 2! 3! = 3 * __ Calculate 2! 2! = 2 * (2-1)! 2! = 2 * 1! 2! = 2 * __

Factorial of 4 (Recursive) Calculate 4! 4! = 4 * (4-1)! 4! = 4 * 3! 4! = 4 * __ Calculate 3! 3! = 3 * (3-1)! 3! = 3 * 2! 3! = 3 * __ Calculate 2! 2! = 2 * (2-1)! 2! = 2 * 1! 2! = 2 * __ Calculate 1! 1! = 1 * (1-1)! 1! = 1 * 0! 1! = 1 * __

Factorial of 4 (Recursive) Calculate 4! 4! = 4 * (4-1)! 4! = 4 * 3! 4! = 4 * __ Calculate 3! 3! = 3 * (3-1)! 3! = 3 * 2! 3! = 3 * __ Calculate 2! 2! = 2 * (2-1)! 2! = 2 * 1! 2! = 2 * __ Calculate 1! 1! = 1 * (1-1)! 1! = 1 * 0! 1! = 1 * __ Calculate 0! 0! = 1

Factorial of 4 (Recursive) Calculate 4! 4! = 4 * (4-1)! 4! = 4 * 3! 4! = 4 * __ Calculate 3! 3! = 3 * (3-1)! 3! = 3 * 2! 3! = 3 * __ Calculate 2! 2! = 2 * (2-1)! 2! = 2 * 1! 2! = 2 * __ Calculate 1! 1! = 1 * (1-1)! 1! = 1 * 0! 1! = 1 * 1 1! = 1

Factorial of 4 (Recursive) Calculate 4! 4! = 4 * (4-1)! 4! = 4 * 3! 4! = 4 * __ Calculate 3! 3! = 3 * (3-1)! 3! = 3 * 2! 3! = 3 * __ Calculate 2! 2! = 2 * (2-1)! 2! = 2 * 1! 2! = 2 * 1 2! = 2

Factorial of 4 (Recursive) Calculate 4! 4! = 4 * (4-1)! 4! = 4 * 3! 4! = 4 * __ Calculate 3! 3! = 3 * (3-1)! 3! = 3 * 2! 3! = 3 * 2 3! = 6

Factorial of 4 (Recursive) Calculate 4! 4! = 4 * (4-1)! 4! = 4 * 3! 4! = 4 * 6 4! = 24

Recursive Factorial Function // Computes the factorial of a nonnegative integer. // Precondition: n must be greater than or equal to 0. // Postcondition: Returns the factorial of n; n is unchanged. int Factorial(int n) { if (n ==0) return (1); else return (n * Factorial(n-1)); }

Tracing Factorial(4) Factorial(4)

Tracing Factorial(4) Factorial (4) call Factorial(4)

Tracing Factorial(4) Factorial (4) Factorial (3) call Factorial(4)= 4 * Factorial(3)

Tracing Factorial(4) Factorial (4) Factorial (3) Factorial (2) call Factorial(4)= 4 * Factorial(3) = 4 * (3 * Factorial(2))

Tracing Factorial(4) Factorial (4) Factorial (3) Factorial (2) Factorial (1) call Factorial(4)= 4 * Factorial(3) = 4 * (3 * Factorial(2)) = 4 * (3 * (2 * Factorial(1)))

Tracing Factorial(4) Factorial (4) Factorial (3) Factorial (2) Factorial (1) Factorial (0) call Factorial(4)= 4 * Factorial(3) = 4 * (3 * Factorial(2)) = 4 * (3 * (2 * Factorial(1))) = 4 * (3 * (2 * (1 * Factorial(0))))

Tracing Factorial(4) Factorial (4) Factorial (3) Factorial (2) Factorial (1) Factorial (0) return1 call Factorial(4)= 4 * Factorial(3) = 4 * (3 * Factorial(2)) = 4 * (3 * (2 * Factorial(1))) = 4 * (3 * (2 * (1 * Factorial(0)))) = 4 * (3 * (2 * (1 * 1)))

Tracing Factorial(4) Factorial (4) Factorial (3) Factorial (2) Factorial (1) Factorial (0) return1 call return1*1=1 call Factorial(4)= 4 * Factorial(3) = 4 * (3 * Factorial(2)) = 4 * (3 * (2 * Factorial(1))) = 4 * (3 * (2 * (1 * Factorial(0)))) = 4 * (3 * (2 * (1 * 1))) = 4 * (3 * (2 * 1))

Tracing Factorial(4) Factorial (4) Factorial (3) Factorial (2) Factorial (1) Factorial (0) return1 call return1*1=1 2*1=2 call Factorial(4)= 4 * Factorial(3) = 4 * (3 * Factorial(2)) = 4 * (3 * (2 * Factorial(1))) = 4 * (3 * (2 * (1 * Factorial(0)))) = 4 * (3 * (2 * (1 * 1))) = 4 * (3 * (2 * 1)) = 4 * (3 * 2)

Tracing Factorial(4) Factorial(4)= 4 * Factorial(3) = 4 * (3 * Factorial(2)) = 4 * (3 * (2 * Factorial(1))) = 4 * (3 * (2 * (1 * Factorial(0)))) = 4 * (3 * (2 * (1 * 1))) = 4 * (3 * (2 * 1)) = 4 * (3 * 2) = 4 * 6 Factorial (4) Factorial (3) Factorial (2) Factorial (1) Factorial (0) return1 call return1*1=1 2*1=2 3*2=6 call

Tracing Factorial(4) Factorial(4)= 4 * Factorial(3) = 4 * (3 * Factorial(2)) = 4 * (3 * (2 * Factorial(1))) = 4 * (3 * (2 * (1 * Factorial(0)))) = 4 * (3 * (2 * (1 * 1))) = 4 * (3 * (2 * 1)) = 4 * (3 * 2) = 4 * 6 = 24 Factorial (4) Factorial (3) Factorial (2) Factorial (1) Factorial (0) return1 call return1*1=1 2*1=2 3*2=6 4*6=24 final answer call

A stack is used to keep track of function calls. Whenever a new function is called, all its parameters and local variables are pushed onto the stack along with the memory address of the calling statement (this gives the computer the return point after execution of the function) A Couple of Things You Should Know

If the recursion never reaches the base case, the recursive calls will continue until the computer runs out of memory and the program crashes. Experienced programmers try to examine the remains of a crash. The message “stack overflow error” or “heap storage exhaustion” indicates a possible runaway recursion. When programming recursively, you need to make sure that the algorithm is moving toward the base case. Each successive call of the algorithm must be solving a simpler version of the problem. Any recursive algorithm can be implemented iteratively, but sometimes only with great difficulty. However, a recursive solution will always run more slowly than an iterative one because of the overhead of opening and closing the recursive calls. Pitfalls of Recursion

Fibonacci’s Problem 35 “Fibonacci” ( Leonardo de Pisa)

Fibonacci’s Problem

Rabbit Rules 1.All pairs of rabbits consist of a male and female 2.One pair of newborn rabbits is placed in hutch on January 1 3.When this pair is 2 months old they produce a pair of baby rabbits 4.Every month afterwards they produce another pair 5.All rabbits produce pairs in the same manner 6.Rabbits don’t die 37

Fibonacci’s Problem How many pairs of rabbits will there be 12 months later? 38

Jan 1 Feb 1 Mar

40 Apr 1 May 1 Mar

Apr 1 May 1 June

Pairs this month In General, Pairs last monthPairs of newborns =+ Pairs this monthPairs last month Pairs 2 months ago =+

Fibonacci Numbers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,... where each number is the sum of the preceding two. Recursive definition: F(0) = 0; F(1) = 1; F(number) = F(number-1)+ F(number-2); 43

Fibonacci Numbers int Fibonacci(int n) { if(n == 0) return 0; else if (n == 1) return 1; else return Fibonacci(n-1)+Fibonacci(n-2); }

Fibonacci Numbers int Fibonacci(int n) { if(n == 0) return 0; else if (n == 1) return 1; else return Fibonacci(n-1)+Fibonacci(n-2); } Base case

Fibonacci Numbers int Fibonacci(int n) { if(n == 0) return 0; else if (n == 1) return 1; else return Fibonacci(n-1)+Fibonacci(n-2); } General case

F(5) Return F(4)+F(3) F(4) Return F(3)+F(2) F(3) Return F(2)+F(1) F(2) Return F(1)+F(0) F(1) Return 1 F(0) Return 0 F(2) Return F(1)+F(0) F(0) Return 0 F(1) Return 1 F(0) Return 0 F(2) Return F(1)+F(0) F(1) Return 1 F(3) Return F(2)+F(1) F(1) Return 1 F(1) Return Tracing Fibonacci(5)

Given n things, how many different sets of size k can be chosen? with base cases: Another Example: n choose r (combinations)

int Combinations(int n, int r) { if(r == 1) // base case 1 return n; else if (n == r) // base case 2 return 1; else //general case return(Combinations(n-1, r-1) + Combinations(n-1, r)); } n choose r (Combinations)

Tracing Combinations(4,3)