Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 206 Introduction to Computer Science II 02 / 27 / 2009 Instructor: Michael Eckmann.

Similar presentations


Presentation on theme: "CS 206 Introduction to Computer Science II 02 / 27 / 2009 Instructor: Michael Eckmann."— Presentation transcript:

1 CS 206 Introduction to Computer Science II 02 / 27 / 2009 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS Spring 2009 Today’s Topics Questions? Comments? More recursion –Dynamic Programming For fibonacci –Divide and Conquer MergeSort Maximum contiguous subsequence

3 Recursion The major problem with that change making algorithm is that it makes so many recursive calls and it duplicates work already done. Example anyone? An idea called dynamic programming is a good solution in this case. Dynamic Programming is an idea that instead of making recursive calls to figure out something that we already figured out we compute it once and save the value in a table for lookup later.

4 Dynamic Programming Could we use dynamic programming for the fibonacci numbers? Yes. Let's write the fibonacci method to take advantage of the stored values when we can.

5 Divide and Conquer The divide and conquer technique is a way of converting a problem into smaller problems that can be solved individually and then combining the answers to these subproblems in some way to solve the larger problem DIVIDE = solve smaller problems recursively, except the base case(s)‏ CONQUER = compute the solution to the overall problem by using the solutions to the smaller problems solved in the DIVIDE part.

6 Divide and Conquer (MergeSort)‏ There are several popular divide and conquer sort algorithms. One is called MergeSort (see pages )‏ Later in the semester we'll talk about two others HeapSort and QuickSort. But for now, let's just discuss the MergeSort algorithm and see how it uses divide and conquer technique. For this problem of sorting, can anyone guess –what might happen in the divide stage –or what happens in the conquer stage

7 Divide and Conquer (MergeSort)‏ Structure of Merge Sort: –if list has 0 or 1 element it is sorted, done (base case)‏ –else Divide list into two lists and do MergeSort on each of these lists. Combine the two sorted lists into one sorted list. Let's look at: Let's assume we're sorting an array of ints. –what will we need to keep track of? –is it recursive? –what will we need to pass in as parameters to MergeSort? –if we write a separate method for the combine part, what will be its parameters and return values?

8 Divide and Conquer (MergeSort)‏ Structure of Merge Sort: –if list has 0 or 1 element it is sorted, done (base case)‏ –else Divide list into two lists and do MergeSort on each of these lists. Combine (merge) the two sorted lists into one sorted list. We'll need to pass in an array as a parameter to MergeSort, but since sometimes we'll only be sorting part of it, we need to pass in the range of indices to sort (or first index and length). The parameters for the merge method will be the array and information to tell it the two ranges of indices that need to be combined.

9 Divide and Conquer (MergeSort)‏ Can anyone think of a good way to merge two sorted parts of an array?

10 Divide and Conquer (MergeSort)‏ Can anyone think of a good way to merge two sorted parts of an array? –Start off with a newarray that is empty and a newindex=0 –start off the indices to the two parts of the array to merge, i1, i2 at the correct values –while (both parts of the array still have elements to consider)‏ –if the i1 element is less than the i2 element then copy it into newarray[newindex] i1++ –else copy the i2 element into newarray[newindex] i2++ –newindex++ –If there are any elements left in either side, copy them into the newarray at the end.

11 Divide and Conquer (MergeSort)‏ Can anyone think of a good way to perform the divide part? we have an array and the first index and length of the portion we are dividing.

12 Divide and Conquer (MergeSort)‏ Can anyone think of a good way to perform the divide part? we have an array and the first index (first) and length of the portion we are dividing (len). –if (len > 1)‏ leftHalfLen = len/2 rightHalfLen = len – leftHalfLen mergeSort(array, first, leftHalfLen); mergeSort(array, first+leftHalfLen, rightHalfLen); merge(array, first, leftHalfLen, rightHalfLen); –else do nothing since a list of len=1 or 0 is already sorted(base case). Let's write code for MergeSort

13 Divide and Conquer We may be able to use divide and conquer for that maximum contiguous subsequence problem. Recall that the Maximum contiguous subsequence problem is: –Given an integer sequence A 1, A 2,..., A N, find (and identify the sequence corresponding to) the maximum value of  j k=i A k. The maximum contiguous subsequence sum is zero if all are negative. Example input: { -2, 11, -4, 13, -5, 2 } the answer is 20 and the sequence is { 11, -4, 13 } Another: { 1, -3, 4, -2, -1, 6 } the answer is 7, the sequence is { 4, -2, -1, 6 } (this problem from our text book)‏

14 Divide and Conquer Divide the sequence in half. Solve each half individually then combine. If we divide the sequence in half and work on the left side, independently from the right side, there are three cases to consider: –either the maximum contiguous subsequence lives wholly within the left side –or it lives wholly within the right side –or it lives partially in the left and partially in the right if this is the case, then we are guaranteed that the sequence contains the last element of the left side and the first element of the right side. Right?

15 Divide and Conquer Further, since we are recursing, each subproblem is divided in half and conquered, and so on. Let's think about the structure of the code for this algorithm. As parameters we can have –an array seq and –2 ints for the indices, left and right so the method will return the sum of the maximum contiguous subsequence found in seq[left.. right ]

16 Divide and Conquer Maximum sum of a contiguous subsequence of –seq[left.. right ] We need a base case We need a divide part for the recursion We need a conquer part for the combining of answers to subproblems Any ideas on any of these?

17 Divide and Conquer Maximum sum of a contiguous subsequence of –seq[left.. right ] Divide part: –center = (left + right) / 2 –call maxCSS(seq, left, center) and save result in maxLeft –call maxCSS(seq, center+1, right) and save result in maxRight

18 Divide and Conquer Maximum sum of a contiguous subsequence of –seq[left.. right ] Conquer part: –compute the maxLeftBorderSum –compute the maxRightBorderSum –decide which is larger maxLeft or maxRight or maxLeftBorderSum + maxRightBorderSum

19 Divide and Conquer Maximum sum of a contiguous subsequence of –seq[left.. right ] Base case: –if (left == right)‏ return the larger of 0 or seq[left] Let's code this thing.


Download ppt "CS 206 Introduction to Computer Science II 02 / 27 / 2009 Instructor: Michael Eckmann."

Similar presentations


Ads by Google