Data Structures And Algorithms

Similar documents
Experiment: The Towers of Hanoi. left middle right

recursive algorithms 1

Recursive Definitions

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

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

CS302 - Data Structures using C++

Recursive Methods and Problem Solving. Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms

Recursion. Example R1

We cover recursion in 150. Why do it again in 151?

Bisection method. we can implement the bisection method using: a loop to iterate until f(c) is close to zero a function handle to the function f

1.7 Recursion. Department of CSE

Unit #2: Recursion, Induction, and Loop Invariants

What is Recursion? ! Each problem is a smaller instance of itself. ! Implemented via functions. ! Very powerful solving technique.


Chapter 5: Recursion. Objectives

Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved

Stack. Data structure with Last-In First-Out (LIFO) behavior. Out

Recursion vs Induction

Tail Recursion. ;; a recursive program for factorial (define fact (lambda (m) ;; m is non-negative (if (= m 0) 1 (* m (fact (- m 1))))))

Recursion vs Induction. CS3330: Algorithms The University of Iowa

Unit #3: Recursion, Induction, and Loop Invariants

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1

Announcements. Recursion and why study it. Recursive programming. Recursion basic idea

Chapter 15: Recursion

CSC 273 Data Structures

Recursion (Rosen, 6 th edition, Section 4.3, 4.4)

05-15/17 Discussion Notes

CS 310 Advanced Data Structures and Algorithms

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

Recursion. February 02, 2018 Cinda Heeren / Geoffrey Tien 1

11. Recursion. n (n 1)!, otherwise. Mathematical Recursion. Recursion in Java: Infinite Recursion. 1, if n 1. n! =

Functions. Chapter 5

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

12. Recursion. n (n 1)!, otherwise. Educational Objectives. Mathematical Recursion. Recursion in Java: 1, if n 1. n! =

Recursion. Tracing Method Calls via a Stack. Beyond this lecture...

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

Chapter 6 Recursion. The Concept of Recursion

15. Pointers, Algorithms, Iterators and Containers II

Chapter 5: Recursion

CS1 Lecture 15 Feb. 18, 2019

Decision-Making and Repetition

Functions. Autumn Semester 2009 Programming and Data Structure 1. Courtsey: University of Pittsburgh-CSD-Khalifa

Chapter 17 Recursion

Warm-Up! Coding Bat Ap-1

Recursion Chapter 4 Self-Reference. Recursive Definitions Inductive Proofs Implementing Recursion

Where does the insert method place the new entry in the array? Assume array indexing starts from 0(zero).

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

Identify recursive algorithms Write simple recursive algorithms Understand recursive function calling

Programming with Recursion. What Is Recursion?

def F a c t o r i a l ( n ) : i f n == 1 : return 1 else : return n F a c t o r i a l ( n 1) def main ( ) : print ( F a c t o r i a l ( 4 ) )

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

Resources matter. Orders of Growth of Processes. R(n)= (n 2 ) Orders of growth of processes. Partial trace for (ifact 4) Partial trace for (fact 4)

Recursion. notes Chapter 8

EE 368. Weeks 4 (Notes)

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

Copyright The McGraw-Hill Companies, Inc. Persion required for reproduction or display. What is Recursion?

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

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

CS1 Lecture 15 Feb. 19, 2018

Recursion. ! When the initial copy finishes executing, it returns to the part of the program that made the initial call to the function.

Lecture 8 Recursion. The Mirrors

Print words entered, but backwards. Solving Problems Recursively. Faster exponentiation. Exponentiation

Comp215: More Recursion

UNIT 5A Recursion: Basics. Recursion

UNIT 5A Recursion: Basics. Recursion

Reduction & Recursion Overview

Types of Recursive Methods

SOFTWARE DEVELOPMENT 1. Recursion 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

Recursive Problem Solving

Recursion. Jordi Cortadella Department of Computer Science

Recursion. Based on Chapter 7 of Koffmann and Wolfgang. Chapter 7: Recursion 1

Recursion: Factorial (1) Recursion. Recursion: Principle. Recursion: Factorial (2) Recall the formal definition of calculating the n factorial:

Chapter 13 Recursion. Chapter Objectives

DS Assignment II. Full Sized Image

Solving problems by recursion

Lecture 10: Recursion vs Iteration

Recursion. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

What is Recursion? Chapter 17 Recursion. Executing RunningSum. High-Level Example: Binary Search

CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion

Fundamentals of Recursion. Solving Problems Recursively. Recursive example. Recursive example

Data Structures and Algorithms

Algorithm Analysis. CENG 707 Data Structures and Algorithms

Recursion. COMS W1007 Introduction to Computer Science. Christopher Conway 26 June 2003

Tribhuvan University Institute of Science and Technology Computer Science and Information Technology (CSC. 154) Section A Attempt any Two questions:

Tree traversals. Review: recursion Tree traversals. October 05, 2017 Cinda Heeren / Geoffrey Tien 1

Recursion. Let s start by looking at some problems that are nicely solved using recursion. First, let s look at generating The Fibonacci series.

CSE 341 Lecture 5. efficiency issues; tail recursion; print Ullman ; 4.1. slides created by Marty Stepp

9/16/14. Overview references to sections in text RECURSION. What does generic mean? A little about generics used in A3

CMPSCI 250: Introduction to Computation. Lecture #14: Induction and Recursion (Still More Induction) David Mix Barrington 14 March 2013

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How?

Recursion. Chapter 17 CMPE13. Cyrus Bazeghi

What Is a Function? Illustration of Program Flow

Algorithmic Methods Tricks of the Trade

