CS 112 Introduction to Programming

Similar documents
Department of Computer Science Yale University Office: 314 Watson Closely related to mathematical induction.

Overview. What is recursion? When one function calls itself directly or indirectly.

2.3 Recursion. Overview. Greatest Common Divisor. Greatest Common Divisor. What is recursion? When one function calls itself directly or indirectly.

2.3 Recursion. Overview. Greatest Common Divisor. Greatest Common Divisor. What is recursion? When one function calls itself directly or indirectly.

26 Feb Recursion. Overview. Greatest Common Divisor. Greatest Common Divisor. Greatest Common Divisor. Greatest Common Divisor

! New mode of thinking. ! Powerful programming paradigm. ! Mergesort, FFT, gcd. ! Linked data structures.

2.3 Recursion. Overview. Mathematical Induction. What is recursion? When one function calls itself directly or indirectly.

2.3 Recursion 7/21/2014 5:12:34 PM

Recursion LOGO STYLE GUIDE Schools within the University 19

2.3 Recursion 7/23/2015 3:06:35 PM

CMSC 150 LECTURE 7 RECURSION

COMPUTER SCIENCE. Computer Science. 6. Recursion. Computer Science. An Interdisciplinary Approach. Section 2.3.

More recursion, Divide and conquer

CS 112 Introduction to Programming

Lecture P6: Recursion

Recursion CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2011

Recursion. Overview. Mathematical induction. Hello recursion. Recursion. Example applications. Goal: Compute factorial N! = 1 * 2 * 3...

Recursion. Fundamentals of Computer Science

Recursive Definitions

CS 112 Introduction to Programming

CS 112 Introduction to Programming

1/16/12. CS 112 Introduction to Programming. A Foundation for Programming. (Spring 2012) Lecture #4: Built-in Types of Data. The Computer s View

Reduction & Recursion Overview

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

Recursion. Chapter 5

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 5. The Towers of Hanoi. Divide and Conquer

Summary. csci 210: Data Structures Recursion. Recursive algorithms. Recursion. Topics READING:

csci 210: Data Structures Recursion

2/15/12. CS 112 Introduction to Programming. Lecture #16: Modular Development & Decomposition. Stepwise Refinement. Zhong Shao

Recursion Chapter 17. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

q To develop recursive methods for recursive mathematical functions ( ).

q To develop recursive methods for recursive mathematical functions ( ).

CS 112 Introduction to Programming

Unit-2 Divide and conquer 2016

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

Recursion. Jordi Cortadella Department of Computer Science

1.3 Conditionals and Loops

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

Recursion Chapter 3.5

CS 171: Introduction to Computer Science II. Quicksort

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

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

CS/CE 2336 Computer Science II

Chapter 10: Recursive Problem Solving

Chapter 15: Recursion

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

Recursion. Thinking Recursively. Tracing the Recursive Definition of List. CMPT 126: Lecture 10. Recursion

Odds and Ends. Think about doing research Programming grading. Assignment 3 due Tuesday

CSE 214 Computer Science II Recursion

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

APCS-AB: Java. Recursion in Java December 12, week14 1

Recursion Chapter 8. What is recursion? How can a function call itself? How can a function call itself?

Homework Assignment #3. 1 (5 pts) Demonstrate how mergesort works when sorting the following list of numbers:

Recap: Assignment as an Operator CS 112 Introduction to Programming

Combinatorial Search. permutations backtracking counting subsets paths in a graph. Overview

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

Dynamic Programming. Introduction, Weighted Interval Scheduling, Knapsack. Tyler Moore. Lecture 15/16

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

Recursion Chapter 8. What is recursion? How can a function call itself? How can a function call itself? contains a reference to itself.

Lecture 3: Loops. While Loops. While Loops: Powers of Two. While Loops: Newton-Raphson Method

2.4 Modular Programming

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

CS 112 Introduction to Programming

1.1 Your First Program

CSC 8301 Design and Analysis of Algorithms: Recursive Analysis

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

1.3 Conditionals and Loops

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

Lecture 24 Tao Wang 1

1.1 Your First Program

UNIT 5A Recursion: Basics. Recursion

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

