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

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

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

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

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.

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.

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.

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

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

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?

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 ]

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?

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

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

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 03 / 02 / 2009 Instructor: Michael Eckmann."

Similar presentations