Chapter 9 Recursion © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Introduction to Algorithms Quicksort
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 6.
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Theory of Algorithms: Divide and Conquer
11 Computer Algorithms Lecture 6 Recurrence Ch. 4 (till Master Theorem) Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 17 Sorting.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 24 Sorting.
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 4 Some of the sides are exported from different sources.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Sorting.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Quicksort CIS 606 Spring Quicksort Worst-case running time: Θ(n 2 ). Expected running time: Θ(n lg n). Constants hidden in Θ(n lg n) are small.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
Chapter 9 Recursion © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Data Structures and Abstractions with Java, 4e Frank Carrano
Chapter 15: Advanced Topics: Introducing Data Structures and Recursion Visual Basic.NET Programming: From Problem Analysis to Program Design.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Chapter 12 Recursion, Complexity, and Searching and Sorting
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.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Data Structures Using C++ 2E Chapter 10 Sorting Algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 26 Sorting.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2.Solve smaller instances.
Lecture 7. Solution by Substitution Method T(n) = 2 T(n/2) + n Substitute n/2 into the main equation 2T(n/2) = 2(2(T(n/4)) + n/2) = 4T(n/4) + n And T(n)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 25 Sorting.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
Nothing is particularly hard if you divide it into small jobs. Henry Ford Nothing is particularly hard if you divide it into small jobs. Henry Ford.
Quicksort This is probably the most popular sorting algorithm. It was invented by the English Scientist C.A.R. Hoare It is popular because it works well.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 26 Sorting.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 23 Sorting.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
Analysis of Algorithms CS 477/677
Divide-and-Conquer The most-well known algorithm design strategy:
Divide-and-Conquer The most-well known algorithm design strategy:
Sorting Chapter 13 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Quicksort and Mergesort
Divide-and-Conquer The most-well known algorithm design strategy:
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Divide & Conquer Algorithms
CSC 380: Design and Analysis of Algorithms
Presentation transcript:

Chapter 9 Recursion © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Overview ● 9.1 – Introduce the recursive way of thinking. ● 9.2 – Recursive algorithms requires new techniques. ● 9.3 and 9.4 – Recursive sorting algorithms are introduced.

Thinking Recursively

● One-disk puzzle is trivial.

Thinking Recursively ● The puzzle for two disks

Thinking Recursively ● Method for the three-disk puzzle

Thinking Recursively ● By invoking hanoi2() we can write a shorter version of hanoi3().

Thinking Recursively ● Rewrite hanio2() using hanoi1()

Thinking Recursively ● We now have a pattern that will allow us to write a method to solve the puzzle for any number of disks. – It would be much better if we could write a single method which would work for any number of disks. ● Recursive method – A method which invokes itself.

Thinking Recursively ● Base case – To prevent the recursion from continuing indefinitely – So simple no recursion needed.

Thinking Recursively ● In general, to solve a problem recursively, we have two cases: – The base case, where we can solve the problem directly. – The recursive case, where we solved the problem in terms of easier subproblems. ● Subproblem leads to the base case.

Thinking Recursively ● Printing a LinkedList backward. – Iterative approach

Thinking Recursively ● This method works, but it is not very efficient. – Invokes get() each time.

Thinking Recursively ● Recursive solution: – If there are no nodes, return the empty String. – Otherwise, generate a String for the rest of the list (the part after the first item). Add the first item to the end of this String and return it.

Thinking Recursively

● To show that a recursive algorithm works correctly: – Show that the base case works correctly. – Show that if the recursive method works for a problem of size n – 1, then it works for a problem of size n. ● Base case – ToStringReversed() returns “( )”.

Thinking Recursively ● Assume that node is a reference to the first of a chain of n nodes

Thinking Recursively ● If we assume that the recursive invocation toStringReversedHelper(node.getNext()) correctly returns the String "D C B", then toStringReversedHelper(node.getNext()) + Node.getItem() + " " evaluates to "D C B A", which is what we want. ● If it works for n-1 nodes, it works for a chain of n nodes.

Thinking Recursively ● ToStringReversed() for our ArrayList class. – Again we need a helper method, and the design of the algorithm is similar: ● If there are no elements being considered, return the empty String. ● Otherwise, generate a String for all of the elements after the current one. Add the current element to the end of this String and return it.

Thinking Recursively