Solutions to Problem 1 of Homework 3 (10 (+6) Points)

Reading 8 : Recursion

6.001 Notes: Section 4.1

BBM 101. Introduction to Programming I. Lecture #06 Recursion

Lecture 3: Loops. While Loops. While Loops: Newton-Raphson Method. While Loops: Powers of Two

Divide-and-Conquer. Combine solutions to sub-problems into overall solution. Break up problem of size n into two equal parts of size!n.

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

Recursion. Dr. Jürgen Eckerle FS Recursive Functions

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

1.3 Conditionals and Loops

1.3 Conditionals and Loops. 1.3 Conditionals and Loops. Conditionals and Loops

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

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

CS/COE 1501 cs.pitt.edu/~bill/1501/ More Math

Recursion. Chapter 7

This Week. Trapezoidal Rule, theory. Warmup Example: Numeric Integration. Trapezoidal Rule, pseudocode. Trapezoidal Rule, implementation

Sorting. Task Description. Selection Sort. Should we worry about speed?

input sort left half sort right half merge results Mergesort overview

University of the Western Cape Department of Computer Science

Running Time. Analytic Engine. Charles Babbage (1864) how many times do you have to turn the crank?

Unit 5: Recursive Thinking

26 How Many Times Must a White Dove Sail...

Lecture Notes 4 More C++ and recursion CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

UNIT 5A Recursion: Basics. Recursion

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

ENGG1811 Computing for Engineers Week 10 Recursion, External Application Interfacing

Recursive Algorithms II

Transcription:

CS 112 Introduction to Programming (Spring 2012) Lecture #13: Recursion Zhong Shao Department of Computer Science Yale University Office: 314 Watson http://flint.cs.yale.edu/cs112 Acknowledgements: some slides used in this class are taken directly or adapted from those accompanying the textbook: Introduction to Programming in Java: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne (Copyright 2002-2010)

Overview What is recursion? When one function calls itself directly or indirectly. Why learn recursion? New mode of thinking. Powerful programming paradigm. Many computations are naturally self-referential. Mergesort, FFT, gcd, depth-first search. Linked data structures. A folder contains files and other folders. Closely related to mathematical induction. Reproductive Parts M. C. Escher, 1948 2

Greatest Common Divisor Gcd. Find largest integer that evenly divides into p and q. Ex. gcd(4032, 1272) = 24. 4032 = 2 6 3 2 7 1 1272 = 2 3 3 1 53 1 gcd = 2 3 3 1 = 24 Applications. Simplify fractions: 1272/4032 = 53/168. RSA cryptosystem. 3

Greatest Common Divisor Gcd. Find largest integer d that evenly divides into p and q. Euclid's algorithm. [Euclid 300 BCE] " gcd(p, q) = # p if q = 0 $ gcd(q, p % q) otherwise base case reduction step, converges to base case gcd(4032, 1272) = gcd(1272, 216) = gcd(216, 192) = gcd(192, 24) = gcd(24, 0) = 24. 4032 = 3 1272 + 216 4

Greatest Common Divisor Gcd. Find largest integer d that evenly divides into p and q. " gcd(p, q) = # p if q = 0 $ gcd(q, p % q) otherwise base case reduction step, converges to base case p q q p % q x x x x x x x x p = 8x q = 3x gcd(p, q) = x gcd 5

Greatest Common Divisor Gcd. Find largest integer d that evenly divides into p and q. " gcd(p, q) = # p if q = 0 $ gcd(q, p % q) otherwise base case reduction step, converges to base case Java implementation. public static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } base case reduction step 6

Recursive Graphics

8

9

Htree H-tree of order n. Draw an H. and half the size Recursively draw 4 H-trees of order n-1, one connected to each tip. tip size order 1 order 2 order 3 10

