Recursion1. 2 What kind of problem is “recursive”? same Recursive problems are those problems where in order to solve the original problem you have to.

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

Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
Back to Sorting – More efficient sorting algorithms.
CS 112 Introduction to Programming Sorting of an Array Debayan Gupta Computer Science Department Yale University 308A Watson, Phone:
Recursion Ellen Walker CPSC 201 Data Structures Hiram College.
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)
Faster Sorting Methods Chapter 9. 2 Chapter Contents Merge Sort Merging Arrays Recursive Merge Sort The Efficiency of Merge Sort Merge Sort in the Java.
1 Sorting Algorithms (Part II) Overview  Divide and Conquer Sorting Methods.  Merge Sort and its Implementation.  Brief Analysis of Merge Sort.  Quick.
Chapter 15 Recursive Algorithms.
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.
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.
COMPSCI 105 S Principles of Computer Science Recursion 3.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 15 Recursive Algorithms.
1 Divide and Conquer Binary Search Mergesort Recurrence Relations CSE Lecture 4 – Algorithms II.
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion.
Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Recursion: The Mirrors Chapter 2.
Recursion: The Mirrors Chapter 2 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
Recursion.
Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 1.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
COSC 2006 Data Structures I Recursion II
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
Recursion Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time.
Computer Science Searching & Sorting.
Chapter 8 Recursion Modified.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
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.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
Sudeshna Sarkar, IIT Kharagpur 1 Functions : Recursion Lecture
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
Big-O and Sorting February 6, Administrative Stuff Readings for today: Ch Readings for tomorrow: Ch 8.
1 Recursion n what is it? n how to build recursive algorithms n recursion analysis n tracing simple recursive functions n hands on attempts at writing.
CompSci Review of Recursion with Big-Oh  Recursion is an indispensable tool in a programmer’s toolkit  Allows many complex problems to be solved.
Contest Algorithms January 2016 Pseudo-code for divide and conquer, and three examples (binary exponentiation, binary search, and mergesort). 5. Divide.
© 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 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Recursion. What is Recursion? Method of solving problems Alternative to iteration All recursive solutions can be implemented iteratively Characteristic...
1 CS1120: Recursion. 2 What is Recursion? A method is said to be recursive if the method definition contains a call to itself A recursive method is a.
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Lecture 25: Searching and Sorting
Recursion what is it? how to build recursive algorithms
Chapter 15 Recursion.
Recursion A problem solving technique where an algorithm is defined in terms of itself A recursive method is a method that calls itself A recursive algorithm.
Recursion notes Chapter 8.
Towers of Hanoi Move n (4) disks from pole A to pole C
Chapter 15 Recursion.
Java 4/4/2017 Recursion.
Recursion Chapter 10.
Announcements Final Exam on August 17th Wednesday at 16:00.
CS1120: Recursion.
Announcements Final Exam on August 19th Saturday at 16:00.
Announcements Final Exam on December 25th Monday at 16:00.
CSE 373 Data Structures and Algorithms
Recursion Chapter 12.
Presentation transcript:

Recursion1

2 What kind of problem is “recursive”? same Recursive problems are those problems where in order to solve the original problem you have to solve one or more identical but smaller version(s) of the same problem. Examples: Binary Search Finding n th Fibonacci number Finding Factorials Finding exponents TOH Finding your way out of maze 8-queens problem Graph traversal problems … i.e., To solve problem of “size n”, first solve the same problem of “size n-1 or less”. How are each of these recursive problems?

