Recursion The Information Company Operators know The first of the month is a Tuesday How to subtract one (e.g., 5-1=4) How calculate the next day of the.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

More on Recursion More techniques 1. Binary search algorithm Binary searching for a key in an array is similar to looking for a word in dictionary Take.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Recursion.
Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate.
ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can: Design recursive algorithms to solve.
Recursion Chapter 7. Spring 2010CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn how.
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.
© 2006 Pearson Addison-Wesley. All rights reserved7 B-1 Chapter 7 (continued) Stacks.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Unit 191 Recursion General Algorithm for Recursion When to use and not use Recursion Recursion Removal Examples Comparison of the Iterative and Recursive.
Fall 2007CS 2251 Recursion Chapter 7. Fall 2007CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Recursion.
Recursion Chapter 7.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 8: Recursion Data Structures.
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.
Fundamental in Computer Science Recursive algorithms 1.
Recursion.  Identify recursive algorithms  Write simple recursive algorithms  Understand recursive function calling  With reference to the call stack.
Discrete Maths Objective to show the close connection between recursive definitions and recursive functions , Semester 2, Recursion.
Recursion Chapter 5.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Some Notes on Recursion Data.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
1 Decrease-and-Conquer Approach Lecture 06 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Chapter 15 Recursion INTRODUCTION Recursion is a program-solving technique that expresses the solution of a problem in terms of the solutions of.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
Principles of Programming Chapter 11: Recursive Function  In this chapter, you will learn about  Recursion function 1 NI S1 2009/10.
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 ,
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.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Functions-Recall 1. 2 Parameter passing & return void main() { int x=10, y=5; printf (“M1: x = %d, y = %d\n”, x, y); interchange (x, y); printf (“M2:
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.
Chapter 6 B Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 B-2 Comparing Implementations All of the three implementations are ultimately.
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.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
Chapter 6 Recursion. Solving simple problems Iteration can be replaced by a recursive function Recursion is the process of a function calling itself.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
7. RECURSIONS Rocky K. C. Chang October 12, 2015.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
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! =
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Data Structures I (CPCS-204) Week # 5: Recursion Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
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.
Recursion CENG 707.
Recursion Version 1.0.
Recursion Topic 5.
Recursion what is it? how to build recursive algorithms
Java Software Structures: John Lewis & Joseph Chase
Discrete Maths 11. Recursion Objective
Recursion.
adapted from Recursive Backtracking by Mike Scott, UT Austin
Recursion.
Presentation transcript:

Recursion The Information Company Operators know The first of the month is a Tuesday How to subtract one (e.g., 5-1=4) How calculate the next day of the week (e.g., given Sunday return Monday) Q: what day of the week is X? A: If X == “first” Then Reply “Tuesday” Else Call the Information Company Ask “What day of the week is X-1?” Take response, reply with next day of week

Outline Recursion Recursive call of function Base (non-recursive, degenerate) case Recursive case Tracing program stack, activation record Recursive order effects Math Induction and Recursion Backtracking and multiple solution paths Examples Factorial, Power, Maze search

Recursion Solving a problem P by solving problem P’, where P’ is simpler than P AND identical in nature to P versus Iteration - iteration generally involves loops and builds UP a solution Recursion in a program - where a function calls itself as part of its solution (simple recursion) OR where a function P calls a function or functions that will eventually call P

Binary Search: A Dictionary IF the “dictionary” contains one page THEN scan the page for the word ELSE open the dictionary to the middle determine which half of dictionary the word appears in IF word appears in first half THEN search a dictionary consisting of first half of dictionary ELSE search a dictionary consisting of second half of dictionary

Divide-and-Conquer Approach Solve problem by “dividing” problem into one (or more) easier to solve pieces Base (degenerate) case - version of problem that is so easy to solve no further recursion needed The first day of the month is Tuesday Finding a word in a one-page dictionary Recursive case - other than the Base case where problem must be “divided” Day of the month for any day but the first Finding a word in a dictionary with more than one page

Recursive Binary Search search(Dictionary,word) if (length of Dictionary == 1) then search page for word else open dictionary to middle determine which half contains word if (word in first half) then search(first half of dictionary,word) else search(second half of dictionary,word)

Recursively Defined Function Factorial of n fact(n) returns n! fact(0) - 0! = 1 fact(1) - 1! = 1 fact(2) - 2! = 2 fact(3) - 3! = 6 fact(4) - 4! = 24 fact(n) = n <= 1, return 1 n > 1, return n * fact(n-1) Factorial code: int fact(int n) { if (n <= 1) return 1; else return n * fact(n-1); } How does this work?

Tracing Recursive Functions (Box Method) Computer allocates space for variables on a “stack” When function called, computer puts “activation record” (AR) on stack with space for variables in function Activation record contains: –indication of where function called from (used at function end) –space for value parameters –space for reference parameters –space for local variables –space for the value returned by the function (if any) When function ends, its AR is removed from top of stack and computer returns to point function called from

A Recursive Function 1: int fact(int n) { 2: if (n <= 1) 3: return 1; 4: else 5: return n * fact(n-1); 6: } 7: void main() { 8: int x, fx; 9: x = 4; 10: fx = fact(x); 11: printf(“%d! = %d\n”,x,fx); 12: }

Tracing Factorial

Tracing Factorial (cont)

Recursive Power Function y Function powr(x,y) returns x where x,y ints p1: int powr(int x, int y) { p2: if (y == 0) p3: return 1; p4: else p5: return x * powr(x,y-1); p6: } call to power: k: x = powr(4,3);

Tracing Power Function

Mathematical Induction and Recursion Two are closely related Math induction often used to prove the correctness of recursive algorithms 1: int fact(int n) { 2: if (n <= 1) 3: return 1; 4: else 5: return n * fact(n-1); 6: } Prove call to fact(n) returns n!

Proof of Fact Function Base case: n is 1 since n is 1, line 3 executed, fact returns 1 (1!) Inductive case: n > 1 Assume: fact(j-1) returns (j-1)! for j > 1 Prove: fact(j) returns j! Call of fact(j) sets n to value of j, since j > 1, therefore n > 1, thus line 5 is executed, fact returns n * fact(n-1). Since n is j, fact returns j * fact(j-1). We know from inductive assumption that fact(j-1) returns (j-1)! Therefore fact returns j * (j-1)! which equals j!

Calculation and Recursive Order Some recursive calculations done on the “way in,” some on the “way out” int fct(int n, int a) { if (n <= 1) return a; else return fct(n - 1, n * a); } Call to fct: x = 4; fx = fct(x,1); Or add second function int fact(int n) { return fct(n,1); }

Tracing Fct

Tracing Fct (cont)

Recursive Order: Writing Array void do_print_arrayF(int A[], int J, int N) { if (J < N) { printf(“ %d”,A[J]); do_print_arrayF(A,J+1,N); } void print_arrayF(int A[], int N) { do_print_arrayF(A,0,N); printf(“\n”); } Call: print_arrayF(A,5); Output: A

Recursive Order: Writing Array void do_print_arrayR(int A[], int J, int N) { if (J < N) { do_print_arrayR(A,J+1,N); printf(“ %d”,A[J]); } void print_arrayR(int A[], int N) { do_print_arrayR(A,0,N); printf(“\n”); } Call: print_arrayR(A,5); Output: A

Recursive Order: Writing Array void do_print_arrayB(int A[], int J, int N) { if (J < N) { printf(“ %d”,A[J]); do_print_arrayB(A,J+1,N); printf(“ %d”,A[J]); } void print_arrayB(int A[], int N) { do_print_arrayB(A,0,N); printf(“\n”); } Call: print_arrayB(A,5); Output: A

Recursion: Backtracking A recursive method may have more than one alternative method for solving a problem One approach: try one method and see how far you get; if that fails, backtrack to the point where you made the choice and try a different method Built into recursive solution

Navigating a Maze Allowed to move from an open space to an adjacent open space (N,E,S,W) Question: is there a path from to ?

A Search Algorithm Visit(position) mark Position as Visited if (Position == ) then Found It! else if Position to East Open and Unvisited then Visit(Position to East) if Position to South Open and Unvisited then Visit(Position to South) if Position to North Open and Unvisited then Visit(Position to North) if Position to West Open and Unvisited then Visit(Position to West)

Tracing Visit Visit Visit Visit Visit Visit Visit Visit Visit Visit Visit Visit Visit Visit Visit Visit Visit Visit Visit Backtrack! Backtrack! Backtrack! New direction!

Tracing Visit (cont) Visit Visit Visit Visit Visit Visit Visit Visit Visit Visit Visit Visit Visit Visit Visit Visit Visit Visit Visit and so on

Issues in Visit Order points visited: … Representation issues: –maze as an array of characters –space characters for open positions –X character for block How to represent Visited? –drop period (.) (bread-crumb) when position Visited

Code for Visit int LegalPos(int r, int c) { if ((r 5)||(c > 5)) return 0; return 1; } void Visit(char Maze[][6], int r, int c) { Maze[r,c] = ‘.’; printf(“Visiting \n”,r,c); if ((r == 5) && (c == 5)) printf(“Found It!\n”); else { if (LegalPos(r,c+1) && (Maze[r,c+1] == ‘ ‘)) Visit(Maze,r,c+1); if (LegalPos(r+1,c) && (Maze[r+1,c] == ‘ ‘)) Visit(Maze,r+1,c); if (LegalPos(r-1,c) && (Maze[r-1,c] == ‘ ‘)) Visit(Maze,r-1,c); if (LegalPos(r,c-1) && (Maze[r,c-1] == ‘ ‘)) Visit(Maze,r,c-1); }