Htree in Java public class Htree { public static void draw(int n, double sz, double x, double y) { if (n == 0) return; double x0 = x - sz/2, x1 = x + sz/2; double y0 = y - sz/2, y1 = y + sz/2; } StdDraw.line(x0, y, x1, y); StdDraw.line(x0, y0, x0, y1); StdDraw.line(x1, y0, x1, y1); draw(n-1, sz/2, x0, y0); draw(n-1, sz/2, x0, y1); draw(n-1, sz/2, x1, y0); draw(n-1, sz/2, x1, y1); draw the H, centered on (x, y) recursively draw 4 half-size Hs } public static void main(string[] args) { int n = Integer.parseInt(args[0]); draw(n,.5,.5,.5); } 11

Animated H-tree Animated H-tree. Pause for 1 second after drawing each H. 20% 40% 60% 80% 100% 12

Towers of Hanoi http://en.wikipedia.org/wiki/image:hanoiklein.jpg

Towers of Hanoi Move all the discs from the leftmost peg to the rightmost one. Only one disc may be moved at a time. A disc can be placed either on empty peg or on top of a larger disc. start finish Towers of Hanoi demo Edouard Lucas (1883) 14

Towers of Hanoi Legend Q. Is world going to end (according to legend)? 64 golden discs on 3 diamond pegs. World ends when certain group of monks accomplish task. Q. Will computer algorithms help? 15

Towers of Hanoi: Recursive Solution Move n-1 smallest discs right. Move largest disc left. cyclic wrap-around Move n-1 smallest discs right. 16

Towers of Hanoi: Recursive Solution public class TowersOfHanoi { } public static void moves(int n, boolean left) { if (n == 0) return; moves(n-1,!left); if (left) System.out.println(n + " left"); else System.out.println(n + " right"); moves(n-1,!left); } public static void main(string[] args) { int N = Integer.parseInt(args[0]); moves(n, true); } moves(n, true) : move discs 1 to n one pole to the left moves(n, false): move discs 1 to n one pole to the right smallest disc 17

Towers of Hanoi: Recursive Solution % java TowersOfHanoi 3 1 left 2 right 1 left 3 left 1 left 2 right 1 left every other move is smallest disc % java TowersOfHanoi 4 1 right 2 left 1 right 3 right 1 right 2 left 1 right 4 left 1 right 2 left 1 right 3 right 1 right 2 left 1 right subdivisions of ruler 18

Towers of Hanoi: Recursion Tree n, left 3, true 1 14 15 28 2, false 2, false 2 7 8 13 16 21 22 27 1, true 1, true 1, true 1, true 3 4 5 6 9 10 11 12 17 18 19 20 23 24 25 26 1 left 2 right 1 left 3 left 1 left 2 right 1 left 19

Towers of Hanoi: Properties of Solution Remarkable properties of recursive solution. Takes 2 n - 1 moves to solve n disc problem. Sequence of discs is same as subdivisions of ruler. Every other move involves smallest disc. Recursive algorithm yields non-recursive solution! Alternate between two moves: move smallest disc to right if n is even make only legal move not involving smallest disc to left if n is odd Recursive algorithm may reveal fate of world. Takes 585 billion years for n = 64 (at rate of 1 disc per second). Reassuring fact: any solution takes at least this long! 20

Divide-and-Conquer Divide-and-conquer paradigm. Break up problem into smaller subproblems of same structure. Solve subproblems recursively using same method. Combine results to produce solution to original problem. Divide et impera. Veni, vidi, vici. - Julius Caesar Many important problems succumb to divide-and-conquer. FFT for signal processing. Parsers for programming languages. Multigrid methods for solving PDEs. Quicksort and mergesort for sorting. Hilbert curve for domain decomposition. Quad-tree for efficient N-body simulation. Midpoint displacement method for fractional Brownian motion. 21

Fibonacci Numbers

Fibonacci Numbers Fibonacci numbers. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, F(n) = # 0 if n = 0 % $ 1 if n =1 % & F(n "1) + F(n " 2) otherwise L. P. Fibonacci (1170-1250) Fibonacci rabbits 23

Fibonacci Numbers and Nature Fibonacci numbers. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, #0 if n = 0 % F(n) = $ 1 if n = 1 % F(n "1) + F(n " 2) otherwise &! pinecone cauliflower 24

A Possible Pitfall With Recursion Fibonacci numbers. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, F(n) = # 0 if n = 0 % $ 1 if n =1 % & F(n "1) + F(n " 2) otherwise A natural for recursion? public static long F(int n) { if (n == 0) return 0; if (n == 1) return 1; return F(n-1) + F(n-2); } 25

