Recursive Algorithms. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Similar documents
Lesson 12: Recursion, Complexity, Searching and Sorting. Modifications By Mr. Dave Clausen Updated for Java 1_5

Sorting & Searching (and a Tower)

Algorithm for siftdown(int currentposition) while true (infinite loop) do if the currentposition has NO children then return

Recursive Definitions

Sorting and Searching

10/21/ Linear Search The linearsearch Algorithm Binary Search The binarysearch Algorithm

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion

Chapter 10 - Notes Applications of Arrays

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

Sorting. Task Description. Selection Sort. Should we worry about speed?

Sorting. Quicksort analysis Bubble sort. November 20, 2017 Hassan Khosravi / Geoffrey Tien 1

Programming with Recursion. What Is Recursion?

Data Structures and Algorithms Notes

C/C++ Programming Lecture 18 Name:

Recursion. Let s start by looking at some problems that are nicely solved using recursion. First, let s look at generating The Fibonacci series.

11/2/2017 RECURSION. Chapter 5. Recursive Thinking. Section 5.1

Identify recursive algorithms Write simple recursive algorithms Understand recursive function calling

CS 171: Introduction to Computer Science II. Quicksort

Sorting. Bringing Order to the World

Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved

Recursion. Chapter 5

Lecture 19 Sorting Goodrich, Tamassia

Programming II (CS300)

COMP Data Structures

Programming II (CS300)

Test Bank Ver. 5.0: Data Abstraction and Problem Solving with C++: Walls and Mirrors, 5 th edition, Frank M. Carrano

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

Sorting is ordering a list of objects. Here are some sorting algorithms

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

Unit-2 Divide and conquer 2016

algorithm evaluation Performance

Sorting and Searching Algorithms

S O R T I N G Sorting a list of elements implemented as an array. In all algorithms of this handout the sorting of elements is in ascending order

Chapter 15: Recursion

Overview of Sorting Algorithms

1.7 Recursion. Department of CSE

CS126 Final Exam Review

Recursion Solution. Counting Things. Searching an Array. Organizing Data. Backtracking. Defining Languages

recursive algorithms 1

Sorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort

LECTURE 17. Array Searching and Sorting


Module 08: Searching and Sorting Algorithms

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

Data Abstraction & Problem Solving with C++: Walls and Mirrors 6th Edition Carrano, Henry Test Bank

Recursion. Chapter 17 CMPE13. Cyrus Bazeghi

Recursion. ! When the initial copy finishes executing, it returns to the part of the program that made the initial call to the function.

Recursion Chapter 17. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Recursion. Example R1

Chapter 6 Recursion. The Concept of Recursion

Chapter Objectives. List Processing. Chapter 10: Applications of Arrays and Strings. Java Programming: Program Design Including Data Structures

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms

O(n): printing a list of n items to the screen, looking at each item once.

The University Of Michigan. EECS402 Lecture 07. Andrew M. Morgan. Sorting Arrays. Element Order Of Arrays

Cpt S 122 Data Structures. Sorting

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

What is Recursion? ! Each problem is a smaller instance of itself. ! Implemented via functions. ! Very powerful solving technique.

SORTING. Comparison of Quadratic Sorts

26 How Many Times Must a White Dove Sail...

Announcement. Submit assignment 3 on CourSys Do not hand in hard copy Due Friday, 15:20:00. Caution: Assignment 4 will be due next Wednesday

Programming II (CS300)

SOFTWARE DEVELOPMENT 1. Recursion 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

2/14/13. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

We can use a max-heap to sort data.

9/10/12. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

CS Sorting Terms & Definitions. Comparing Sorting Algorithms. Bubble Sort. Bubble Sort: Graphical Trace

Divide & Conquer. 2. Conquer the sub-problems by solving them recursively. 1. Divide the problem into number of sub-problems

Table ADT and Sorting. Algorithm topics continuing (or reviewing?) CS 24 curriculum

Sorting Pearson Education, Inc. All rights reserved.

Data structure and algorithm in Python

Searching and Sorting

CS159. Nathan Sprague. November 9, 2015

