Recursion.

Slides:



Advertisements
Similar presentations
CS1010 Programming Methodology
Advertisements

Exercise 4 1. Write a program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. Let the program toss the.
Modular Programming (2) H&K Chapter 6 Instructor – Gokcen Cilingir Cpt S 121 (July 8, 2011) Washington State University.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 14: Recursion by.
Programming Recursion “To Iterate is Human, to Recurse, Divine” -- L. Peter Deutsch.
ICS103 Programming in C Lecture 11: Recursive Functions
CSC 2300 Data Structures & Algorithms January 30, 2007 Chapter 2. Algorithm Analysis.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 14 Recursion.
1 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions.
Introduction Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion.
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
1 Decrease-and-Conquer Approach Lecture 06 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Programming Principles II Lecture Notes 5 Recursion Andreas Savva.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
CP104 Introduction to Programming Recursion 2 Lecture 29 __ 1 Recursive Function for gcd Recursive formula for the greatest common divisor of m and n,
Examples of Recursion Data Structures in Java with JUnit ©Rick Mercer.
Advanced Computer Architecture and Parallel Processing Rabie A. Ramadan http:
Using Recursion to Convert Number to Other Number Bases Data Structures in Java with JUnit ©Rick Mercer.
RECURSION Go back, Jack, and do it again.. Recursion 2  Recursion occurs whenever "something" is defined in terms of itself.  Structural recursion:
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Recitation 8 Programming for Engineers in Python.
Concepts of Algorithms CSC-244 Unit 5 and 6 Recursion Shahid Iqbal Lone Computer College Qassim University K.S.A.
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.
WEEK 10 Class Activities Lecturer’s slides.
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! =
Program Development and Design Using C++, Third Edition
Chapter 9 Recursion. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe recursive function is –a.
CS212: Data Structures and Algorithms
Tutorial #4 Summer 2005.
Chapter 19: Recursion.
Recursion CSE 2320 – Algorithms and Data Structures
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.
CS1010 Programming Methodology
Data Structures 1st Week
Introduction to Computer Algorithmics and Programming Ceng 113
Topic 6 Recursion.
MSc/ICY Software Workshop , Semester 2
Recursion DRILL: Please take out your notes on Recursion
Decrease-and-Conquer Approach
Programming Functions.
Recursion Chapter 12.
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion CSE 2320 – Algorithms and Data Structures
Computer Science 101 While Statement.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
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
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Recursion Chapter 11.
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion UW CSE 160 Winter 2017
Recursion Spring 2015 UW CSE 160
Recursion UW CSE 160 Spring 2018
Programming application CC213
CSC215 Homework Homework 04 Due date: Oct 14, 2016.
Recursion Winter 2014 UW CSE 140
Recursion Department of Computer Science-BGU יום שני 10 דצמבר 2018.
A function with one argument
Week 2 Variables, flow control and the Debugger
Recursion UW CSE 160 Winter 2016
Extra Practice for Recursion
Yan Shi CS/SE 2630 Lecture Notes
ICS103 Programming in C Lecture 11: Recursive Functions
Presentation transcript:

Recursion