Recursion Challenge 1 (difficult but important) Q. Is this an efficient way to compute F(50)? public static long F(int n) { if (n == 0) return 0; if (n == 1) return 1; return F(n-1) + F(n-2); } A. No, no, no! This code is spectacularly inefficient. F(50) F(49) F(48) F(48) F(47) F(47) F(46) F(47) F(46) F(46) F(45) F(46) F(45) F(45) F(44) recursion tree for naïve Fibonacci function F(50) is called once. F(49) is called once. F(48) is called 2 times. F(47) is called 3 times. F(46) is called 5 times. F(45) is called 8 times.... F(1) is called 12,586,269,025 times. F(50) 26

Recursion Challenge 2 (easy and also important) Q. Is this a more efficient way to compute F(50)? public static long F(int n) { long[] F = new long[n+1]; F[0] = 0; F[1] = 1; for(int i = 2; i <= n; i++) F[i] = F[i-1] + F[i-2]; return F[n]; } FYI: classic math F(n) = " n # (1#") n 5 = $ " n 5 % φ = golden ratio 1.618 A. Yes. This code does it with 50 additions. Lesson. Don t use recursion to engage in exponential waste. Context. This is a special case of an important programming technique known as dynamic programming (stay tuned). 27

Summary How to write simple recursive programs? Base case, reduction step. Trace the execution of a recursive program. Use pictures. Why learn recursion? New mode of thinking. Powerful programming tool. Towers of Hanoi by W. A. Schloss. Divide-and-conquer. Elegant solution to many important problems. 28

Extra Slides

Collatz Sequence Collatz sequence. If n is 1, stop. If n is even, divide by 2. If n is odd, multiply by 3 and add 1. Ex. 35 106 53 160 80 40 20 10 5 16 8 4 2 1. public static void collatz(int n) {! System.out.print(n + " ");! if (n == 1) return;! if (n % 2 == 0) collatz(n / 2);! collatz(3*n + 1);! } 30

Fractional Brownian Motion

Fractional Brownian Motion Physical process which models many natural and artificial phenomenon. Price of stocks. Dispersion of ink flowing in water. Rugged shapes of mountains and clouds. Fractal landscapes and textures for computer graphics. 32

Simulating Brownian Motion Midpoint displacement method. Maintain an interval with endpoints (x 0, y 0 ) and (x 1, y 1 ). Divide the interval in half. Choose δ at random from Gaussian distribution. Set x m = (x 0 + x 1 )/2 and y m = (y 0 + y 1 )/2 + δ. Recur on the left and right intervals. 33

Simulating Brownian Motion: Java Implementation Midpoint displacement method. Maintain an interval with endpoints (x 0, y 0 ) and (x 1, y 1 ). Divide the interval in half. Choose δ at random from Gaussian distribution. Set x m = (x 0 + x 1 )/2 and y m = (y 0 + y 1 )/2 + δ. Recur on the left and right intervals. public static void curve(double x0, double y0, double x1, double y1, double var) { if (x1 - x0 < 0.01) { StdDraw.line(x0, y0, x1, y1); return; } double xm = (x0 + x1) / 2; double ym = (y0 + y1) / 2; ym += StdRandom.gaussian(0, Math.sqrt(var)); } curve(x0, y0, xm, ym, var/2); curve(xm, ym, x1, y1, var/2); variance halves at each level; change factor to get different shapes 34

Plasma Cloud Plasma cloud centered at (x, y) of size s. Each corner labeled with some grayscale value. Divide square into four quadrants. The grayscale of each new corner is the average of others. center: average of the four corners + random displacement others: average of two original corners Recur on the four quadrants. c 1 +c 2 c 1 2 c 2 c 1 +c 3 2 c 2 +c 4 2 (c 1 +c 2 +c 3 +c 4 ) 4 + " c 3 c 3 +c 4 c 4 2 35

Plasma Cloud 36

Brownian Landscape Reference: http://www.geocities.com/aaron_torpy/gallery.htm 37