Lecture 7 CS2110 Fall 2014 RECURSION

Test Bank Ver. 5.0: Data Abstraction and Problem Solving with C++: Walls and Mirrors, 5 th edition, Frank M. Carrano

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

Chapter 10. Implementing Subprograms

Recursion. Recursion is: Recursion splits a problem:

MEIN 50010: Python Recursive Functions

Transcription:

Data Structures And Algorithms Recursion Eng. Anis Nazer First Semester 2016-2017

Recursion Recursion: to define something in terms of itself Example: factorial n!={ 1 n=0 n (n 1)! n>0

Recursion Example: factorial of 4 = 4 x factorial of 3 factorial of 3 = 3 x factorial of 2 factorial of 2 = 2 x factorial of 1 factorial of 1 = 1 x factorial of 0 factorial of 0 = 1

Recursive function A recursive function is a function that calls itself, directly or indirectrly int f() {.. f().. }

Recursion Direct recursion: function calls itself Indirect recursion: function1 calls function2 function2 calls function3 function3 calls function1

Recursion Recursion is like a loop To stop the recursion, a base case should be written The base case does not call the function and stops the recursion

Example Factorial: int fact(int n) { if ( n == 0 ) return 1; else { int r = n * fact( n 1 ); return r; } }

Example what happens if you call: cout << fact(3) ; main().. cout << fact(3) ;.. 6 3 fact(3) n 3 r = 3 * fact(2) return 6; 2 fact(2) 2 n 2 r = 2 * fact(1) return 2; 1 fact(1) 1 n 1 r = 1 * fact(0) return 1; 0 fact(0) 1 n return 1; 0

Function calls Activation record: is the function data, local variables return address, return value etc When a function is called: the operating system pushes the activation record in a runtime stack When a function returns: the operating system pops the activation record from the runtime stack The runtime stack is managed by the operating system

Example What does this function do? int fun(int x, int y) { if ( y == 1 ) return x; else { int r = x + fun( x, y 1 ); return r; } }

Fibonacci series Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13, 21, start with: 0, 1 each number is the sum of the previous two numbers

Fibonacci series the function: fib(n)={ n n<2 fib(n 1)+fib(n 2) n 2

Fibonacci series fib(0) = 0 fib(1) = 1 fib(2) = fib(1) + fib(0) = 1 + 0 = 1 fib(3) = fib(2) + fib(1) = 1 + 1 = 2 fib(4) = fib(3) + fib(2) = 2 + 1 = 3 fib(6) =?

Fibonacci series Fibonacci can be easily implemented using a recursive function: int fib( int n ) { if ( n < 2 ) return n; else return fib(n 1) + fib(n 2); }

Fibonacci series How many function calls are performed if you call fib(4)? fib(4) fib(3) fib(2) fib(2) fib(1) fib(1) fib(0) fib(1) fib(0)

Fibonacci series How many function calls are performed if you call fib(5)? fib(5) = fib(4) + fib(3) fib(4) 9 function calls fib(3) 5 function calls => 14 function calls the number of function calls is exponential which is very inefficient O(2 n )

Fibonacci series Fibonacci can be written using a loop: int fib( int n ) { if ( n < 2 ) return n; else { int older = 0, old = 1, sum = 0, i = 2; while ( i <= n ) { sum = old + older; older = old; old = sum; i++; } return sum; } }

Example What does the function do? void rev() { char c; cin.get(c); if ( c == '\n' ) return; else { rev(); cout.put(c); } return; }

Tail Recursion Tail Recursion: recursive call is the last statement in the function Tail recursion can be easily written using a loop Example: factorial()

Non-Tail recursion The recursive call is not the last statement in the function Non-Tail recursion can be written using a loop but you have to use a stack example: reverse() Try writing reverse() using a loop

Nested Recursion The recursive function calls itself as a parameter Example: h(n)={ 0 n=0 n n>4 h(2+h(2n)) n 4 what is h(3)?

Recursion Why use recursion since you can use a loop? Some problems are recursive by definition Some problems are solved easily using recursion example: towers of Hanoi

Towers of Hanoi A B C

Towers of Hanoi Game where you have three towers (A, B, C) A number of disks of different sizes are stacked on tower A You have to move the disks from tower A to tower C Rules: move a single disk per move you cannot put a large disk on top of a smaller disk

Towers of Hanoi Take the case of 3 disks: 1) move 2 disks from A to B 2) move disk from A to C 3) move 2 disks from B to C

Towers of Hanoi Take the case of 4 disks: 1) move 3 disks from A to B 2) move disk from A to C 3) move 3 disks from B to C

Towers of Hanoi Take the case of n disks: 1) move (n-1) disks from A to B 2) move disk from A to C 3) move (n-1) disks from B to C

Towers of Hanoi Write a function that will solve this game?? parameters: n: number of disks src: the source tower dst: the destination tower tmp: the third tower

Towers of Hanoi if you have one disk only: move it from source to destination if you have n disks ( n > 1 ) 1) move (n-1) disks from source to tmp 2) move disk from source to destination 3) move (n-1) disks from tmp to destination

Towers of Hanoi void move(int n, char src, char dst, char tmp) { if ( n == 1 ) cout << "move from " << src << " to " << dst << endl; else { move(n 1, src, tmp, dst); move(1, src, dst, tmp); move(n 1, tmp, dst, src); } }

Towers of Hanoi A myth says that the world will end after you move a tower of 64 disks...?? 64 disks need 2^64 = 1.844674407 10¹ ⁹ moves Assuming 10 moves per second. you need. 58,494,241 century not in our lifetime :)