Humorous Asides “A journey begins with single step”

Slides:



Advertisements
Similar presentations
Basics of Recursion Programming with Recursion
Advertisements

Introduction to Recursion and Recursive Algorithms
ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can: Design recursive algorithms to solve.
Recursion. Binary search example postponed to end of lecture.
More on Recursive Recursion vs. Iteration Why 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.
Recursion.
ICS103 Programming in C Lecture 11: Recursive Functions
Copyright © 2003 Pearson Education, Inc. Slide 1.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Recursion.
CSC 313 – Advanced Programming Topics. Why Recurse?  Recursion is useful, powerful technique  Often yields compact, easy-to-read code  Highlights all.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Humorous Asides “A journey begins with single step”
Question of the Day While walking across a bridge I saw a boat filled with people. Nobody boarded or left the boat, but on board the boat there was not.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
M180: Data Structures & Algorithms in Java
Lecture 10 Recursion CSE225: Data Structures. 2 A Look Back at Functions #include double distance(double x1, double y1, double x2, double y2) { double.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
CIS 068 Welcome to CIS 068 ! Stacks and Recursion.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
Hopefully this lesson will give you an inception of what recursion is.
Chapter 8 Recursion Modified.
CSC Programming for Science Lecture 16: Debugging.
CSIS 123A Lecture 9 Recursion Glenn Stevenson CSIS 113A MSJC.
CSE 501N Fall ‘09 12: Recursion and Recursive Algorithms 8 October 2009 Nick Leidenfrost.
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
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.
CSC 212 More Recursion, Stacks, & Queues. Linear Recursion Test for the bases cases  At least one base case needs to be defined If not at a base case,
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
LECTURE 20: RECURSION CSC 212 – Data Structures. Humorous Asides.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Recursion Chapter 11. How it works “A recursive computation solves a problem by using the solution of the same problem with simpler inputs” Big Java,
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
W1-1 University of Washington Computer Programming I Recursion © 2000 UW CSE.
Question of the Day  What three letter word completes the first word and starts the second one: DON???CAR.
COP INTERMEDIATE JAVA Recursion. The term “recursion” refers to the fact that the same computation recurs, or occurs repeatedly, as a problem is.
LECTURE 21: RECURSION & LINKED LIST REVIEW CSC 212 – Data Structures.
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Problem of the Day  On the next slide I wrote today’s problem of the day. It has 3 possible answers. Can you guess which 1 of the following is the solution?
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
Chapter 6 (Lafore’s Book) Recursion Hwajung Lee.  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:
Function Recursion to understand recursion you must understand recursion.
Recursion Powerful Tool
Sections 4.1 & 4.2 Recursive Definitions,
Identify the Appropriate Method for Handling Repetition
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
Java 4/4/2017 Recursion.
CSE 116/504 – Intro. To Computer Science for Majors II
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Chapter 14 Recursion. Chapter 14 Recursion Overview 14.1 Recursive Functions for Tasks 14.2 Recursive Functions for Values 14.3 Thinking Recursively.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Lesson #6 Modular Programming and Functions.
Recursion Chapter 11.
Functions Recursion CSCI 230
CS302 - Data Structures using C++
Lesson #6 Modular Programming and Functions.
Recursion Taken from notes by Dr. Neil Moore
Yan Shi CS/SE 2630 Lecture Notes
Recursion.
Recursive Thinking.
Recursive Function Prepared by Harprith ICT2102 Introduction to Data Structure.
Presentation transcript:

Humorous Asides

“A journey begins with single step”

 Large problems hard to solve  Thinking about & solving small problems easier  Splitting problems into smaller ones often helps  Before you start coding, plan each assignment  Break up large functions with many if s and loops  Move repeated action into small functions Solving Problems

Smaller is Better

CENSORED