Chapter 17 Recursion

MPATE-GE 2618: C Programming for Music Technology. Unit 4.2

SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY

CS/CE 2336 Computer Science II

Divide and Conquer Algorithms: Advanced Sorting

Sorting. Bubble Sort. Selection Sort

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

Lecture Notes 14 More sorting CSS Data Structures and Object-Oriented Programming Professor Clark F. Olson

Algorithm Design and Recursion. Search and Sort Algorithms

Priority queues. Priority queues. Priority queue operations

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm

CP222 Computer Science II. Searching and Sorting

Lecture 6 Sorting and Searching

q To develop recursive methods for recursive mathematical functions ( ).

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

q To develop recursive methods for recursive mathematical functions ( ).

IS 709/809: Computational Methods in IS Research. Algorithm Analysis (Sorting)

Active Learning: Sorting

CSE 143. Two important problems. Searching and Sorting. Review: Linear Search. Review: Binary Search. Example. How Efficient Is Linear Search?

UNIT-2. Problem of size n. Sub-problem 1 size n/2. Sub-problem 2 size n/2. Solution to the original problem

cs Java: lecture #6

DATA STRUCTURES/UNIT 3

Sorting. CSE 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation. CSE143 Au

CSE 214 Computer Science II Recursion

Introduction. Sorting. Definitions and Terminology: Program efficiency. Sorting Algorithm Analysis. 13. Sorting. 13. Sorting.

Transcription:

Recursive Algorithms CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Recursive Algorithms Within a given method, we are allowed to call other accessible methods. It is also possible to call the same method from within the method itself. This is called a recursive call. For certain problems a recursive solution is very natural and simple. It is possible to implement a recursive algorithm without using recursion, but the code can be more complex. 2

