COSC 2006 Data Structures I Recursion III

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 Ellen Walker CPSC 201 Data Structures Hiram College.
Factorial Recursion stack Binary Search Towers of Hanoi
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.
Chapter 10 Recursion Instructor: alkar/demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe.
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.
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.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
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.
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.
© 2006 Pearson Addison-Wesley. All rights reserved3-1 Chapter 3 Recursion: The Mirrors.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
COMPSCI 105 S Principles of Computer Science Recursion 3.
Recursion Chapter 5.
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,
Chapter 14: Recursion Starting Out with C++ Early Objects
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
© 2006 Pearson Addison-Wesley. All rights reserved 3-1 Chapter 3 Recursion: The Mirrors.
Chapter 3 Recursion: The Mirrors. © 2004 Pearson Addison-Wesley. All rights reserved 3-2 Recursive Solutions Recursion –An extremely powerful problem-solving.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Chapter 15: Advanced Topics: Introducing Data Structures and Recursion Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
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.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 8 Recursion Modified.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
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.
Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.
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.
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.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Towers of Hanoi Move n (4) disks from pole A to pole B such that a larger disk is never put on a smaller disk A BC ABC.
1 Recursion. 2 A process by which a function calls itself repeatedly  Either directly. X calls X  Or cyclically in a chain. X calls Y, and Y calls X.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
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.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
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.
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.
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.
Recursion Powerful Tool
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 19: Recursion.
Chapter 15 Recursion.
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Towers of Hanoi Move n (4) disks from pole A to pole C
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.
Fundamentals of Programming
Recursion Data Structures.
CSC 143 Recursion.
Dr. Sampath Jayarathna Cal Poly Pomona
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Recursion: The Mirrors
Presentation transcript:

COSC 2006 Data Structures I Recursion III

kth smallest Can be solved by sorting, but this does more work than needed (why?) solving kth smallest for all k More efficient solution possible: Pick an element, any element partition the other elements into two sets: those smaller (S1) and those bigger (S2) If S1 has k-1 members, we got lucky! If S1 has at least k members, recurse to find the kth smallest in S1 Otherwise recurse to find the kth smallest in S2 …

kth smallest: Devil in the details What about this last case? We dispensed with |S1| + 1 elements! So we are looking for the ___th smallest element in S2 (k - (|S1| + 1))th Need to “pass” subarray with first, last So, |S1| = pivotIndex - first

kth smallest: partial code This motivates the details: kSmall(k, anArray, first, last) choose a pivotIndex if (k < pivotIndex - first + 1) return kSmall(k, anArray, first, pivotIndex-1) else if (k == pivotIndex - first + 1) return p else return kSmall(k - (pivotIndex-first+1), anArray, pivotIndex+1, last)

Problems with Recursion Memory (stack) exhaustion: if many stack frames are pushed - can run out of space for stack Time: each recursive call requires time to set up a new stack frame, copy in actual parameter values and transfer execution

Problems with Recursion Any recursive algorithm can be rewritten using iterative technique - it will be longer and more likely to have problems but will run faster.

Recursion Vs Iteration Note that just because we can use recursion to solve a problem, that does not mean this is the optimal solution. For instance, we usually would not use recursion to solve the sum of 1 to N problem, because the iterative version is easier to understand and requires less memory is N is a large number.

Recursion Vs Iteration The iterative solution is easier to understand: sum = 0; for(int number = 1; number <=N; number++) sum += number; Recursion has memory overhead. However, for some problems, recursion provides an elegant solution, often cleaner than an iterative version

Example: Towers of Hanoi For some problems, recursive solution is the only reasonable solution Given: N disks & three poles: A the source B the destination C the spare (temporary) The disks are of different sizes & have holes in the middle Only one disk can be removed at a time A larger disk can't be placed on a smaller one Initially all disks are on pole A Required: Move the disks, one-by-one, from pole(peg) A to B, using pole C as spare A B C

Example: Towers of Hanoi Solution: Only one disk (N=1, First base case): Move the disk from the source peg A to the destination peg B Only two disks (N=2, Second base case): Move the top disk from peg A to peg C Move the second disk from peg A to peg B Move the top disk from peg C to peg B

Example: Towers of Hanoi Solution: Three disks (N=3): Move top disk from peg A to peg B Move second disk from peg A to peg C Move top disk from peg B to peg C Move the remaining bottom disk from peg A to peg B Move the top disk from peg C to peg A Move second disk from peg C to peg B A C B

Towers of Hanoi 1- initial stage 2- move 1 disk from A to C using B 3- move 1 disk from A to B using C A C B A C B A C B 1 2 3

Towers of Hanoi - Stages C B A C B A C B 4 5 6 4- move 1 disk from C to B using A now have moved 2 disks from A to B using C 5- move 1 disk from A to C using B 6- move 1 disk from B to A using C 7- move 1 disk from B to C using A 8- move 1 disk from A to C using B completes moves of 2 disks from B to C using A and completes moves of 3 disks from A to C using B A C B A C B 7 8

Example: Towers of Hanoi Solution: General Solution (N-1) disks: Move the top (N-1) disks to Temp pole Move the bottom disk to the destination Move the N-1 disks from Temp to the destination

Example: Towers of Hanoi Solution: General Solution (N-1) disks: a) Initial state b) Move N-1 disks from A to C c) Move 1 disk from A to B d) Move N-1 disks from C to B A B C

Towers of Hanoi First let’s play with blocks … solveTowers(count, source, dest, spare) if (count is 1) { move a disk directly from source to dest else solveTowers(count-1, source, spare, dest) solveTowers(1, source, dest, spare) solveTowers(count-1, spare, dest, source)

Towers of Hanoi - Algorithm public static void solveTowers(int n, char source, char dest, char intermed) { if (n == 1) System.out.println("move a disk from " + source + " to " + dest); else solveTowers(n-1, source, intermed, dest); solveTowers(1, source, dest, intermed); solveTowers(n-1, intermed, dest, source); } original call: solveTowers ( 3, 'A', 'C', 'B'); 2 Trace: n = 3 S = A D = C I = B ret = 1 n = 2 n = 2 S = A S = B D = B D = C I = C I = A ret = 2 ret = 3 n = 1 n = 1 n = 1 n = 1 S = A S = C S = B S = A D = C D = B D = A D = C I = B I = A I = C I = B ret = 2 ret =3 ret =2 ret = 3 3 1

Example: Towers of Hanoi Observations Solution analog to mathematical induction If we can solve the problem for N-1 disks, then we can solve it for N disks In each move, the source, destination, and temporary poles change locations Three recursive calls inside the function

Review When you solve a problem by solving two or more smaller problems, each of the smaller problems must be ______ the base case than the original problem. closer to farther to either closer to or the same “distance” from either farther to or the same “distance” from

Review In a sorted array, the kth smallest item is given by ______. anArray[k-1] anArray[k] anArray[SIZE-k] anArray[SIZE+k]

Review A recursive solution that finds the factorial of n always reduces the problem size by ______ at each recursive call. 1 2 half one-third

Review In the recursive solution to the kth smallest item problem, the problem size decreases by ______ at each recursive call. 1 at least 1 half at least half

Review In the recursive solution to the Towers of Hanoi problem, the number of disks to move ______ at each recursive call. decreases by 1 increases by 1 decreases by half increases by half

Review A recursive solution that finds the factorial of n generates ______ recursive calls. n-1 n n+1 n*2

Review In the Fibonacci sequence, which of the following integers comes after the sequence 1, 1, 2, 3? 3 4 5 6