ECE 2574: Data Structures and Algorithms - Recursion Part I. C. L. Wyatt

Similar documents
Inductive Definition to Recursive Function

Recursion. Lars-Henrik Eriksson. Functional Programming 1. Based on a presentation by Tjark Weber and notes by Sven-Olof Nyström

CS 310 Advanced Data Structures and Algorithms

Programming & Data Structure Laboratory. Day 2, July 24, 2014

CS 97SI: INTRODUCTION TO PROGRAMMING CONTESTS. Jaehyun Park

Loops / Repetition Statements

Mathematics. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Theory of control structures

Chapter 4 Functions By C.K. Liang

CSci 1113, Fall 2015 Lab Exercise 4 (Week 5): Write Your Own Functions. User Defined Functions

Lecture 7: General Loops (Chapter 7)

I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub

Question Bank (SPA SEM II)

Recursion. CSCI 112: Programming in C

Programming & Data Structure

34. Recursion. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Module 05: Types of recursion

CSE 230 Intermediate Programming in C and C++ Recursion

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS

Case by Case. Chapter 3

Standard Version of Starting Out with C++, 4th Edition. Chapter 19 Recursion. Copyright 2003 Scott/Jones Publishing

Notes - Recursion. A geeky definition of recursion is as follows: Recursion see Recursion.

OVERVIEW. Recursion is an algorithmic technique where a function calls itself directly or indirectly. Why learn recursion?

Chapter 13. Recursion. Copyright 2016 Pearson, Inc. All rights reserved.

Introduction to Programming (Java) 4/12

ESc101 : Fundamental of Computing

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Recursion and Induction

Creating a new data type

Fundamentals of Programming Session 13

Lecture Notes, CSE 232, Fall 2014 Semester

- Wikipedia Commons. Intro Problem Solving in Computer Science 2011 McQuain

Recursion: Recursion: repeated curses

ASSIGNMENT 4 SOLUTIONS

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion

CS103L SPRING 2017 UNIT 8: RECURSION

UNIT 3: ANALYSIS OF SIMPLE ALGORITHMS

UNIT 1 ANALYSIS OF ALGORITHMS

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

Exponentiation. Evaluation of Polynomial. A Little Smarter. Horner s Rule. Chapter 5 Recursive Algorithms 2/19/2016 A 2 M = (A M ) 2 A M+N = A M A N

Algorithm Analysis. CENG 707 Data Structures and Algorithms

What is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself.

PDF created with pdffactory Pro trial version Recursion

Recursion. Comp Sci 1575 Data Structures. Introduction. Simple examples. The call stack. Types of recursion. Recursive programming

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

Conditionals !

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

ECE 2574: Data Structures and Algorithms - Basic Sorting Algorithms. C. L. Wyatt

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

CS 173 [A]: Discrete Structures, Fall 2012 Homework 8 Solutions

What is recursion? Recursion. How can a function call itself? Recursive message() modified. contains a reference to itself. Week 7. Gaddis:

This session. Recursion. Planning the development. Software development. COM1022 Functional Programming and Reasoning

Structured programming

COP 4516: Math for Programming Contest Notes

CSCI 136 Data Structures & Advanced Programming. Fall 2018 Instructors Bill Lenhart & Bill Jannen

ECE G205 Fundamentals of Computer Engineering Fall Exercises in Preparation to the Midterm

Dept. of CSE, IIT KGP

CSCI 2041: Functions, Mutation, and Arrays

Any Integer Can Be Written as a Fraction

Recursion. CSE 2320 Algorithms and Data Structures University of Texas at Arlington

Functions Introduction to Algorithms and Programming II School of Computer Science Term: Summer 2013 Instructor: Dr. Asish Mukhopadhyay

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

11/2/2017 RECURSION. Chapter 5. Recursive Thinking. Section 5.1

Simplifying Square Root Expressions[In Class Version][Algebra 1 Honors].notebook August 26, Homework Assignment. Example 5 Example 6.

Names and Functions. Chapter 2

SOLUTION FOR FUNCTION. 1) Write a program to display Hello 10 times. Create user-defined function message().

Recall our recursive multiply algorithm:

Analysis of Recursive Algorithms

CS 101: Computer Programming and Utilization

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Chapter 10: Recursive Problem Solving

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

Recursion. General Algorithm for Recursion. When to use and not use Recursion. Recursion Removal. Examples

Warmup: On paper, write a C++ function that takes a single int argument (n) and returns the product of all the integers between 1 and n.

BUGCATCHER SITE REVIEW Version 1, Cassie Chin, 10/10/2014,

Recursion Chapter 3.5

CSE 215: Foundations of Computer Science Recitation Exercises Set #4 Stony Brook University. Name: ID#: Section #: Score: / 4

CS103 Unit 8. Recursion. Mark Redekopp

Recursion. Tjark Weber. Functional Programming 1. Based on notes by Sven-Olof Nyström. Tjark Weber (UU) Recursion 1 / 37

What is an algorithm?

Data Structures and Algorithms CMPSC 465

Recursion. Manolis Koubarakis. Data Structures and Programming Techniques

Chapter 7. Iteration. 7.1 Multiple assignment

Recursive Definitions

A function that invokes itself is said to

Counting. Andreas Klappenecker