Example of Recursion The factorial of N is the product of the first N positive integers: N! = N * (N 1) * (N 2 ) *... * 2 *1 This is useful for many situations, e.g. there are n! possible sequences of n objects there are n!(n-k)!/k! unique subsets of size k, from a set of size n. The factorial of N can be defined recursively as { n * factorial(n-1) if n>1 factorial(n) = 1 otherwise 3

Recursive Method An recursive method is a method that contains a statement (or statements) that makes a call to itself. Implementing the factorial of N recursively will result in the following method. Test to stop or continue. End case: recursion stops. Recursive case: recursion continues. public int factorial( int N ) { if ( N == 1 ) { return 1; else { return N * factorial( N-1 ); 4

The details As with any call, a recursive call results in the creation of temporary workspace for the called method and copying of parameters. Each call to a method results in the creation of a separate workspace with new copies of variables (local and formal parameters). When a recursive call ends, flow returns to the caller. 5

Example factorial( 3 ); public int factorial( int N ) { if ( N == 1 ) { return 1; else { return N * factorial( N-1 ); N 3 public int factorial( int N ) { if ( N == 1 ) { return 1; else { return N * factorial( N-1 ); N 2 public int factorial( int N ) { if ( N == 1 ) { return 1; else { return N * factorial( N-1 ); N 1 6

6 Example factorial( 3 ); public int factorial( int N ) { if ( N == 1 ) { return 1; else { 2 return N * factorial( N-1 ); N 3 public int factorial( int N ) { if ( N == 1 ) { return 1; else { 1 return N * factorial( N-1 ); N 2 public int factorial( int N ) { if ( N == 1 ) { return 1; else { return N * factorial( N-1 ); N 1 7

Directory Listing List the names of all files in a given directory and its subdirectories. public void directorylisting(file dir) { //assumption: dir represents a directory String[] filelist = dir.list(); //get the contents String dirpath = dir.getabsolutepath(); Test End case for (int i = 0; i < filelist.length; i++) { File file = new File(dirPath + "/" + filelist[i]); if (file.isfile()) { //it's a file System.out.println( file.getpath() ); Recursive case else { directorylisting( file ); //it's a directory //so make a //recursive call 8

Anagram List all anagrams of a given word. Word C A T C T A A T C A C T T C A T A C Anagrams 9

Anagram Solution The basic idea is to make recursive calls on a sub-word after every rotation. Here s how: C A T C A T Recursion C T A Rotate Left A T C Rotate Left T C A Recursion Recursion A T C A C T T C A T A C 10

Anagram Method Test End case public void anagram( String prefix, String suffix ) { String newprefix, newsuffix; int numofchars = suffix.length(); if (numofchars == 1) { //End case: print out one anagram System.out.println( prefix + suffix ); Recursive case else { for (int i = 1; i <= numofchars; i++ ) { newsuffix = suffix.substring(1, numofchars); newprefix = prefix + suffix.charat(0); anagram( newprefix, newsuffix ); //recursive call //rotate left to create a rearranged suffix suffix = newsuffix + suffix.charat(0); 11

Towers of Hanoi The goal of the Towers of Hanoi puzzle is to move N disks from Peg 1 to Peg 3: Start: Goal: Peg1 Peg2 Peg3 Only one disk can be moved at a time. A larger disk cannot be placed on top of a smaller disk. Peg1 Peg2 Peg3 12

Towers of Hanoi Solution Peg1 Peg2 Peg3 Peg1 Peg2 Peg3 Peg1 Peg2 Peg3 Peg1 Peg2 Peg3 13

towersofhanoi Method Test public void towersofhanoi(int N, //number of disks int from, //origin peg int to, //destination peg int spare ){//"middle" peg if ( N == 1 ) { End case Recursive case moveone( from, to ); else { towersofhanoi( N-1, from, spare, to ); moveone( from, to ); towersofhanoi( N-1, spare, to, from ); private void moveone( int from, int to ) { System.out.println( from + " ---> " + to ); 14

Recursion vs Iteration Recursion has greater overhead due to method calls, variable setups etc. Recursion provides cleaner solutions Can be simulated using iteration and a stack-like structure may add too much complexity (consider an iterative solution for Towers of Hanoi) 15

When Not to Use Recursion When recursive algorithms are designed carelessly, it can lead to very inefficient and unacceptable solutions. For example, consider the following: public int fibonacci( int N ) { if (N == 0 N == 1) { return 1; else { return fibonacci(n-1) + fibonacci(n-2); 16

Excessive Repetition Recursive Fibonacci ends up repeating the same computation numerous times. fibonacci(5) fibonacci(4)+fibonacci(3) fibonacci(2)+fibonacci(1) fibonacci(1)+fibonacci(0) fibonacci(3)+fibonacci(2) fibonacci(1)+fibonacci(0) fibonacci(2)+fibonacci(1) fibonacci(1)+fibonacci(0) 17

Non-recursive Fibonacci public int fibonacci( int N ) { int fibn, fibn1, fibn2, cnt; if (N == 0 N == 1 ) { return 1; else { fibn1 = fibn2 = 1; cnt = 2; while ( cnt <= N ) { fibn = fibn1 + fibn2; //get the next fib no. fibn1 = fibn2; fibn2 = fibn; cnt ++; return fibn; 18

Quiz What does the following method compute for N>0? public int recurse( int N ) { if (N == 1 ) { return 2; else { return 2*recurse(N-1); A. N 2 B. 2 N C. N! D. (N-1)! 19

Overhead of recursion Remember that each recursive call is expensive create local variables for each call copy arguments into local variables track the execution point for each call returned values are copied back local space is reclaimed 20

When Not to Use Recursion In general, use recursion if A recursive solution is natural and easy to understand. A recursive solution does not result in excessive duplicate computation. The equivalent iterative solution is too complex. 21

Trees Trees are a very commonly used data structure in Computer Science For example, simple binary trees can be used to maintain a sorted list of strings Suppose we had an unknown number of strings to input, sort, then output We could use linked lists Have to modify insert to ensure sorted order Or, we can use trees more efficient 22

A Sorted Binary Tree d, b, a, e, f, c, do, dot d b e a c do f dot 23

The Sorted Binary Tree Node class SBTNode { private SBTNode left, right; private String content; public SBTNode (String c) { left = right = null; content = c; public void insert(string c){ if(c.compareto(this.content)<=0) if(left==null) left = new SBTNode(c); else left.insert(c); else if(right==null) right = new SBTNode(c); else right.insert(c );... left :SBTNode content left right content right 24

Binary Tree Node Class (cont.)... public void print(){ if(left!=null) left.print(); System.out.println(content); if(right!=null) right.print(); 25

Example Use public static void main (String[] args) { SBTNode root = null; String input; input = JOptionPane.showInputDialog(null, Enter String ); if(input.length()>0) { root = new SBTNode(input); while(true){ input= JOptionPane.showInputDialog(null, Enter String ); if(input.length()<1) break; root.insert(input); root.print(); 26

Expression Trees 5*x+ 2*y + + * * 5 x 2 y + * * z / v 5 x 2 y 5*x+ 2*y + z/v 27

Tree traversal Visiting all the nodes of a tree is called a tree traversal e.g., in earlier example, we visited and printed out each node Important types of traversals In order: left child, parent, right child e.g., sorted output Post order: left child, right child, parent e.g., to get postfix version of expression useful for expression evaluation using a stack Pre order: parent, left child, right child e.g., to get prefix version of expression 28

Expression Tree Traversals * In: 5*x+ 2*y Post: 5x*2y*+ Pre: +*5x*2y + * 5 x 2 y * + * + 5 x 2 y z In: 5*x+ 2*y + z/v Post: 5x*2y*+zv/+ Pre: ++*5x*2y/zv 29 / v

General Trees In general, trees may have more than two children no ordering among children They don t have: cycles, multiple parents, single root More general structure: graph Applications: class hierarchy, index structures (databases) 30

Tree Traversal... public void inorderprint(){ if(left!=null) left.print(); System.out.println(content); if(right!=null) right.print(); public void preorderprint(){ System.out.println(content); if(left!=null) left.print(); if(right!=null) right.print(); 31

Searching When we maintain a collection of data, one of the operations we need is a search routine to locate desired data quickly. Array searching Given an array of values and a search key, return the index of the key if found -1 if key is not in the array Linear search Binary search logarithmic performance, but requires sorted data 32

Search Result Unsuccessful Search: search( 45 ) NOT_FOUND Successful Search: search( 12 ) 4 number 0 1 2 3 4 5 6 7 8 23 17 5 90 12 44 38 84 77 33

Linear Search Search the array from the first to the last position in linear progression. public int linearsearch ( int[] number, int searchvalue ) { int loc = 0; while (loc < number.length && number[loc]!= searchvalue) { loc++; if (loc == number.length) { //Not found return NOT_FOUND; else { return loc; //Found, return the position 34

Binary Search Routine public int binarysearch (int[] number, int searchvalue) { int low = 0, high = number.length - 1, mid = (low + high) / 2; while (low <= high && number[mid]!= searchvalue) { if (number[mid] < searchvalue) { low = mid + 1; else { //number[mid] > searchvalue high = mid - 1; mid = (low + high) / 2; //integer division will truncate if (low > high) { mid = NOT_FOUND; return mid; 35

Binary Search Performance Successful Search Best Case 1 comparison Worst Case log 2 N comparisons Unsuccessful Search Best Case = Worst Case log 2 N comparisons Since the portion of an array to search is cut into half after every comparison, we compute how many times the array can be divided into halves. After K comparisons, there will be N/2 K elements in the list. We solve for K when N/2 K = 1, deriving K = log 2 N. 36

Comparing N and log 2 N Performance Array Size Linear N Binary log 2 N 37

Sorting When we maintain a collection of data, many applications call for rearranging the data in certain order, e.g. arranging Person information in ascending order of age. Here s the problem statement: Given an array of N integer values, arrange the values into ascending order. We will count the number of comparisons the algorithms make to analyze their performance. The ideal sorting algorithm will make the least possible number of comparisons to arrange data in a designated order. We will compare different sorting algorithms by analyzing their worst-case performance. 38

Selection Sort 1. Find the smallest element in the list. 0 1 2 3 4 5 6 7 8 2. Exchange the element in the first position and the smallest element. Now the smallest element is in the first position. first min 23 17 5 90 12 44 38 84 77 exchange 3. Repeat Step 1 and 2 with the list having one less element (i.e., the smallest element is discarded from further processing). 0 1 2 3 4 5 6 7 8 5 17 23 90 12 44 38 84 77 sorted unsorted This is the result of one pass. 39

Selection Sort Passes Pass # 1 sorted 0 1 2 3 4 5 6 7 8 5 17 23 90 12 44 38 84 77 Result AFTER one pass is completed. 2 5 12 23 90 17 44 38 84 77 3 5 12 17 90 23 44 38 84 77 7 5 12 17 23 38 44 77 84 90 8 5 12 17 23 38 44 77 84 90 40

Selection Sort Routine public void selectionsort( int[] number ) { int startindex, minindex, length, temp; length = number.length; for (startindex = 0; startindex <= length-2; startindex++){ //each iteration of the for loop is one pass minindex = startindex; //find the smallest in this pass at position minindex for (i = startindex+1; i <= length-1; i++) { if (number[i] < number[minindex]) minindex = i; //exchange number[startindex] and number[minindex] temp = number[startindex]; number[startindex] = number[minindex]; number[minindex] = temp; 41

Selection Sort Performance We derive the total number of comparisons by counting the number of times the inner loop is executed. For each execution of the outer loop, the inner loop is executed length start times. The variable length is the size of the array. Replacing length with N, the array size, the sum is derived as 42

Bubble Sort With selection sort, we make one exchange at the end of one pass. Bubble sort improves the performance by making more than one exchange during each pass. By making multiple exchanges, we will be able to move more elements toward their correct positions using the same number of comparisons as the selection sort makes. The key idea of the bubble sort is to make pairwise comparisons and exchange the positions of the pair if they are out of order. 43

One Pass of Bubble Sort 0 1 2 3 4 5 6 7 8 23 17 5 90 12 44 38 84 77 exchange 17 5 23 12 44 90 38 84 77 exchange 17 23 5 90 12 44 38 84 77 exchange 17 5 23 12 44 38 90 84 77 exchange 17 5 23 90 12 44 38 84 77 17 5 23 12 44 38 84 90 77 ok exchange exchange 17 5 23 12 90 44 38 84 77 exchange 17 5 23 12 44 38 84 77 90 The largest value 90 is at the end of the list. 44

Bubble Sort Routine public void bubblesort(int[] number) { int temp, bottom, i; boolean e x c h a n g e d = t r u e ; bottom = number.length - 2; while (exchanged) { exchanged = false; for (i = 0; i <= bottom; i++) { if (number[i] > number[i+1]) { temp = number[i]; //exchange number[i] = number[i+1]; number[i+1] = temp; exchanged = true; //exchange is made bottom--; 45

Bubble Sort Performance In the worst case, the outer while loop is executed N-1 times for carrying out N-1 passes. For each execution of the outer loop, the inner loop is executed bottom+1 times. The number of comparisons in each successive pass is N-1, N-2,, 1. Summing these will result in the total number of comparisons. So the performances of the bubble sort and the selection sort are approximately equivalent. However, on the average, the bubble sort performs much better than the selection sort because it can finish the sorting without doing all N-1 passes. 46

Quicksort To sort an array from index low to high, we first select a pivot element p. Any element may be used for the pivot, but for this example we will user number[low]. Move all elements less than the pivot to the first half of an array and all elements larger than the pivot to the second half. Put the pivot in the middle. Recursively apply quicksort on the two halves. 47

Quicksort Partition low high p... Any element can be used as a pivot. For simplicity, we use number[low] as pivot. number[i]<p partition number[i]>p p Quicksort mid Quicksort 48

Method quicksort public void quicksort( int[] number, int low, int high ) { if ( low < high ) { int mid = partition( number, low, high ); quicksort( number, low, mid-1 ); quicksort( number, mid+1, high ); 49

Quicksort Performance In the worst case, quicksort executes roughly the same number of comparisons as the selection sort and bubble sort. On average, we can expect a partition process to split the array into two roughly equal subarrays. 50