RECURSION COMP 103 Thomas Kuehne 2016-T2 Lecture 16

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.
Starting Out with Java: From Control Structures through Objects
Factorial Recursion stack Binary Search Towers of Hanoi
Recursive methods. Recursion A recursive method is a method that contains a call to itself Often used as an alternative to iteration when iteration is.
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.
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.
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.
Lecture 8 Analysis of Recursive Algorithms King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
COMPSCI 105 S Principles of Computer Science Recursion 3.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
Data Structures and Abstractions with Java, 4e Frank Carrano
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Recursion COMP 102 #32.
Sudeshna Sarkar, IIT Kharagpur 1 Functions : Recursion Lecture
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArraySet and Binary Search.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
FASTER SORTING using RECURSION : QUICKSORT 2014-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
An introduction to costs (continued), and Binary Search 2013-T2 Lecture 11 School of Engineering and Computer Science, Victoria University of Wellington.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 103 #
QUICKSORT 2015-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
2015-T2 Lecture 21 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
2014-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Recursion COMP 102 #30.
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
2014-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Recursion COMP x1 Sedgewick Chapter 5. Recursive Functions problems can sometimes be expressed in terms of a simpler instance of the same problem.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
COMP 103 Binary Search Trees II Marcus Frean 2014-T2 Lecture 26
Chapter Topics Chapter 16 discusses the following main topics:
Recursion Salim Arfaoui.
Recursion CENG 707.
Chapter 15 Recursion.
COMP 103 RECURSION.
COMP 103 Linked Structures Marcus Frean 2014-T2 Lecture 17
Towers of Hanoi Move n (4) disks from pole A to pole C
Chapter 15 Recursion.
FASTER SORT using RECURSION : MERGE SORT
COMP 103 Tree Traversals Lindsay Groves 2016-T2 Lecture 22
More complexity analysis & Binary Search
Depth-first vs breadth-first traversals
Introduction to Computer Science - Alice
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Announcements Final Exam on August 17th Wednesday at 16:00.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Calculate n! Multiply my number by the factorial of the rest of the numbers. if( num > 2 ) return num * factorialR2( num - 1 ); else return 2;
CSE 1342 Programming Concepts
Announcements Final Exam on August 19th Saturday at 16:00.
Announcements Final Exam on December 25th Monday at 16:00.
Recursion.
The structure of programming
Thinking procedurally
Recursion Chapter 12.
Presentation transcript:

RECURSION COMP 103 Thomas Kuehne 2016-T2 Lecture 16 Marcus Frean Thomas Kuehne School of Engineering and Computer Science, Victoria University of Wellington 2016-T2 Lecture 16

RECAP-TODAY RECAP TODAY Recursion on recursive data structures 2 RECAP Recursion on recursive data structures TODAY Recursion with numbers to understand recursion, you must first understand recursion …

Recursion Methods that call themselves 3 Methods that call themselves not only works for recursive data structurs no different from calling another method public void drawBoxes(int c, int b , int width, int height) { if (height < 10) return; UI.fillRect(c - width/2, b - height, width, height); drawBoxes(c, b, width - 5, height - 5); } Base case When does it not call itself Recursive call Each call has its own set of arguments and local variables.

