Lecture Objectives  Learn how to think recursively  Trace a recursive method  Prove a recursive method is correct  Write recursive algorithms and methods.

Slides:



Advertisements
Similar presentations
Numbers Treasure Hunt Following each question, click on the answer. If correct, the next page will load with a graphic first – these can be used to check.
Advertisements

1 A B C
Simplifications of Context-Free Grammars
Variations of the Turing Machine
TK1924 Program Design & Problem Solving Session 2011/2012
AP STUDY SESSION 2.
1
Copyright © 2003 Pearson Education, Inc. Slide 1.
David Burdett May 11, 2004 Package Binding for WS CDL.
Create an Application Title 1Y - Youth Chapter 5.
CALENDAR.
The 5S numbers game..
Break Time Remaining 10:00.
Factoring Quadratics — ax² + bx + c Topic
EE, NCKU Tien-Hao Chang (Darby Chang)
Turing Machines.
PP Test Review Sections 6-1 to 6-6
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Chapter 10: Applications of Arrays and the class vector
11 Data Structures Foundations of Computer Science ã Cengage Learning.
Abstract Data Types and Algorithms
Topic 14 Searching and Simple Sorts "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The.
Data structure is concerned with the various ways that data files can be organized and assembled. The structures of data files will strongly influence.
Digital Lessons on Factoring
1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.
Lilian Blot Recursion Autumn 2012 TPOP 1. Lilian Blot Recursion Autumn 2012 TPOP 2.
1 Hash Tables Saurav Karmakar. 2 Motivation What are the dictionary operations? What are the dictionary operations? (1) Insert (1) Insert (2) Delete (2)
Briana B. Morrison Adapted from William Collins
Bellwork Do the following problem on a ½ sheet of paper and turn in.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
Chapter 1: Expressions, Equations, & Inequalities
Lilian Blot PART III: ITERATIONS Core Elements Autumn 2012 TPOP 1.
Adding Up In Chunks.
MaK_Full ahead loaded 1 Alarm Page Directory (F11)
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt Synthetic.
Artificial Intelligence
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 11: Priority Queues and Heaps Java Software Structures: Designing.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 7: Recursion Java Software Structures: Designing and Using.
: 3 00.
5 minutes.
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
1 Let’s Recapitulate. 2 Regular Languages DFAs NFAs Regular Expressions Regular Grammars.
Types of selection structures
Converting a Fraction to %
Starting Out with Java: From Control Structures through Objects
Clock will move after 1 minute
Topic 16 Sorting Using ADTs to Implement Sorting Algorithms.
Physics for Scientists & Engineers, 3rd Edition
Chapter 13 Recursion, Complexity, and Searching and Sorting
Select a time to count down from the clock above
Copyright Tim Morris/St Stephen's School
1 Decidability continued…. 2 Theorem: For a recursively enumerable language it is undecidable to determine whether is finite Proof: We will reduce the.
FIGURE 3-1 Basic parts of a computer. Dale R. Patrick Electricity and Electronics: A Survey, 5e Copyright ©2002 by Pearson Education, Inc. Upper Saddle.
ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can: Design recursive algorithms to solve.
Recursion Chapter 5.
Recursion Chapter 7. Spring 2010CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn how.
Fall 2007CS 2251 Recursion Chapter 7. Fall 2007CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method.
Recursion Chapter 7.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 5.
RECURSION Chapter 5. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Recursion Topic 5.
Recursion Chapter 5.
7.2 Recursive Definitions of Mathematical Formulas
7.2 Recursive Definitions of Mathematical Formulas
Presentation transcript:

Lecture Objectives  Learn how to think recursively  Trace a recursive method  Prove a recursive method is correct  Write recursive algorithms and methods for searching arrays CS340 1

Recursion  Recursion can solve many programming problems that are difficult to solve linearly  In artificial intelligence, recursion is used to write programs that  play games of chess  prove mathematical theorems  recognize patterns  Recursive algorithms can  compute factorials  compute a greatest common divisor  process data structures (strings, arrays, linked lists, etc.)  search efficiently using a binary search  find a path through a maze, and more CS340 2

Recursive Thinking CS340 3

Recursive Thinking Recursion reduces a problem into one or more simpler versions of itself CS340 4

Recursive Thinking (cont.) CS340 5

Recursive Thinking (cont.) Recursion of Process Nested Dreams A child couldn't sleep, so her mother told a story about a little frog, who couldn't sleep, so the frog's mother told a story about a little bear, who couldn't sleep, so bear's mother told a story about a little weasel...who fell asleep....and the little bear fell asleep;...and the little frog fell asleep;...and the child fell asleep. CS340 6

Recursive Thinking (cont.) Consider searching for a target value in an array With elements sorted in increasing order Compare the target to the middle element If the middle element does not match the target search either the elements before the middle element or the elements after the middle element Instead of searching n elements, we search n/2 elements CS340 7

