2.3 Recursion do automated demo of towers of hanoi before class

Slides:



Advertisements
Similar presentations
3/25/2017 Chapter 16 Recursion.
Advertisements

Starting Out with Java: From Control Structures through Objects
Towers of Hanoi
2.3 Recursion do automated demo of towers of hanoi before class
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 14: Recursion by.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
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.
RECURSION Recursion Towers of Hanoi To Recurse or to Loop? Fractals Do something!
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion.
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.
© 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.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
# 1# 1 VBA Recursion What is the “base case”? What is the programming stack? CS 105 Spring 2010.
Chapter 8 Recursion Modified.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Data Structures & Algorithms Recursion and Trees Richard Newman.
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.
1 Recursion n what is it? n how to build recursive algorithms n recursion analysis n tracing simple recursive functions n hands on attempts at writing.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
UNIT 17 Recursion: Towers of Hanoi.
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
Tower of Hanoi Tower of Hanoi is a mathematical puzzle invented by a French Mathematician Edouard Lucas in The game starts by having few discs stacked.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
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 Chapter 10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Recursion.
Recursion.
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 19: Recursion.
Recursion The programs discussed so far have been structured as functions that invoke one another in a disciplined manner For some problems it is useful.
Recursion Topic 5.
Data Structures with Java © Rick Mercer
Chapter 15 Recursion.
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Recursion A problem solving technique where an algorithm is defined in terms of itself A recursive method is a method that calls itself A recursive algorithm.
Decrease-and-Conquer Approach
Chapter 15 Recursion.
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.
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Recursive Definitions
Chapter 12 Recursion (methods calling themselves)
Data Structures & Algorithms
Chapter 14: Recursion Starting Out with C++ Early Objects
Exam 2 Review 1.
Recursive GCD Demo public class Euclid {
The Hanoi Tower Problem
Basics of Recursion Programming with Recursion
Chapter 11 Recursion.
11 Recursion Software Solutions Lewis & Loftus java 5TH EDITION
CSC 143 Recursion.
Greatest Common Divisor.
Dr. Sampath Jayarathna Cal Poly Pomona
Recursion Chapter 12.
2.3 Recursion Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2010 · 2/17/11.
Recursive Thinking.
Presentation transcript:

2.3 Recursion do automated demo of towers of hanoi before class M. C. Escher, 1948

Overview What is recursion? When one function calls itself directly or indirectly. Why learn recursion? Occurs in nature New way of thinking about programming Powerful programming paradigm. Many computations are naturally self-referential. Mergesort, FFT, gcd. A folder contains files and other folders. Closely related to mathematical induction. 1) Ask for examples of recursive functions in mathematics M. C. Escher, 1948

Greatest Common Divisor Gcd. Find largest integer that evenly divides into p and q. Ex. gcd(4032, 1272) = 24. Applications. Simplify fractions: 1272/4032 = 53/168. RSA cryptosystem. 4032 = 26  32  71 1272 = 23  31  531 gcd = 23  31 = 24 How would you implement gcd?

Greatest Common Divisor Gcd. Find largest integer that evenly divides into p and q. Euclid's algorithm. [Euclid 300 BCE] base case reduction step, converges to base case gcd = one of world's oldest algorithms gcd(4032, 1272) = gcd(1272, 216) = gcd(216, 192) = gcd(192, 24) = gcd(24, 0) = 24. 4032 = 3  1272 + 216

Greatest Common Divisor Gcd. Find largest integer d that evenly divides into p and q. Java implementation. base case reduction step, converges to base case 1) What is your first impression of this code? public static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } base case reduction step

How-To’s on Writing Recursive Functions Base Case: You must check if we’ve reached the base case before doing another level of recursion! Make Progress Towards Base Case: Your recursive calls must be on a smaller or simpler input. Eventually this must reach the base case (and not miss it). Multiple recursive calls: Sometimes more than one recursive call. Sometimes not. Are their return values chosen, combined? Learn and follow these rules carefully! How would you implement gcd?

Recursive Graphics

Amazing hand-drawn image in NY Times by Serkan Ozkaya [Serkan Ozkaya] simply wanted to draw and see printed a faithful copy of all the type and pictures planned for a broadsheet page of this newspaper: this very page you are reading right now, which shows his version of the page you are reading right now, which shows his version of his version of the page you are reading right now, which...

H-tree Used in integrated circuits to distribute the clock signal. H-tree of order n. Draw an H. Recursively draw 4 H-trees of order n-1, one connected to each tip. and half the size tip Application: distribute clock signal to endpoints on integrated circuit. Want distance from clock to each endpoint to be the same. Otherwise clock skew. size order 1 order 2 order 3

Htree in Java Dr. Java Demo 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); } public static void main(String[] args) { int n = Integer.parseInt(args[0]); draw(n, .5, .5, .5); draw the H, centered on (x, y) What is the computational cost of running this program? recursively draw 4 half-size Hs

Computational Cost of the H-Tree Program H-tree of Order 1 First invocation of draw() calls itself 4 times H-tree of Order 2?

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. Edouard Lucas invented Towers of Hanoi puzzle (1883) Lucas died as the result of a freak accident at a banquet when a plate was dropped and a piece flew up and cut his cheek. He died of erysipelas (rare skin infection) a few days later. start finish Towers of Hanoi demo Edouard Lucas (1883)

Towers of Hanoi: Recursive Solution Move n-1 smallest discs right. Move largest disc left. cyclic wrap-around Consider the task of moving n discs from leftmost pole to rightmost pole. It is definitely the case that you need to transfer the biggest disc to the bottom of the rightmost pole, but you can't do that until all the remaining n-1 discs are removed from pole A. To get them out of the way, a good place to store them would be the middle pole. Once you have done this, you move the largest disc to pole C, and then transfer the rest of the discs from pole B to pole C. Move n-1 smallest discs right.