Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 206 Introduction to Computer Science II 10 / 10 / 2008 Instructor: Michael Eckmann.

Similar presentations


Presentation on theme: "CS 206 Introduction to Computer Science II 10 / 10 / 2008 Instructor: Michael Eckmann."— Presentation transcript:

1 CS 206 Introduction to Computer Science II 10 / 10 / 2008 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 206 - Fall 2008 Today’s Topics Questions? Divide & Conquer –code for MergeSort –and another solution to the maximum contiguous subsequence problem

3 Recursion 1. have at least one base case that is not recursive 2. recusive case(s) must progress towards the base case 3. trust that your recursive call does what it says it will do (without having to unravel all the recursion in your head.)‏ 4. try not to do redundant work. That is, in different recursive calls, don't recalculate the same info.

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

5 Divide and Conquer (MergeSort)‏ There are several popular divide and conquer sort algorithms. One is called MergeSort (see pages 313-316)‏ 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

6 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: http://math.hws.edu/TMCM/java/xSortLab/ 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?

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

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

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

10 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).

11 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)‏

12 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?

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

14 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?

15 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

16 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

17 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 10 / 10 / 2008 Instructor: Michael Eckmann."

Similar presentations


Ads by Google