Analyzing Recursive Algorithms ● Analyze a recursive algorithm we must think recursively, in terms of a base case and a recursive case. – toStringReversedHleper()—a recurrence.

Analyzing Recursive Algorithms ● Solving a recurrence means transforming it with T(n) on the left and no mention of T on the right.

Analyzing Recursive Algorithms ● The base must work exactly to constitute a solution. – Guessing T(n) = n + 1 – ToStringReversed() runs in linear time.

Analyzing Recursive Algorithms ● The recurrence for hanoi():

Analyzing Recursive Algorithms

● This expansion continues until we have many copies of T(1). ● There are n levels, corresponding to T(n) down through T(1). ● The bottommost level is the therefore level n-1.

Analyzing Recursive Algorithms

● Total number of steps:

Analyzing Recursive Algorithms ● Verification that the solution is correct.

Analyzing Recursive Algorithms ● The recursion tree method can be used to analyze algorithms with only one recursive call. – Example:

Analyzing Recursive Algorithms

Merge Sort ● The recursive idea behind merge sort is: – If there is only one number to sort, do nothing. – Otherwise, divide the numbers into two groups. Recursively sort each group, then merge the two sorted groups into a single sorted array.

Merge Sort

● Merge sort is an example of a divide-and- conquer algorithm. ● A sorting algorithm that modifies an existing array, such as insertion sort, is called an in- place sort. ● Merge sort is not an in-place sort.

Merge Sort

● Merge() combine two sorted arrays into one longer, sorted array.

Merge Sort ● Merge() takes linear time in the total length of the resulting array.

Merge Sort ● mergeSortHelper recurrence.

Merge Sort

Quicksort ● Quicksort – Another divide-and-conquer sorting algorithm. – Here's the plan: ● If there are one or fewer numbers to sort, do nothing. ● Otherwise, partition the region into “small” and Large” numbers, moving the small numbers to the left and the large numbers to the right. Recursively sort each section. The entire array is now sorted.

Quicksort

● Partitioning algorithm begins by choosing some array element as the pivot. ● Numbers less than or equal to the pivot are considered small, while numbers greater than the pivot are considered large. ● As it runs, the algorithm maintains four regions: – Those numbers known to be small. – Those numbers know to be large. – Those numbers which haven't been examined yet. – The pivot itself.

Quicksort

● The four regions: – data[bottom] through data[firstAfterSmall - 1] are known to be small. – data[firstAfterSmall] through data[i-1] are known to be large. – data[i] through data[top-1] have not yet been examined. – The pivot is at data[top].

Quicksort

● Partition() is linear time

Quicksort ● Best case O(n log n), but partition() might not divide the region evenly in half. ● Worst case:

Quicksort

● Quicksort is better than insertion sort, but not as good as merge sort. – Since it has a low constant factor associated with its running time, and operates in place, Quicksort is sometimes used instead of merge sort when n is not expected to be very large.

Quicksort ● Class java.util.Arrays has several overloaded versions of the static method sort(). – The ones for arrays of primitive types use an optimized version of Quicksort that makes the worst-case behavior unlikely. – The version for arrays of objects uses merge sort. ● The difference has to do with the fact that two objects that are equals() may not be identical. ● If a sort keeps such elements in the same order as the original array, the sort is said to be stable. ● Merge sort is stable, but Quicksort is not.

Summary ● To solve a problem recursively, we define a simple base case and a recursive case. ● Recursive case solves the problems in terms of subproblems which are closer to the base case. ● Recursive algorithms are analyzed using recurrences. – To solve a recurrence, expand it into a recursion tree, then determine the number of steps at each level and the number of levels. – Plug the solution into the recurrence to verify it is correct.

Summary ● Merge sort and Quicksort – Both of these are divide-and-conquer algorithms which divide the data into parts, recursively sort the parts, and then recombine the solutions. – Merge sort, the hard work is in the recombining. ● Θ (n log n) – Quicksort, the hard work is in the dividing. ● Θ (n log n) on average, its worst-case running time is quadratic. ● Simple improvements can make the worst case unlikely.

Summary ● Recursion allows for the design of powerful, elegant algorithms, but it uses up time and space for the call stack. – Efficiency can sometimes be improved by eliminating recursion. – A tail-recursive algorithm can easily be converted into a loop. – If the algorithm is only returning a value (as opposed to modifying an existing data structure), redundant computation can be avoided by storing the results of previous invocation in a table.