Another example 4 /** Draws a stream of bubbles from (x,y) to the top of the screen */ public void drawBubbles(int x, int y) { if ( y>0 ) { paint1Bubble(x, y); drawBubbles(x, y-15); } y=0 y=70 x=30 drawBubbles (30, 40); paint1Bubble(30,40); drawBubbles (30, 55); paint1Bubble(30,55); drawBubbles (30, 70); paint1Bubble(30,70);

Tracing the calls drawBubbles(30, 70) drawBubbles(30, 55) public void drawBubbles(int x, int y) { if ( y>0 ) { paint1Bubble(x, y); drawBubbles(x, y-15); } return drawBubbles(30, 70) paint1Bubble(30, 70) drawBubbles(30, 55) return y=0 paint1Bubble(30, 55) drawBubbles(30, 40) return paint1Bubble(30, 40) drawBubbles(30, 25) return paint1Bubble(30, 25) drawBubbles(30, 10) return paint1Bubble(30, 10) drawBubbles(30, -5) return

Recursion with wrapper method 6 /** Draws a stream of bubbles from (x,y) to the top of the screen the bubbles should get larger as they go up. */ public void drawBubblesRecurse(int x, int y, int size) { if ( y>0 ) { UI.fillOval(x, y, size, size); drawBubblesRecurse(x, y-size-6, size+2); } public void drawBubbles(int x, int y) { UI.setColor(Color.blue); drawBubblesRecurse(x, y, 10); Condition Do First Do the Rest Arguments for the Rest Initial size

Tail recursion vs nested recursion. 7 Tail recursion: recursive call is the last step in the method can be easily replaced with iteration Nested recursion: actions follow the recursive call recursion has to “unroll” in reverse order Example: Print an “onion” : ( ( ( ( ( ( ( ) ) ) ) ) ) ) public void onion (int layers) { UI.print( “(“ ); if (layers > 1) onion(layers-1); UI.print( “)“ ); } open do the inside layers close

Nested Recursion at work 8 onion(4) ⇒ public void onion (int layers) { UI.print( “(“ ); if (layers > 1) onion(layers-1); UI.print( “)“ ); ( ( ( ( ) ) ) ) public void onion (int layers) { UI.print( “(“ ); if (layers > 1) onion(layers-1); UI.print( “)“ ); } 4 . public void onion (int layers) { UI.print( “(“ ); if (layers > 1) onion(layers-1); UI.print( “)“ ); } 3 . ✔ public void onion (int layers) { UI.print( “(“ ); if (layers > 1) onion(layers-1); UI.print( “)“ ); } 2 . ✔ ✔ public void onion (int layers) { UI.print( “(“ ); if (layers > 1) onion(layers-1); UI.print( “)“ ); } 1 . ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔

Alternative Visualisation 9 onion(4) ✔ ✔ print( “(” ) print( “)” ) onion(3) ✔ ✔ ✔ print( “(” ) print( “)” ) ✔ onion(2) ✔ print( “)” ) ✔ print( “(” ) onion(1) ✔ ✔ ✔ print( “(” ) print( “)” ) ✔ Drawing diagrams can help with understanding recursive methods

Recursive Arch-Wall Draw a recursive arch-wall: 10 Draw a recursive arch-wall: Consists of an arch with two half size arch-walls on top of it.

Recursive Arch-Wall Algorithm 11 To draw an ArchWall of given base size (width, height): draw a base arch of size (width, height) if width is not too small draw a half size archwall on the left draw a half size archwall on the right public void archWall (int x, int y, int width, int height) { drawArch(x, y, width, height); if ( width > 20 ) { int w = width / 2; // width of next smaller arch int h = height / 2; // height of next smaller arch archWall(x, y-h, w, h); // left half archWall(x+w, y-h, w, h); // right half }

Tracing the execution aw(10, 300, 80, 40) aw(50,280,40,20) 12 aw(10, 300, 80, 40) aw(50,280,40,20) drawArch aw(10, 280, 40, 20) drawArch aw(10,270,20,10) aw(30,270,20,10) drawArch aw(50,270,20,10) aw(70,270,20,10) drawArch drawArch drawArch drawArch

Recursively finding the maximum 14 1 2 3 4 5 6 7 8 9 29 10 11 12 13 30 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 31 public double findMax(double[] data, int start , int end) { if ( end - start < 1 ) return data[start]; else { int mid = ( start + end) / 2; double leftMax = findMax(data, start, mid); double rightMax = findMax(data, mid+1, end); return Math.max(leftMax, rightMax); } Mid-Split is silly here. Might as well just step one by one. However, shows how you could find a maximum in a binary tree & would be quicker if you could run the methods in parallel

Recursive Binary Search 15 private int findIndexOf(Comparable<E> item, int low, int high) { if (low > high) return low; // nothing left to search in int mid = (low + high) / 2; // calculate best guess int c = item.compareTo(data[mid]); if (c == 0) return mid; // found the item! if (c < 0) return findIndexOf(item, low, mid-1); // must be in lower half else return findIndexOf(item, mid+1, high); // must be in upper half } Same as the iterative binary search, but easier to design.

Iterative Binary Search 16 private int findIndexOf(Comparable<E> item, int low, int high) { while (low <= high) { int mid  =  (low + high) / 2; // calculate best guess int c = item.compareTo(data[mid]); if (c == 0) return mid; // found the item!     if (c > 0) low = mid + 1;          // item should be in [mid+1..high]     else   high = mid - 1;        // item should be in [low..mid-1] } return low;   // return insertion position

Towers of Hanoi Move all disks from the first to the middle pole 17 http://towersofhanoi.info/Animate.aspx Move all disks from the first to the middle pole only move one disk at a time only disks at the very top can be moved never put a larger disk on a smaller disk There are monks in Hanoi doing this with 64 disks when they are finished, the world will end

Towers of Hanoi 1 3 2 void hanoi(int n, int from, int to ) { 18 1 3 2 http://towersofhanoi.info/Animate.aspx void hanoi(int n, int from, int to ) { if there is one disk only move (1) "from" => "to" else { determine third pole "other" hanoi (n-1) "from" => "other" (1) move (1) "from" => "to" (2) hanoi (n-1) "other" => "to" (3) }