Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 9 Recursion © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved."— Presentation transcript:

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

2 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.

3 Thinking Recursively

4

5

6 ● One-disk puzzle is trivial.

7 Thinking Recursively ● The puzzle for two disks

8 Thinking Recursively ● Method for the three-disk puzzle

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

10 Thinking Recursively ● Rewrite hanio2() using hanoi1()

11 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.

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

13 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.

14 Thinking Recursively ● Printing a LinkedList backward. – Iterative approach

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

16 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.

17 Thinking Recursively

18 ● 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 “( )”.

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

20 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.

21 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.

22 Thinking Recursively

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

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

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

26 Analyzing Recursive Algorithms ● The recurrence for hanoi():

27 Analyzing Recursive Algorithms

28 ● 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.

29 Analyzing Recursive Algorithms

30 ● Total number of steps:

31 Analyzing Recursive Algorithms ● Verification that the solution is correct.

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

33 Analyzing Recursive Algorithms

34 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.

35 Merge Sort

36 ● 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.

37 Merge Sort

38

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

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

41 Merge Sort ● mergeSortHelper recurrence.

42 Merge Sort

43 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.

44 Quicksort

45

46 ● 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.

47 Quicksort

48 ● 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].

49 Quicksort

50

51 ● Partition() is linear time

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

53 Quicksort

54 ● 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.

55 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.

56 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.

57 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.

58 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.


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

Similar presentations


Ads by Google