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

Slides:



Advertisements
Similar presentations
Back to Sorting – More efficient sorting algorithms.
Advertisements

CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
CS 206 Introduction to Computer Science II 02 / 27 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 22 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 29 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 02 / 2009 Instructor: Michael Eckmann.
CSCE 3110 Data Structures & Algorithm Analysis
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
September 12, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
CS 171: Introduction to Computer Science II Mergesort.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
CS 206 Introduction to Computer Science II 04 / 28 / 2009 Instructor: Michael Eckmann.
CMPS1371 Introduction to Computing for Engineers SORTING.
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
CS 206 Introduction to Computer Science II 04 / 27 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 12 / 09 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 20 / 2009 Instructor: Michael Eckmann.
CS 280 Data Structures Professor John Peterson. Project Not a work day but I’ll answer questions as long as they keep coming! I’ll try to leave the last.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 12 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 12 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 20 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 01 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 13 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 12 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 15 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 28 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 04 / 29 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 12 / 10 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 16 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 08 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 12 / 08 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 25 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
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.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
1 Designing algorithms There are many ways to design an algorithm. Insertion sort uses an incremental approach: having sorted the sub-array A[1…j - 1],
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 04 / 22 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 18 / 2009 Instructor: Michael Eckmann.
Programming Abstractions Cynthia Lee CS106X. Today’s Topics Sorting! 1.The warm-ups  Selection sort  Insertion sort 2.Let’s use a data structure! 
CS 206 Introduction to Computer Science II 01 / 30 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 02 / 2009 Instructor: Michael Eckmann.
Sorting 9/13/2010. Introduction In CS1 you covered Insertion Sort, Bubble Sort, and Selection Sort. – In these algorithms we end up making a significant.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
CS 3343: Analysis of Algorithms
Algorithms and Data Structures Lecture III
Presentation transcript:

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

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

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.

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

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?

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?

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

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.