COSO 1030 Section 4 Recursion. What is about Towers of Hanoi Divide and Conquer Strategy Recursion and Induction Thinking Recursively Recursion Pitfalls.

Slides:



Advertisements
Similar presentations
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Advertisements

CSC 205 Programming II Lecture 10 Towers of Hanoi.
Recursion, Divide and Conquer Lam Chi Kit (George) HKOI2007.
Factorial Recursion stack Binary Search Towers of Hanoi
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)
Kavita Math231 Recursion and Iteration. Kavita Math231 We use Recursion when we have to perform a complex task that can be broken into the several subtasks.
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.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
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.
Recursion. 2 CMPS 12B, UC Santa Cruz Solving problems by recursion How can you solve a complex problem? Devise a complex solution Break the complex problem.
© 2006 Pearson Addison-Wesley. All rights reserved6-1 More on Recursion.
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.
Review of Recursion What is a Recursive Method? The need for Auxiliary (or Helper) Methods How Recursive Methods work Tracing of Recursive Methods.
Recursion. Recursive Solutions Recursion breaks a problem into smaller identical problems – mirror images so to speak. By continuing to do this, eventually.
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.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Unit 7 1 Unit 7: Recursion H Recursion is a fundamental programming technique that can provide an elegant solution to a certain kinds of problems H In.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 15 Recursive Algorithms.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1.
Recursion A method is recursive if it makes a call to itself. A method is recursive if it makes a call to itself. For example: For example: public void.
4.4 Recursive Algorithms A recursive algorithm is one which calls itself to solve “smaller” versions of an input problem. How it works: – The current status.
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.
Recursion Jordi Cortadella Department of Computer Science.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
Chapter 8 Recursion Modified.
Recursion Part 3 CS221 – 2/27/09. Recursion A function calls itself directly: Test() { … Test(); … }
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Informal Analysis of Merge Sort  suppose the running time (the number of operations) of merge sort is a function of the number of elements to sort  let.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
A Different Solution  alternatively we can use the following algorithm: 1. if n == 0 done, otherwise I. print the string once II. print the string (n.
15. MORE RECURSIONS Rocky K. C. Chang November 22, 2015.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Recursive. Recursive F(n) = F(n-1) + F(n-2) n! = (n-1)! x n C(m,n) = C(m-1,n-1)+C(m-1,n)......
Review of Recursion What is a Recursive Method?
Recursion CENG 707.
Analysis of Algorithms
Towers of Hanoi Move n (4) disks from pole A to pole C
Analysis of Algorithms
Recursion &Faster Sorting
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.
Jordi Cortadella Department of Computer Science
CS201: Data Structures and Discrete Mathematics I
Recursion.
Analysis of Algorithms
Review of Recursion What is a Recursive Method?
CS 2210 Discrete Structures Advanced Counting
Recursion: The Mirrors
Review of Recursion What is a Recursive Method?
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Recursion Chapter 12.
Recursive Thinking.
Analysis of Algorithms
Review of Recursion What is a Recursive Method?
Presentation transcript:

COSO 1030 Section 4 Recursion

What is about Towers of Hanoi Divide and Conquer Strategy Recursion and Induction Thinking Recursively Recursion Pitfalls Analyze Efficiency of Recursion Tail Recursion Elimination

Towers of Hanoi Move n disks from one peg to another with a help peg, can’t put a big disk on a small Mark pegs as source, destination and spare Or simply 1, 2 and 3 Print each move by saying “Move disk k from peg i to j”.

Divide and Conquer If there is only one disk, just move it. If we can move n-1 disks from source to spare, then we can move the biggest one from source to destination. And the problem size reduced by 1, we only need to move n-1 disks from spare to destination using source as helper

Pseudo Code (level 1) /** * Towers of Hanoi n int – n disks I int – source peg j int – destination peg k int – spare peg * print out each move */ Void hanoi(int n, int I, int j, int k)

{ /* divide */ if( n == 1) { print(“Move ” + n + “th disk from ”+ i + “ to ” + j); } else { // 1. move n-1 disks from i to k using j for help // 2. move nth disk from i to j // 3. move n-1 disks from k to j using i. } The sub problem 2. is easy to solve print(“Move ” + n + “th disk from ”+ i + “ to ” + j); To the 1 and 3, problem size is n-1

Conquer Solve Sub Problems Recursively To the 1: – Move n-1 disks from i to k using j – hanoi(n-1, i, k, j); To the 3: – Move n-1 disks from k to j using I – hanoi(n-1, k, j, I);

void hanoi(int n, int i, int j, int k) { /* merge */ if( n == 1) { print(“Move ” + n + “th disk from ”+ i + “ to ” + j); } else { // 1. move n-1 disks from i to k using j for help hanoi(n-1, i, k, j); // 2. move nth disk from i to j print(“Move ” + n + “th disk from ”+ i + “ to ” + j); // 3. move n-1 disks from k to j using i. hanoi(n-1, k, j, i); }

Recursion A function call it self directly or indirectly Recursive Function Termination – If it is defined on Natural numbers (N) A sequence with a minimal value – Base - the minimal value – N with 0 as base – Can’t recursively calculate the function on base – Any other well defined sequence can be mapped to a sub set of N

Recursion and Induction Inductive definition of String – String ::= EMPTY – String ::= String + CHAR Recursively counts space characters in a string int numOfSpaceCh(String string) { if(string == null) { return 0; // base } else { int n = numOfSpaceCh(string.substring(1)); // recursion return ((string.charAt(0) == ‘ ’) ? 1: 0) + n; // merge }

Think Recursively Divide into small problem(s) – Half and half – Head and tail – Random divide – Cut one at end Solve base directly Recursively solve sub problem(s) Merge the result

Recursion Pitfalls Infinite recursion – Not well ordered …, -n, -(n-1), …, -2, -1, 0 – hanoi(0, 1, 2, 3) Exponent complexity class – Hanoi(n) is O(2^n) – Fibonacci(n) could be O(2^n)

Analyze Recursion Efficiency Recursion Efficiency depends on – Number of recursive calls – Size reduced Hanoi(n) – Has 2 recursive calls – Size reduced by 1 – O(2*O(Hanoi(n-1))) – O(2^n)

Inductive Proof There exists K and n0 such that for any n >= n0, K 2^n <= Hanoi(n) There exists K’ and n0’ such that for any n >= n0’, Hanoi(n) <= K’ 2^n Let K = 1 and n0 = 2 Base n = n0: if; Hanoi(1); print; Hanoi(1); 2^2 = 4 = Steps of Hanoi(2) Use SofH(n) for Steps of Hanoi(n)

Induction Assume for any m = n - 1, m >= n0, 2^m <= SofH(m) SofH(m + 1) are no less than SofH(m) SofH(m). By assumption 2^m <= SofH(m), we get SofH(m+1)<= 2 * 2^m = 2 ^(m+1) Conclusion: for any n > n0 Steps of Hanoi(n) >= 2 ^ n.

O(numOfSpaceCh()) One recursive call Length reduced by 1 It is O(n) where n is the length of a string 1 * Steps of numOfSpaceCh(n-1)

Tail Recursion A recursive function is tail recursion, if – It has only one recursive call – And the call is the last statement of the function numOfSpaceCh is a tail recursive function Iteration version int n = 0; for (int I = 0; I < string.length; I ++) { if (string.charAt(I) == ‘ ’) n++; }; return n;

Tail Recursion Elimination Recursive Version F(n) { if (n == 0) base; else { m = non-recursive(n); F(m); // m < n } Iteration Version F(n) { base; while (n != 0) { n = non-recursive(n); }

Why Eliminate Recursion Efficiency – Tail Recursion O(n) Time, O(n) Stack Space – Iteration O(n) Time, O(1) Stack Space

Summary Recursion – A Powerful Tool Apply Divide and Conquer Strategy Inductive Definition, Proof and Recursive Computation How to Efficiently Divide Problems Analyze Computation Complexity Avoidable Pitfalls Eliminate Tail Recursion