Question of the Day  What three letter word completes the first word and starts the second one: DON???CAR.

Slides:



Advertisements
Similar presentations
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Advertisements

CSC 213 – Large Scale Programming or. Today’s Goals  Begin by discussing basic approach of quick sort  Divide-and-conquer used, but how does this help?
Chapter 10 Introduction to Arrays
HST 952 Computing for Biomedical Scientists Lecture 9.
Analysis And Algorithms CMSC 201. Search Sometimes, we use the location of a piece of information in a list to store information. If I have the list [4,
CSC 213 – Large Scale Programming. Today’s Goals  Look at how Dictionary s used in real world  Where this would occur & why they are used there  In.
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
Lecture 3: Topics If-then-else Operator precedence While loops Static methods 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.
Chapter 19 Recursion.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
29-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 3: Arrays, Linked Lists, and Recursion
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Recursion James Wright St Peter’s Secondary School 733 Parkhill Road West Peterborough,Ontario K9J-8M4
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Humorous Asides “A journey begins with single step”
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
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.
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.
COSC 2006 Data Structures I Recursion II
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
Humorous Asides “A journey begins with single step”
CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Announcements  If you need more review of Java…  I have lots of good resources – talk to me  Use “Additional Help” link on webpage  Weekly assignments.
LECTURE 34: MAPS & HASH CSC 212 – Data Structures.
CSC 107 – Programming For Science. The Week’s Goal.
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.
Problem of the Day  I am thinking of a question and propose 3 possible answers. Exactly one of the following is the solution. Which is it? A. Answer 1.
LECTURE 35: COLLISIONS CSC 212 – Data Structures.
Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
LECTURE 20: RECURSION CSC 212 – Data Structures. Humorous Asides.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
CSC 213 – Large Scale Programming. Today’s Goal  Understand why testing code is important  Result of poor or no testing & embarrassment caused  Learn.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
LECTURE 21: RECURSION & LINKED LIST REVIEW CSC 212 – Data Structures.
Question of the Day  What three letter word completes the first word and starts the second one: DON???CAR.
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
Recursion COMP x1 Sedgewick Chapter 5. Recursive Functions problems can sometimes be expressed in terms of a simpler instance of the same problem.
Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)
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?
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
CSE 143 Lecture 9: introduction to recursion reading: 12.1.
CSC 212 – Data Structures Lecture 28: More Hash and Dictionaries.
Recursion Powerful Tool
Sorts, CompareTo Method and Strings
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Introduction To Repetition The for loop
CSE 116/504 – Intro. To Computer Science for Majors II
Announcements Final Exam on August 17th Wednesday at 16:00.
Recursion 12-Nov-18.
Recursion 2-Dec-18.
Recursion 2-Dec-18.
Linked Lists.
Recursion 29-Dec-18.
Searching, Sorting, and Asymptotic Complexity
CS302 - Data Structures using C++
Searching.
Presentation transcript:

Question of the Day  What three letter word completes the first word and starts the second one: DON???CAR

Question of the Day  What three letter word completes the first word and starts the second one: DON???CAR  Key  Key completes the first word (donkey) and a key starts a car

 re-cur-sion  re-cur-sion: Solving problems by breaking into identical, smaller problems and combining solutions after base case(s) reached. Recursion

 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

 No different than usual coding had been doing  At some point, recursive method calls itself  Parameters & locals in frame & so specific to each call  Once complete, execution returns to calling frame NOT Writing Recursive Code

Starting Recursive Solution  Identify when solution easy with little/no work  Checking for situation involves one or two operations  Simple return statement with literal or no value base case(s)  May find more than one of these base case(s) If you cannot determine, stop immediately That are many problems without recursive solution

Base Case(s) Example Base Case(s)  Index leaves array with 0 or 1 entries  If specific item searched for in array, stop at 0 entries base case  1 entry used as base case when each item will be used  At last Node or null reference in Linked List base case(s)  Just like with arrays, base case(s) are comparable  int reaches 0 if working linearly through data base case  If multiplying int each pass, base case at 1  0 or 1 characters left in a String

Recursive Step Starting Recursive Step base case(s)  Determine state preceding each of base case(s)  These cases should be almost as easy to solve base case(s)  One step away from reaching base case(s) base case  Stand-alone base case may not have prior state base case  Identify single step advancing toward base case  Coding much easier when same step used in all cases  Know when each is used if different steps needed

Consider All Possible Inputs  After defining recursive call solution will be using  Must check that it works for all possible inputs  For each input you examine, must consider  Check that recursive call moves toward base case recursive steps  No conflicting recursive steps that cycle within space

Consider All Possible Inputs  After defining recursive call solution will be using  Must check that it works for all possible inputs  For each input you examine, must consider  Check that recursive call moves toward base case recursive steps  No conflicting recursive steps that cycle within space

Next to Last Step  Examine how method combines recursive result  No result for some recursive solutions, so can skip this  Some other recursive solutions just return result  Others require even more work to be performed  When using recursion, fairly simple actions required  If many or complex actions required, then…  First ask yourself: is this solution actually recursive?  Could it be more recursive & simpler is next question

 Do not want to be locked into recursive solution  Recursion often adds parameter to the signature  Public method only has required parameters  Linked list instance or array to be processed  User is interested in testing some value  Use private method to hold the extra variable  Value or array index where method currently working  Node in linked list to be processed by method  Range or indices where method starts & stops Last Step

public static int findMin(int[] a, int j) { if (j == a.length - 1) { return a[j]; } else { int minFollowing = findMin(a, j+1); return Math.min(a[j], minFollowing); } } Check This Out

public static int findMin(int[] a, int j) { if (j == a.length - 1) { return a[j]; } else { int minFollowing = findMin(a, j+1); return Math.min(a[j], minFollowing); } } public static int findMin(int[] a) { } Check It Out

private static int findMin(int[] a, int j) { if (j == a.length - 1) { return a[j]; } else { int minFollowing = findMin(a, j+1); return Math.min(a[j], minFollowing); } } public static int findMin(int[] a) { return findMin(a, 0); } Check It Out

Your Turn  Get into your groups and complete activity

For Next Lecture  Read GT for class on Friday  Is all code really equal?  How can we figure out which is faster?  Week #7 weekly assignment available now  Angel also has programming assignment #1  Pulls everything together and shows off your stuff NO!