What Does this Program Do? void func(){ char c; c = getchar(); if (c == '\n') return; func(); putchar(c); { int main(){ return 0;

What Does this Program Do? void func(){ char c; c = getchar(); if (c == '\n') return; func(); putchar(c); { int main(){ return 0; Input: live func c = ‘l’

What Does this Program Do? void func(){ char c; c = getchar(); if (c == '\n') return; func(); putchar(c); { int main(){ return 0; Input: live func c = ‘l’ func c = ‘i’

What Does this Program Do? void func(){ char c; c = getchar(); if (c == '\n') return; func(); putchar(c); { int main(){ return 0; Input: live func c = ‘l’ func c = ‘i’ func c = ‘v’

What Does this Program Do? void func(){ char c; c = getchar(); if (c == '\n') return; func(); putchar(c); { int main(){ return 0; Input: live func c = ‘l’ func c = ‘i’ func c = ‘v’ func c = ‘e’

What Does this Program Do? void func(){ char c; c = getchar(); if (c == '\n') return; func(); putchar(c); { int main(){ return 0; Input: live e func c = ‘l’ func c = ‘i’ func c = ‘v’ func c = ‘e’ func c = ‘\n’

What Does this Program Do? void func(){ char c; c = getchar(); if (c == '\n') return; func(); putchar(c); { int main(){ return 0; Input: live ev func c = ‘l’ func c = ‘i’ func c = ‘v’ func c = ‘e’ func c = ‘\n’

What Does this Program Do? void func(){ char c; c = getchar(); if (c == '\n') return; func(); putchar(c); { int main(){ return 0; Input: live evi func c = ‘l’ func c = ‘i’ func c = ‘v’ func c = ‘e’ func c = ‘\n’

What Does this Program Do? void func(){ char c; c = getchar(); if (c == '\n') return; func(); putchar(c); { int main(){ return 0; Input: live evil func c = ‘l’ func c = ‘i’ func c = ‘v’ func c = ‘e’ func c = ‘\n’

The Three Rules of Recursion Base (termination) condition Decomposition to smaller instances Use solutions to smaller instances to solve the original problem

Reverse Print Recursive Call void reverse_print(){ char c; c = getchar(); if (c == '\n') return; func(); putchar(c); { int main(){ return 0; Recursive Call

Exercise Write a program that receives an integer and returns its sum of digits. We do not know the number of digits in advance. Example: the sum of digits of 369 is 18.

GCD – Greatest Common Divisor Definition: For two or more non-zero integers, The GCD is the largest positive integer that divides both numbers without a remainder. Examples: GCD(12, 8) = 4 GCD(12, 4) = 4 GCD(12, 5) = 1

GCD – Euclid’s Algorithm 616 % 143 = 44  GCD(143, 44) 143 % 44 = 11  GCD(44, 11) 44 % 11 = 0  11

GCD - Code #include <stdio.h> int gcd(int m, int n) { if (m == 0) return n; if (n == 0) return m; return gcd(n, m % n); } int main() { int num1, num2, div; printf("Enter two positive numbers: "); scanf("%d %d", &num1, &num2); div = gcd(num1, num2); printf(“the GCD is %d\n", div); return 0; }

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

Fast Power - Code int rec_power(int x, int y) { int tmp; if (y == 0) return 1; if (y % 2 != 0) return x * rec_power(x, y - 1); else tmp = rec_power(x, y / 2); return tmp*tmp; }

Fast Power - Run rec_power(2, 5) rec_power(2, 5) x = 2, y = 5 return 2 * rec_power x = 2, y = 4 return rec_power ^2 x = 2, y = 2 return rec_power ^2 x = 2, y = 1 return 2* rec_power x = 2, y = 0 return 1;

Binomial Coefficient = Definition 1: The coefficient of xk in the expansion of the binomial power (1 + x)n. Definition 2: The number of ways that k elements can be "chosen" from the set {1, 2, 3, …, n}.

Binomial Coefficient Think recursion: If the nth element is selected – there are k-1 elements to choose from {1, …, n-1}. If the nth element is not selected – there are k elements to choose from {1, …, n-1}.

Binomial Coefficient Recursive formula: Termination Criteria: k > n  0 k = 0  1 k == n  1

Binomial Coefficient – from Formula to Code int binomial_coef(int n, int k) { // termination criteria if ((k == 0) || (n == k)) return 1; if (n < k) return 0; return binomial_coef(n-1, k-1) + binomial_coef(n-1, k); }

Binomial Coefficient – Recursion Tree k = 2 int main() { binomial_coef(4, 2); return 0; } n = 3 k = 2 n = 3 k = 1 n = 2 k = 1 n = 2 k = 0 n = 2 k = 1 n = 2 k = 2 n = 1 k = 0 n = 1 k = 1 n = 1 k = 0 n = 1 k = 1 n = 0 k = 0 n = 0 k = 1 n = 0 k = 0 n = 0 k = 1

Reverse digits Receive an Integer number, reverse the order of its digits We already wrote the iterative version. How can we solve it recursively?

Reverse Digits Example: reverse_digits(573824) Wishful Thinking: If I only knew reverse_digits(73824)… Problem – for a number with d digits, the last digit should be multiplied by 10d. But how can I know d in advance? Solution – Call a helper function that diffuses the temporal result throughout the recursion. - What is the termination criterion?

Reverse Digits - Code int helper(int n, int res) int main() { { if (n == 0) return res; return helper(n/10, 10*res + n%10); } int reverse_digits(int num) return helper(num, 0); int main() { int num; printf("Insert an integer\n"); scanf("%d", &num); printf("%d\n", reverse_digits(num)); return 0; }

Sum Digits - Solution int sum_digits(int num)} if (num < 10) return num; return sum_digits (num/10) + num%10; { int main(){ int num = 0, sum = 0; printf("Insert an integer\n"); scanf("%d", &num); sum = sum_digits(num); printf("%d sum of digits is %d\n", num, sum); return 0;