Programming Recursion “To Iterate is Human, to Recurse, Divine” -- L. Peter Deutsch.

Slides:



Advertisements
Similar presentations
More Recursion: Permutations and Towers of Hanoi COP 3502.
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Functions a group of declarations and statements that is assigned a name effectively, a named statement block usually has a value a sub-program when we.
Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful:  for example, in.
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)
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
1 Gentle Introduction to Programming Session 3: Higher Order Functions, Recursion.
Exercise 4 Recursion. Functions – a short reminder a group of variables and statements that is assigned a name a sub-program  inside main we can call.
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.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
1 Gentle Introduction to Programming Session 4: Arrays.
Data Structures Using C++ 2E Chapter 6 Recursion.
Data Structures Using C++ 2E Chapter 6 Recursion.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 17: Recursion.
Ceng-112 Data Structures I Chapter 6 Recursion.
Recursion. Functions – reminder A function can call other functions. Return causes the execution of the function to terminate and returns a value to the.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
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.
Return function Random function Recursion function Function in C 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 8 Recursion Modified.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 6 Recursion.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
Functions. Why use functions? They can break your problem down into smaller sub-tasks (modularity).  easier to solve complex problems They make a program.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Senem Kumova Metin // CS115 // FUNCTIONS CHAPTER 5.
1 Recursion. 2 A process by which a function calls itself repeatedly  Either directly. X calls X  Or cyclically in a chain. X calls Y, and Y calls X.
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 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality in the process.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Program Development and Design Using C++, Third Edition
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Chapter 9 Recursion. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe recursive function is –a.
Recursion CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Chapter Topics Chapter 16 discusses the following main topics:
COMP 51 Week Fourteen Recursion.
Topic 6 Recursion.
Data Structures with Java © Rick Mercer
Recursion DRILL: Please take out your notes on Recursion
Abdulmotaleb El Saddik University of Ottawa
Functions Department of Computer Science-BGU יום רביעי 12 ספטמבר 2018.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Java Software Structures: John Lewis & Joseph Chase
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Formatted and Unformatted Input/Output Functions
Recursive Definitions
Chapter 12 Recursion (methods calling themselves)
Recursion Chapter 11.
Recursion Department of Computer Science-BGU יום שני 10 דצמבר 2018.
A function with one argument
Java Programming: Chapter 9: Recursion Second Edition
Functions Department of Computer Science-BGU יום שישי 26 אפריל 2019.
Dr. Sampath Jayarathna Cal Poly Pomona
Recursion.
Recursive Thinking.
Presentation transcript:

Programming Recursion “To Iterate is Human, to Recurse, Divine” -- L. Peter Deutsch

Functions – Reminder A group of variables and statements that is assigned a name A sub-program  A function may call other functions. return-type name(arg_type1 arg_name1, arg_type2 arg_name2, …) { function body; return value; }

Return Statement - Reminder Return causes the execution of the function to terminate and returns a value to the caller.

Scope of Variables - Reminder A variable declared within a function is unrelated to variables declared elsewhere A function cannot directly access variables that are declared in other functions

Function Declaration - Reminder We can call a function from the point in the file in which the function has been declared, until the end of the file. return-type name(arg_type1 arg_name1, arg_type2 arg_name2, …);

Recursive Function A function defined in terms of itself is called a recursive function. return_value rec_func(type arg1, type arg2, …) { … rec_func(…); … }

As we saw, n! = 1*2*3*… *(n-1)*n Thus, we can also define factorial the following way:  0! = 1  n! = n*(n-1)! for n>0 (n-1)!* n Factorial

A Recursive Definition C functions can call themselves!  However, not with the same parameters (why?) Some functions can be defined using smaller occurrences of themselves. Such a definition is called a “recursive definition” of a function. Every recursive function must have a “boundary condition”. The function stops calling itself when it is satisfied.  Why is this necessary?

Example - Factorial int fact_rec(int n) { if (n == 0) return 1; return n * fact_rec(n-1); } int fact_itr(int n) { int fact = 1; while (n >= 1) { fact *= n; --n; } return fact; }

Recursive factorial int fact_rec(int n) { if (n == 0) return 1; return n * fact_rec(n-1); } n ← 4 return ← ?

Recursive factorial int fact_rec(int n) { if (n == 0) return 1; return n * fact_rec(n-1); } n ← 4 return ← 4 * ?

Recursive factorial int fact_rec(int n) { if (n == 0) return 1; return n * fact_rec(n-1); } n ← 4 return ← 4 * ? n ← 3 return ← ?

Recursive factorial int fact_rec(int n) { if (n == 0) return 1; return n * fact_rec(n-1); } n ← 4 return ← 4 * ? n ← 3 return ← 3 * ?

Recursive factorial int fact_rec(int n) { if (n == 0) return 1; return n * fact_rec(n-1); } n ← 4 return ← 4 * ? n ← 3 return ← 3 * ? n ← 2 return ← ?

Recursive factorial int fact_rec(int n) { if (n == 0) return 1; return n * fact_rec(n-1); } n ← 4 return ← 4 * ? n ← 3 return ← 3 * ? n ← 2 return ← 2 * ?

Recursive factorial int fact_rec(int n) { if (n == 0) return 1; return n * fact_rec(n-1); } n ← 4 return ← 4 * ? n ← 3 return ← 3 * ? n ← 2 return ← 2 * ? n ← 1 return ← ?