Recursion3 First understanding a method and method call: For a method, the method call defines the problem size: … method(int k){ //method implementation //solves problem of size k } Solve a problem of “size n”- method(n) Solve a problem of “size n-1”- method(n-1) Solve a problem of “size n/2”- method(n/2) Solve a problem of “size 1”- method(1) Implicit sizes Solve a problem over an array of n elements - method(int[] array, int left, int right);

Recursion4 x n/2 * x n/2, if n is even x n/2 * x n/2 * x, if n is odd Recursive problems and methods: Problem:Find the exponent x n First First solve x n-1 To find: x n x n = x n-1 * x 1 … x 2 = x 1 * x 1 x 1 = x 0 * x 1 x 0 = 1 Recursive solution 1:Recursive solution 2: x n = Recursive problems are those problems where in order to solve the original problem you have to solve one or more identical but smaller version(s) of the problem until you reach the trivial or base case. To find: x n First First solve x n/2 … x 2 = x 1 * x 1 x 1 = x 0 * x 0 * x 1 x 0 = 1 Trivial or base case

Recursion5 Recursive problems and methods: Problem: Corresponding method signature: Find the exponent x n /** * purpose of this method is to calculate x^n x - the base n - the exponent, n >= 0 x^n */ public static double power(double x, int n) Usage: Solve 5^3- power(5,3); Solve 0.031^9- power(0.031,9); Solve ^0- power( ,0); Solve x^n- power(x,n) Solve x^(n-1)- power(x,n-1)

Recursion6 Recursive problems and methods: Connecting the problem and the code x n = x n-1 * x 1 The problemThe method signature /** * purpose of this method is to calculate x^n x - the base n - the exponent, n >= 0 x^n */ public static double power(double x, int n) Solve x n = power(x,n) = power(x,n-1) * x +

Recursion7 Recursive problems and methods: Problem: Corresponding method: Find the exponent x n Recursive solution: /** * purpose of this method is to calculate x^n x - the base n - the exponent, n >= 0 x^n */ public static double power(double x, int n) Solve x^(n-1) Solve x^n x n = x n-1 * x 1 … x 1 = x 0 * x 1 x 2 = x 1 * x 1 x 0 = 1 { if (n == 0)return 1; Base case always first! Recursion next double ans = power(x,n-1) * x; return ans; }

Recursion8 Recursive problems and methods: Trace the following call: … System.out.println(power(5,4));...

Recursion9 Recursive problems and methods: Problem: Corresponding method: Find the exponent x n Recursive solution: /** * purpose of this method is to calculate x^n x - the base n - the exponent, n >= 0 x^n */ public static double power(double x, int n) double xn; double xno2 = power(x,n/2); if (n%2 == 0)xn = xno2 * xno2; else xn = xno2 * xno2 *x; return xn; } Solve x n/2 Solve x^n { if (n == 0)return 1; x n/2 * x n/2, if n is even x n/2 * x n/2 * x, if n is odd x n = … x 2 = x 1 * x 1 x 1 = x 0 * x 0 * x 1 x 0 = 1

Recursion10 Recursion - Can you do this? Find x! as follows: 0! = 1 x! = x*(x-1)! /** * purpose of this method is to calculate n! n - the value whose factorial we have to find n! */ public static double factorial(int n)

Recursion11 Recursion - another example: Problem: f(n) = 1, when n is 0 or 1 f(n) = f(n-1) + f(n-2) when n > 1 Base case Recursive case /** * Finds the nth Fibonacci number n - the index of the Fibonacci number to find, n >= 0 nth Fibonacci number */ public static int fibonacci(int n){ if ((n==0) || (n==1)) return 1; //apparently, n is not 0 or 1 return fibonacci(n-1) + fibonacci(n-2); } Trace the following: System.out.println(fibonacci(4));

Recursion12 Recursion: Inherent cost of recursion: A lot of system overhead associated with method calls Advantages of recursion: Provides a clear readable intuitive solution to the problem.

Recursion13 How to problem solve recursively Given a problem of size n ask the following question: “ASSUMING I knew the solution to a smaller but identical sub problem, can I use that answer to solve my current problem?” If “yes”, how do you proceed? 1. Imagine m() to be a method that will solve all such smaller problems. 2. Imagine calling m() and incorporate the solution from m() to solve current problem. [ Think carefully about the signature of m(). ] 3. What size is the smallest sub problems? ( Note: smallest sub problem cannot be broken apart because if it could that would imply a still smaller problem! ) 4. All such small sub problems found in 3 are your base cases. 5. Replace all m() ‘s with a call to your self. 6. Put all the pieces together.

Recursion14 How to problem solve recursively Problem: Write a method to find x n Can this problem be solved recursively? “ASSUMING I knew the solution … 1. Imagine a method m() that will solve this problem, and call it m(x,n-1) -> returns an int, x n-1 2. Imagine calling m() and incorporating that solution to solve … int x_to_n = x * m(x, n-1) 3. What size is the smallest sub problems? n = 0, x 0 = 1 4. All such small sub problems found in 3 are your base cases. if (n == 0) return 1; 5. Replace all m() ‘s with a call to your self. int x_to_n = x * power(x,n-1);

Recursion15 How to problem solve recursively 6. Put all the pieces together. public static int power(int x, int n){ if (n == 0) return 1; //apparently, n is not zero int x_to_n = x * power(x,n-1); return x_to_n; }

Recursion16 The (classic) Towers of Hanoi problem Objective: To move n rings from any peg start peg ( s ) to any destination peg ( d ), without 1. moving more than one ring at a time, and 2. without placing a larger ring on top of a smaller ring. Analysis/algorithm: ? peg 1peg 2peg 3 Is it recursive? What would method m() look like? Can we phrase the solution in terms of m() ? s = 1, d = 3

Recursion17 Towers of Hanoi problem /** * The purpose of this method is to solve the towers of Hanoi * problem for n rings being moved amongst 3 pegs, numbered 1,2 * and 3. n - the number of rings to move o - the origination peg (where the rings currently are) d - where the rings should be moved too */ public static void toh(int n, int o, int d){ //check for base if (n == 1) System.out.println(“move a ring from “+o+” to “+d); else{ toh(n-1,o, 6-o-d); System.out.println(“move a ring from “+o+” to “+d); toh(n-1, 6-o-d, d); }

Recursion18 Towers of Hanoi problem TOH(3,1,3) TOH(2,1,2) TOH(1,1,3)TOH(1,3,2) TOH(2,2,3) TOH(1,2,1)TOH(1,1,3) Trace: toh(3,1,3) What is the performance of toh() ?

Recursion19 Recursion and arrays - print an array out, backwards Problem: print an array backwards. Can this problem be solved recursively? 1. Imagine a method m() that will solve this problem. [ Think carefully about the signature of such a method. ] 2. Imagine calling m() and incorporating that solution to solve … 3. What size is the smallest sub problems? 4. All such small sub problems found in 3 are your base cases. 5. Replace all m() ‘s with a call to your self. [ What happens if the signatures don’t match? ] 6. Put all the pieces together. Performance?

Recursion20 Searching an array Problem: Search and array, A, of integers for a value B. If found return location index; otherwise return –1. Performance?

Recursion21 Recursion - Divide and Conquer - Binary search /** * This method searches an array of sorted Comparable objects a - the array of sorted Comparable objects - the value searched for - the left starting index - the right starting index array index of value if found; -1 otherwise. */ public static int binarySearch(int[] a, int val, int l, int r){ if (l > r)return -1; //not found int mid = (l+r)/2; if (val == a[mid])return mid; //found if (val < a[mid])return binarySearch(a,val,l,mid-1); elsereturn binarySearch(a,val,mid+1,r); } Performance?

Recursion22 Sorting an array Problem: Sort an integer array, A. What recursive strategies can we use, if any? Can you come up with any?

Recursion23 Recursion - Divide and Conquer - Merge Sort /** This method will sort an array using merge sort*/ public static int[] sort(int a[]){ return mergeSort(a, l, r); } /** This method will sort an array of Comparable objects * using Merge sort array - the array to sort l - the left index r - the right index */ public static void mergeSort(int[] array, int l, int r){ if (l < r){ int mid = (l+r)/2;//divide in half mergeSort(array, l, mid);//sort each half mergeSort(array, mid+1, r); merge(array, l,mid,r);//merge sorted halves } public static void merge(int[] array, int l, int mid, int r){ 1. create a temp array to hold the merging halves 2. merge the two halves 3. Make sure all elements of the two halves are in the temp array 4. Copy elements from the temp array back to array. } Performance?

Recursion24 Recursion - Divide and Conquer - Merge Sort public static void merge(int[] array, int l, int mid, int r){ //create an array int[] temp = new int[r+1]; //merge int li=l, ri = mid+1, ti = l-1; while ((li <= mid) && (ri <= r)){ ti++; if (array[li] < array[ri]){ temp[ti] = array[li]; li++; } else{ temp[ti] = array[ri]; ri++; } // Make sure all elements of the two halves are in the temp array for(int i=li; i<=mid; i++, ti++) temp[ti] = array[i]; for(int i=ri; i<=r; i++, ti++) temp[ti] = array[i]; //Copy elements from the temp array back to array. for(int i=l; i<=r; i++) array[i] = temp[i]; }

Recursion25 Recursion - Divide and Conquer - Quick Sort Problem: Sort an integer array, A. public static int[] sort(int[] a) Can this problem be solved recursively? Imagine 1: Move elements in array into the left and right of the array such that all the elements on the left are less than some pivot value and all the elements to the right are greater. Imagine that you know this break even index. Imagine 2: Imagine that m() is a method that will sort an array’s elements between l and r: int[] m(int[] a, int l, int r) 2. Imagine calling m() and incorporating that solution to solve … int pivIndex = partition(a, l,r); m(a,l,pivIndex-1); m(a,pivIndex+1, r); 3. Base case(s) if (l < r) {

Recursion26 Recursion - Divide and Conquer - Quick Sort /** This method will sort an array using quick sort*/ public static int[] sort(int a[]){ return quiskSort(a, l, r); } /** This method will sort an array of Comparable objects * using Quick sort array - the array to sort l - the left index r - the right index */ private static void quickSort(int[] array, int l, int r){ if (l < r){ int pivot = partition(array,l,r); quickSort(array, l, pivot-1); //sort each half quickSort(array, pivot+1, r); } private static void partition(int[] array, int l,int r){ 0. Pivot value is array[l] 1. Scan from left to right and right to left moving items larger than pivot to the right and those smaller than pivot to the left 2. Move pivot to its sorted position 3. return pivot index } Performance?

Recursion27 Recursion - Divide and Conquer - Quick Sort public static void partition(int[] array, int l,int r){ //Pivot value is array[l] int pivotVal = array[l]; //Scan from left to right and right to left moving items larger //than pivot to the right and those smaller than pivot to the left int li = l, ri = r; while (li < ri){ while ((pivotVal > array[li]) && (li<ri)) li++; while (pivotVal < array[ri]) ri--; if (li<ri){ int temp = array[li]; array[li] = array[ri]; array[ri] = temp; li++; ri--; } //Move pivot to its sorted position array[l] = array[ri]; array[ri] = pivotVal; //return pivot index return ri; }