Sorting Pearson Education, Inc. All rights reserved.

CS103 Unit 8. Recursion. Mark Redekopp

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

There are three questions on this exam. You have 2 hours to complete it. Please indent your program so that it is easy for the grader to read.

Functions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction

Software Development. Modular Design and Algorithm Analysis

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from

Computer Science 210 Data Structures Siena College Fall Topic Notes: Recursive Methods

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

Algorithm Discovery and Design. Why are Algorithms Important? Representing Algorithms. Chapter 2 Topics: What language to use?

CMSC 132: Object-Oriented Programming II. Recursive Algorithms. Department of Computer Science University of Maryland, College Park

MODULE 2: Branching and Looping

Transcription:

ECE 2574: Data Structures and Algorithms - Recursion Part I C. L. Wyatt

Today we will introduce the notion of recursion, look at some examples, and see how to implement them in code. Introduction to recursion Warmup Examples Exercise: Recursive Egyptian Powers

Top-down Design Top down design divides problems into smaller and easier sub-problems. The hope is, at each of these successive levels, these sub-problems are easier to solve.

Recursion In some cases, the solution to lowest level sub-problem can be applied at the higher level. This type of solution is called recursive.

A graphical view Graphical depictions of algorithms show connected boxes.

Recursive solutions form nested boxes. The inner-most box solves the lowest-level problem, The next box solves the next level above. In a recursive solution the algorithm is the same at each level.

Example: n!, the factorial Simple iterative solution to compute the factorial int factorial (int n) { int result = n; do { n -= 1; result = result*n; } while (n > 1); return result; }; How large an n will this work for in a real programming languages?

We can break the factorial solution into a recursive solution. n! = 1*2*3*4*...*n grouping terms n! = 1*2*3*4*...*(n -1)*n n! = ( (n -1)! )*n This is an example of a recurrence relation.

A recursive solution to the factorial int factorial (int n) { if (n <= 1) return 1; else return(n*factorial(n-1)); };

A recursive solution to the factorial Graphically each box takes the output of the box inside it and multiplies by the integer in that box. Example: 4! = 4*3*2*1 = (4*(3*(2*(1))))

Formal definition of recursion A recursive procedure is one whose evaluation at (non-initial) inputs involves invoking the procedure itself at another input. In the case of the factorial this involves invoking the procedure at (n-1) when the input is n: n! = n*(n-1)! Recursion is a very powerful tool in the design and analysis of algorithms. Often complex problems have very simple recursive solutions.

What makes recursive procedures work? * At each invocation, the solution must get closer to a known solution i.e. 0! = 1! = 1 The procedure calls must terminate in a finite number, that is the function must not endlessly call itself. Otherwise the recursion is infinite

Recursive version of the GCD algorithm Recall the GCD algorithm A.0 If m < n, swap m and n. A.1 Divide m by n and let r be the remainder. A.2 If r = 0, terminate; n is the answer. A.3 Set m to n, n to r, and go back to step A.1. A recursive solution (after step 0). int gcd(int m, int n) { if( n == 0 ) return m; else return gcd(n, m%n); }

Recursive version of the GCD algorithm The recurrence relation for GCD gcd(m,n) = gcd(n, m mod n) The stopping condition (base case) is n = 0 Example: gcd(131,62) -> gcd(62,7) -> gcd(7,6) -> gcd (6,1) -> gcd(1,0)

Warmup #1 Which of the following C++ functions correctly computes the sum from 1 to n using recursion? int function1(int n){ int sum = 0; for(int i = 1; i <= n; ++i){ sum += i; } return sum; } Incorrect. (15%).

Warmup #1 Which of the following C++ functions correctly computes the sum from 1 to n using recursion? int function2(int n){ if(n == 1) return 1; return n + function2(n-1); } Correct (81%).

Warmup #1 Which of the following C++ functions correctly computes the sum from 1 to n using recursion? int function3(int n){ return n*(n+1)/2; } Incorrect. (3%).

Warmup #2 What would happen if function1 in the previous question was called with an argument of -1? int function1(int n){ int sum = 0; for(int i = 1; i <= n; ++i){ sum += i; } return sum; } The correct answer is it would return 0 (63%).

Warmup #3 What would happen if function2 in the previous question was called with an argument of -1? int function2(int n){ if(n == 1) return 1; return n + function2(n-1); } The correct answer is A run-time error would occur (73%).

Warmup #4 What would happen if function3 in the previous question was called with an argument of -1? int function3(int n){ return n*(n+1)/2; } The correct answer is It would return 0 (85%).

Another Example: Exponentiation The Egyptian Powers algorithm computes x to the power n by repeated squaring. The recurrence relation for computing x n for any positive integer n: { x n = (x x) n/2 n even x(x x) (n 1)/2 n odd

Exercise: write a recursive function and a set of tests implementing the Egyptian Powers algorithm. In pseudo-code function RecPowers (x, n) Input: a real number x and positive integer n Output: x raised to power n if (n == 1) // initial condition pow = x; else if even(n) then pow = RecPowers(x*x,n/2) else pow = x*recpowers(x*x,(n-1)/2) endif endif return (pow) endfunction

Next Actions and Reminders Read CH pp. 67-87 Warmup before noon on Monday. Program 0 due tonight by 11:59 PM.