Recursive factorial int fact_rec(int n) { if (n == 0) return 1; return n * fact_rec(n-1); } n ← 4 return ← 4 * ? n ← 3 return ← 3 * ? n ← 2 return ← 2 * ? n ← 1 return ← 1 * ?

Recursive factorial int fact_rec(int n) { if (n == 0) return 1; return n * fact_rec(n-1); } n ← 4 return ← 4 * ? n ← 3 return ← 3 * ? n ← 2 return ← 2 * ? n ← 1 return ← 1 * ? n ← 0 return ← ?

Recursive factorial int fact_rec(int n) { if (n == 0) return 1; return n * fact_rec(n-1); } n ← 4 return ← 4 * ? n ← 3 return ← 3 * ? n ← 2 return ← 2 * ? n ← 1 return ← 1 * ? n ← 0 return ← 1

Recursive factorial int fact_rec(int n) { if (n == 0) return 1; return n * fact_rec(n-1); } n ← 4 return ← 4 * ? n ← 3 return ← 3 * ? n ← 2 return ← 2 * ? n ← 1 return ← 1 * 1

Recursive factorial int fact_rec(int n) { if (n == 0) return 1; return n * fact_rec(n-1); } n ← 4 return ← 4 * ? n ← 3 return ← 3 * ? n ← 2 return ← 2 * 1

Recursive factorial int fact_rec(int n) { if (n == 0) return 1; return n * fact_rec(n-1); } n ← 4 return ← 4 * ? n ← 3 return ← 3 * 2

Recursive factorial int fact_rec(int n) { if (n == 0) return 1; return n * fact_rec(n-1); } n ← 4 return ← 4 * 6

Another Example void print_nums(int num) { if (num == 0) return; printf("%d ", num); print_nums(num - 1); printf("%d ", num); } print call func print return

What Does It Do? void foo() { int num; scanf("%d", &num); if (num < 0) return; foo(); printf("%d ", num); }

Reverse Print void reverse_print() { int num; scanf("%d", &num); if (num < 0) return; reverse_print(); printf("%d ", num); } Reads an unbounded series of numbers from the user and prints them in reverse order.

Power x y = x*x*…*x Recursive definitions (assume non-negative y): 1,y = 0 x y = x*x y-1,y odd (x y/2 ) 2,y even y times

rec_pow int rec_pow(int x, int y) { if (y == 0) return 1; if (y % 2 != 0) return x * rec_pow(x, y - 1); else return square(rec_pow(x, y / 2)); }

The Three Rules of Recursion 1.Know when to stop. 2.Decide how to take one step. 3.Break the problem down into that step plus a smaller problem.

Exercise Write a program that receives two non- negative integers and computes their product recursively. Hint: Notice that the product a*b is actually a+a+…+a (b times).

Solution int rec_mul(int a, int b) { /* base condition – a * 0 = 0 */ if (b == 0) return 0; /* save a and call recursively */ return a + rec_mul(a, b-1); }

Exercise Given the following iterative version of sum- of-digits calculation Find the recursive definition of this function (don’t forget the base case!) int sum_digits(int n) { int sum = 0; while (n > 0) { sum += n%10; n = n/10; } return sum; }

Solution int sum_digit_rec(int n) { if (n == 0) return 0; return n % 10 + sum_digit_rec(n / 10); }

More uses Recursion is a general approach to programming functions Its uses are not confined to calculating mathematical expressions! For example – max_rec.c

Step By Step int max_rec(int *arr, int size) { int rest_max; if (size == 1) return *arr; rest_max = max_rec(arr + 1, size - 1); if (*arr > rest_max) return *arr; else return rest_max; } 1472 arr: max_rec(, 4) rest_max = max_rec(, 3) rest_max = max_rec(, 2) rest_max = max_rec(, 1) rest_max = 2 7 7

What Does It Do? char *rec_func(char *str, char c) { if (*str == '\0') return NULL; if (*str == c) return str; return rec_func(++str, c); }

Solution A recursive implementation of strchr

Exercise Give a recursive implementation to int strcmp(const char *s, const char *t); The function compares two strings and return:  0 – if s == t  negative – if s < t  positive – if s > t Write a program that accepts two strings from the user and compare them using the above function

Solution int strcmp(cons char *s, const char *t) { if (*s != *t) return *s - *t; if (*s == '\0') return 0; return strcmp(++s, ++t); }

Towers of Hanoi Initial Setup:  Tower A contains n disks of different sizes.  Disks can only go on top of smaller disks (or directly on the board). ACB

Towers of Hanoi Objective:  Move all n disks from tower A to tower C. ACB

Towers of Hanoi Moves:  Take the top disk of a tower and move the disk to another tower.  No disk may be put on top of a smaller disk. ACB

Towers of Hanoi Move tower with n disks is equivalent to: 1.Move tower with n-1 disks ACB

Towers of Hanoi Move tower with n disks is equivalent to: 2. Move a single disk ACB

Towers of Hanoi Move tower with n disks is equivalent to: 3. Move tower with n-1 disks ACB

Towers of Hanoi - C void move_disk(char from, char to) { printf("move disk from %c to %c\n", from, to); } void move_tower(int height, char from, char to, char temp) { if (height == 1) { move_disk(from, to); } else { move_tower(height - 1, from, temp, to); move_disk(from, to); move_tower(height - 1, temp, to, from); }