Recursive Thinking (cont.) Recursive Algorithm to Search an Array if the array is empty return -1 as the search result else if the middle element matches the target return the subscript of the middle element as the result else if the target is less than the middle element recursively search the array elements before the middle element and return the result else recursively search the array elements after the middle element and return the result CS340 8

Steps to Design a Recursive Algorithm  Base case:  for a small value of n, it can be solved directly  Recursive case(s)  Smaller versions of the same problem  Algorithmic steps:  Identify the base case and provide a solution to it  Reduce the problem to smaller versions of itself  Move towards the base case using smaller versions CS340 9

Recursive Algorithm for Finding the Length of a String if the string is empty (has no characters) the length is 0 else the length is 1 plus the length of the string that excludes the first character CS340 10

Recursive Algorithm for Finding the Length of a String (cont.) /** Recursive method str The The length of the string */ public static int length(String str) { if (str == null || str.equals("")) return 0; else return 1 + length(str.substring(1)); } CS340 11

Proving that a Recursive Method is Correct  Proof by induction  Prove the theorem base case is true  Assuming that the theorem is true for n, prove it is true for n+1  Recursive proof is similar to induction. Verify that  The base case is recognized and solved correctly  Each recursive case makes progress towards the base case  If all smaller problems are solved correctly, then the original problem also is solved correctly CS340 12

Tracing a Recursive Method Unwinding the recursion is the process of returning from recursive calls computing the partial results CS340 13

Run-Time Stack and Activation Frames  Java maintains a run-time stack  Activation frames save information in the run-time stack  The activation frame contains storage for  method arguments  local variables (if any)  the return address of the instruction that called the method  Whenever a new method is called (recursive or not), Java pushes a new activation frame into the run-time stack CS340 14

Run-Time Stack and Activation Frames (cont.) CS340 15

Run-Time Stack and Activation Frames CS340 16