Smaller is Better  (At least for programming) CENSORED

 Should be boring, easy, understandable drone  Given its parameters, perform the expected action  Only perform action defined for its parameters  Should not cure cancer  Do not worry about the larger problem  Solving entire problem is not this function’s issue  Split into tasks since solving whole problem hard Goal of a Function

 re-cur-sion  re-cur-sion: Method of solving problem by combining solutions to identical, smaller problems Recursion

 re-cur-sion  re-cur-sion: Method of solving problem by combining solutions to identical, smaller problems Recursion

 Recursive stepbase case(s)  Recursive step simplifies problem to base case(s)  Recast using slightly easier version in recursive step 4! = 4 * 3! See Recursion Work

 Recursive stepbase case(s)  Recursive step simplifies problem to base case(s)  Recast using slightly easier version in recursive step 4! = 4 * 3! = 4 * (3 * 2!) See Recursion Work

 Recursive stepbase case(s)  Recursive step simplifies problem to base case(s)  Recast using slightly easier version in recursive step 4! = 4 * 3! = 4 * (3 * 2!) = 4 * (3 * (2 * 1!)) See Recursion Work

 Recursive stepbase case(s)  Recursive step simplifies problem to base case(s)  Recast using slightly easier version in recursive step 4! = 4 * 3! = 4 * (3 * 2!) = 4 * (3 * (2 * 1!))  Base case(s) handle and solve obvious cases = 4 * (3 * (2 * 1)) See Recursion Work

 Recursive stepbase case(s)  Recursive step simplifies problem to base case(s)  Recast using slightly easier version in recursive step 4! = 4 * 3! = 4 * (3 * 2!) = 4 * (3 * (2 * 1!))  Base case(s) handle and solve obvious cases = 4 * (3 * (2 * 1))  After base case, combine solutions in recursive steps = 4 * (3 * 2) See Recursion Work

 Recursive stepbase case(s)  Recursive step simplifies problem to base case(s)  Recast using slightly easier version in recursive step 4! = 4 * 3! = 4 * (3 * 2!) = 4 * (3 * (2 * 1!))  Base case(s) handle and solve obvious cases = 4 * (3 * (2 * 1))  After base case, combine solutions in recursive steps = 4 * (3 * 2) = 4 * 6 See Recursion Work

 Recursive stepbase case(s)  Recursive step simplifies problem to base case(s)  Recast using slightly easier version in recursive step 4! = 4 * 3! = 4 * (3 * 2!) = 4 * (3 * (2 * 1!))  Base case(s) handle and solve obvious cases = 4 * (3 * (2 * 1))  After base case, combine solutions in recursive steps = 4 * (3 * 2) = 4 * 6 = 24 See Recursion Work

Very easy to create solution that does not work  Infinite recursion occurs if base case never reached  Frame-by-frame stack grows from function calls  Program crashes without warning or explanation For Recursion To Work

Very easy to create solution that does not work Recursive step must advance toward a base case  If there are multiple, which base case is unimportant one  Get one step closer to base case at each recursive call  Must check if algorithm works for all possible inputs For Recursion To Work

recursive  A function is recursive if it calls itself: int factorial(int i) { if (i <= 1) { return 1; } else { return i * factorial(i - 1); } } Recursion in C++

recursive  A function is recursive if it calls itself: int factorial(int i) { if (i <= 1) { return 1; } else { int nextI = i – 1; int result = factorial(nextI); return i * result; } } Recursion in C++ Base case: Solution is simple

recursive  A function is recursive if it calls itself: int factorial(int i) { if (i <= 1) { return 1; } else { int nextI = i – 1; int result = factorial(nextI); return i * result; } } Recursion in C++ Recursive Step: 1 step Take 1 step to solution

recursive  A function is recursive if it calls itself: int factorial(int i) { if (i <= 1) { return 1; } else { int nextI = i – 1; int result = factorial(nextI); return i * result; } } Recursion in C++ Recursive Step: 1 step Take 1 step to solution Make 1 or more recursive calls

recursive  A function is recursive if it calls itself: int factorial(int i) { if (i <= 1) { return 1; } else { int nextI = i – 1; int result = factorial(nextI); return i * result; } } Recursion in C++ Recursive Step: 1 step Take 1 step to solution Make 1 or more recursive calls Simple process computes result

 Start with check for base case(s)  These cases must return blatantly obvious answer  1+ recursive calls found within recursive step(s)  Write these assuming recursive call works 1  Take 1 step toward base case (not 2, 3, or 10482) Recursive Function Basics

 Start with check for base case(s)  These cases must return blatantly obvious answer  1+ recursive calls found within recursive step(s)  Write these assuming recursive call works 1  Take 1 step toward base case (not 2, 3, or 10482) Recursive Function Basics

 No different than usual tracing we were doing  When function called, we add frame for the call  Local variables & parameters shown in frame  (Processors also include line being executed) Tracing Recursion

 No different than usual tracing we were doing  When function called, we add frame for the call  Local variables & parameters shown in frame  (Processors also include line being executed) NOT Tracing Recursion

int findMin(int[] a, int len, int j) { if (j == len - 1) { return a[j]; } else { int minFollowing = findMin(a, len, j+1); if (a[j] < minFollowing) { return a[j]; } return minFollowing; } } int[] example1 = { 0 }; findMin(example1, 1,0); Trace This, Buddy!

int findMin(int[] a, int len, int j) { if (j == len - 1) { return a[j]; } else { int minFollowing = findMin(a,len,j+1); if (a[j] < minFollowing) { return a[j]; } return minFollowing; } } int[] example2 = { 2, 3, 0, 1 }; findMin(example2, 4, 0); Trace This, Buddy!

Your Turn  Get into your groups and complete activity

For Next Lecture  Programming Project #3  Programming Project #3 due Friday