Recursion!. Question Stephen King’s books J. R. R. Tolkien’s books Recursion Do they have something in common?

Slides:



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

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.
The Towers of Hanoi or Apocalypse When?.
CSC 205 Programming II Lecture 10 Towers of Hanoi.
The Algorithmic problems?
More Recursion: Permutations and Towers of Hanoi COP 3502.
AE1APS Algorithmic Problem Solving John Drake
COSC 2006 Data Structures I Recursion III
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)
Representational Choices The Towers of Hanoi Problem.
1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)
Recursion … just in case you didn’t love loops enough …
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L10 (Chapter 19) Recursion.
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.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Recursion.
Review of Recursion. Definition: Recursion - The process of a function calling itself.
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.
1 Section 6.1 Recurrence Relations. 2 Recursive definition of a sequence Specify one or more initial terms Specify rule for obtaining subsequent terms.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 16: Recursion.
6.Advanced Counting Techniques 1 Copyright M.R.K. Krishna Rao 2003 Ch 6. Recurrence Relations A recurrence relation for the sequence {a n } is an equation.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Recursion.
Introduction to Programming (in C++) Recursion Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Towers of Hanoi. Introduction This problem is discussed in many maths texts, And in computer science an AI as an illustration of recursion and problem.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 15 Recursion.
Solving Recurrence Lecture 19: Nov 25. Some Recursive Programming (Optional?)
Recursion Jordi Cortadella Department of Computer Science.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
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.
Recursion (part 1) HKUCS Provinci Training Chan Ho Leung.
Recursion. Hanoi Tower Legend: Inside a Vietnamese temple there are three rods (labeled as r 1, r 2, and r 3 ), surrounded by n golden disks of different.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Chapter 6.1: Recurrence Relations Discrete Mathematical Structures: Theory and Applications.
Recursive Algorithms A recursive algorithm calls itself to do part of its work The “call to itself” must be on a smaller problem than the one originally.
22C:19 Discrete Math Advanced Counting Fall 2010 Sukumar Ghosh.
Recursion Colin Capham Recursion – an introduction to the concept.
CS112 Scientific Computation Department of Computer Science Wellesley College Blowing a gasket Classic recursive examples.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 18 Recursion.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 15 Recursion.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
1 In this puzzle, the player begins with n disks of decreasing diameter placed one on top of the other on one of three pegs of the game board. The player.
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 Towers of Hanoi Three pegs, one with n disks of decreasing diameter; two other pegs are empty Task: move all disks to the third peg under the following.
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.
Hubert Chan (Chapters 1.6, 1.7, 4.1)
Chapter 18 Recursion CS1: Java Programming Colorado State University
Tower of Hanoi problem: Move the pile of rings from one peg to another
Recursion.
COMP 51 Week Fourteen Recursion.
Recursion Salim Arfaoui.
Hubert Chan (Chapters 1.6, 1.7, 4.1)
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
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
Recursion Chapter 11.
When Will The World End “Temple of Brahma” legend from the Indian city of Benares Priests must move 64 gold disks from one end of the temple to the other.
Tower of Hanoi problem: Move the pile of rings from one peg to another
The Hanoi Tower Problem
Recursion When performing a repetitive task either: a loop recursion
Recursion.
Tower of Hanoi problem: Move the pile of rings from one peg to another
Presentation transcript:

Recursion!

Question Stephen King’s books J. R. R. Tolkien’s books Recursion Do they have something in common?

Answer: J. R. R. Tolkien has written “Two towers” Stephen King has written Dark Tower books The Towers of Hanoi!

The Towers of Hanoi The Legend. In an ancient city in India, so the legend goes, monks in a temple have to move a pile of 64 sacred disks from one location to another. The disks are fragile; only one can be carried at a time. A disk may not be placed on top of a smaller, less valuable disk. And, there is only one other location in the temple (besides the original and destination locations) sacred enough that a pile of disks can be placed there. Source:

The Towers of Hanoi Source:

The Towers of Hanoi How should the monks proceed? Will they make it?

The Towers of Hanoi Recursive solution! 1.For N = 0 do nothing 2.Move the top N-1 disks from Src to Aux (using Dst as an intermediary peg) 3.Move the bottom disks from Src to Dst 4.Move N-1 disks from Aux to Dst (using Src as an intermediary peg) The first call: Solve(3, 1, 2, 3)

The Towers of Hanoi 1.Move from Src to Dst 2.Move from Src to Aux 3.Move from Dst to Aux 4.Move from Src to Dst 5.Move from Aux to Src 6.Move from Aux to Dst 7.Move from Src to Dst

The Towers of Hanoi How much time will monks spend?  For one disk, only one move is necessary For two disks, we need three moves For n disks???

The Towers of Hanoi Let T n denote the number of moves needed to move n disks. T 1 = 1 T 2 = 3 T n = 2*T n-1 + 1

The Towers of Hanoi Let T n denote the number of moves needed to move n disks. T 1 = 1 T 2 = 3 T n = 2*T n T n = 2 n - 1 How to prove it?

Induction! Base case: T 1 = 1 = OK! Inductive hypothesis: T n = 2 n -1 Inductive step: we show that: T n+1 =2 n T n+1 =2*T n +1=2*(2 n -1)+1= =2 n = 2 n QED!

Towers of Hanoi Suppose it takes one minute for a monk to move a disk. The whole task hence would take minutes = (2 10 ) 6 * minutes ≈ (10 3 ) 6 *15 minutes ≈ 25*10 16 hours ≈ days = days ≈ the age of universe

Recursion Sierpiński triangle Wacław Sierpiński – Polish mathematician

Sierpiński Triangle Draw a black triangle –Draw a white triangle in the middle of the triangle. –Call the procedure for three left black triangles

Tail recursion Consider the following method: int last(int n, int []arr) // returns the last element in arr // starting from n // -1 if n is too large { if (n > arr.length - 1) return –1; if (n == arr.length – 1) return arr[n]; return last(n+1, arr); }

Tail recursion The recursive function call is the last command in the method We can transform then recursion into iteration Some compilers can do it!

Tail recursion int last(int n, int []arr) // returns the last element in arr // starting from n { while (n < arr.length – 1) n++; if (n == arr.length – 1) return arr[n]; else return –1; }

Tail recursion The transformed program doesn’t use any extra memory The transformation can save space Exercise: Transform Towers of Hanoi.