Recursive Algorithm for Printing String Characters in Reverse /** Recursive method printCharsReverse post: The argument string is displayed in reverse, one character per str The string */ public static void printCharsReverse(String str) { if (str == null || str.equals("")) return; else { printCharsReverse(str.substring(1)); System.out.println(str.charAt(0)); } CS340 17

Recursive Algorithm for Finding Duplicate characters /** Recursive method removeDuplicates post: The word without word The string */ public String removeDuplicates(String word) { if(word == null || word.length() <= 1) return word; else if( word.charAt(0) == word.charAt(1) ) return removeDuplicates(word.substring(1, word.length())); else return word.charAt(0) + removeDuplicates(word.substring(1, word.length())); } CS340 18

Recursive Definitions of Mathematical Formulas CS340 19

Recursive Definitions of Mathematical Formulas Mathematicians often use recursive definitions of formulas Examples include: factorials powers greatest common divisors (gcd) CS340 20

Factorial of n: n! The factorial of n, or n! is defined as follows: 0! = 1 n! = n x (n -1)! (n > 0) The base case: n equal to 0 The second formula is a recursive definition CS340 21

Factorial of n: n! (cont.)  The recursive definition can be expressed by the following algorithm: if n equals 0 n! is 1 else n! = n x (n – 1)!  The last step can be implemented as: return n * factorial(n – 1); CS340 22

Factorial of n: n! (cont.) public static int factorial(int n) { if (n == 0) return 1; else return n * factorial(n – 1); } CS340 23

Infinite Recursion and Stack Overflow  Call factorial with a negative argument, what will happen? CS StackOverflowException

Recursive Algorithm for Calculating x n CS Eclipse example

Recursive Algorithm for Calculating gcd The greatest common divisor (gcd) of two numbers is the largest integer that divides both numbers The gcd of 20 and 15 is 5 The gcd of 36 and 24 is 12 The gcd of 38 and 18 is 2 CS340 26

Recursive Algorithm for Calculating gcd (cont.) Given 2 positive integers m and n (m > n) if n is a divisor of m gcd(m, n) = n else gcd (m, n) = gcd (n, m % n) CS340 27

Recursive Algorithm for Calculating gcd (cont.) CS /** Recursive gcd method (in RecursiveMethods.java). pre: m > 0 and n > m The larger n The smaller Greatest common divisor of m and n */ public static double gcd(int m, int n) { if (m % n == 0) return n; else if (m < n) return gcd(n, m); // Transpose arguments. else return gcd(n, m % n); }

Recursion Versus Iteration  Similarities between recursion and iteration  Iteration  Loop repetition condition determines whether to repeat the loop body or  exit from the loop  Recursion  The condition usually tests for a base case  You can always write an iterative solution to a recursion  But can you always write a recursion for an iteration problem?  Which one is best? CS340 29

Recursion Versus Iteration Difference between iteration and recursion is: with iteration, each step clearly leads onto the next, like stepping stones across a river, in recursion, each step replicates itself at a smaller scale, so that all of them combined together eventually solve the problem. CS340 30

Iterative factorial Method CS /** Iterative factorial method. pre: n >= n The integer whose factorial is being n! */ public static int factorialIter(int n) { int result = 1; for (int k = 1; k <= n; k++) result = result * k; return result; }

Efficiency of Recursion Recursive methods often have slower execution times Why? What about memory and recursion vs iteration? Why would we ever prefer recursion? CS340 32

Fibonacci Numbers Fibonacci numbers were invented to model the growth of a rabbit colony fib 1 = 1 fib 2 = 1 fib n = fib n-1 + fib n-2 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, … CS340 33

An Exponential Recursive fibonacci Method CS340 34

Efficiency of Recursion: Exponential fibonacci CS Inefficient

An O(n) Recursive fibonacci Method CS340 36

An O(n) Recursive fibonacci Method (cont.)  Non-recursive wrapper method: /** Wrapper method for calculating Fibonacci numbers (in RecursiveMethods.java). pre: n >= n The position of the desired Fibonacci The value of the nth Fibonacci number */ public static int fibonacciStart(int n) { return fibo(1, 0, n); } CS340 37

Efficiency of Recursion: O(n) fibonacci CS Efficient

Efficiency of Recursion: O(n) fibonacci Method fibo is an example of tail recursion or last-line recursion Why is it more effective? CS340 39

Recursive Array Search CS340 40

Recursive Array Search Simplest way to search is a linear search Start with the first element Examine one element at a time End with the last (n + 1)/2 elements examined in a linear search If the target is not in the list, n elements are examined What is the complexity? CS340 41

Recursive Array Search (cont.) Base cases for recursive search: Empty array, target can not be found; result is -1 First element of the array being searched = target? Yes? Then result is the subscript of first element No? Then recursive step searches the rest of the array, excluding the first element CS340 42

Algorithm for Recursive Linear Array Search CS if the array is empty the result is –1 else if the first element matches the target the result is the subscript of the first element else search the array excluding the first element and return the result

Implementation of Recursive Linear Search CS340 44

Implementation of Recursive Linear Search (cont.)  A non-recursive wrapper method: /** Wrapper for recursive linear search items The array being target The object being searched The subscript of target if found; otherwise -1 */ public static int linearSearch(Object[] items, Object target) { return linearSearch(items, target, 0); } CS340 45

Implementation of Recursive Linear Search (cont.) CS340 46

Design of a Binary Search Algorithm  Array has to be sorted  Base cases  The array is empty  Element being examined matches the target  Compares the middle element for a match with the target  Excludes the half of the array within which the target cannot lie CS340 47

Binary Search Algorithm (cont.) CS if the array is empty return –1 as the search result else if the middle element matches the target return the subscript of the middle element as the result else if the target is less than the middle element recursively search the array elements before the middle element and return the result else recursively search the array elements after the middle element and return the result

Binary Search Algorithm CS ChinaDenmarkEnglandGreeceJapanRussiaUSA England target first = 0last = 6middle = 3 First call

Binary Search Algorithm (cont.) CS ChinaDenmarkEnglandGreeceJapanRussiaUSA England target first = 0last = 2 middle = 1 Second call

Binary Search Algorithm (cont.) CS ChinaDenmarkEnglandGreeceJapanRussiaUSA England target first= middle = last = 2 Third call

Efficiency of Binary Search  An array of 16 would search arrays of length 16, 8, 4, 2, and 1; 5 probes in the worst case  16 = 2 4  5 = log  A doubled array size would only require 6 probes in the worst case  32 = 2 5  6 = log  An array with 32,768 elements requires only 16 probes! (log = 15) 52

Comparable Interface Comparable interface classes must define a compareTo method Method obj1.compareTo(obj2) returns an integer negative: obj1 < obj2 zero: obj1 == obj2 positive: obj1 > obj2 CS340 53

Implementation of a Binary Search Algorithm 54

Implementation of a Binary Search Algorithm (cont.) CS340 55

CS Trace of Binary Search

Testing Binary Search  You should test arrays with  an even number of elements  an odd number of elements  duplicate elements  Test each array for the following cases:  the target is the element at each position of the array  the target is less than the smallest array element  the target is greater than the largest array element  the target is a value between each pair of items in the array CS340 57

Method Arrays.binarySearch Java API class Arrays contains a binarySearch method If the objects in the array are not comparable or if the array is not sorted, the results are undefined If there are multiple copies of the target value in the array, there is no guarantee which one will be found Throws ClassCastException if the target is not comparable to the array elements CS340 58