Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| courses:cs211:winter2018:journals:holmesr:section_5.0 [2018/03/14 03:49] – holmesr | courses:cs211:winter2018:journals:holmesr:section_5.0 [2018/03/14 03:50] (current) – holmesr | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Chapter 5 Divide and Conquer algorithms ====== | ====== Chapter 5 Divide and Conquer algorithms ====== | ||
| - | ====== Section 5.1 The Mergesort Algorithm | + | ===== Section 5.1 The Mergesort Algorithm ===== |
| The mergesort algorithm with which we are all familiar provides an excellent opportunity to study recurrence relations. As with many divide-and-conquer algorithms, the behvior of mergesort can be described with a template such as: divide the input into two equally-sized pieces, recursively solve those pieces and then recombine the results into an overall solution. For mergesort, as with many algorithms of this nature, it will " | The mergesort algorithm with which we are all familiar provides an excellent opportunity to study recurrence relations. As with many divide-and-conquer algorithms, the behvior of mergesort can be described with a template such as: divide the input into two equally-sized pieces, recursively solve those pieces and then recombine the results into an overall solution. For mergesort, as with many algorithms of this nature, it will " | ||
| Line 10: | Line 10: | ||
| There are also methods using substitution and partial substitution to solve for the running time but these seem to me to be unhelpful unless you already have a solid guess for the running time. Even in the case that you have a solid guess for running time though, it is still a guess and more likely to lead you down the wrong path than unrolling the recursion, so why not just unroll it from the start in order to save yourself time, frustration and guesswork? These two substitution sections at the end are my main issue with the section 5.1 since they seem to add now value, at least in my mind. Maybe I simply don't understand them though. | There are also methods using substitution and partial substitution to solve for the running time but these seem to me to be unhelpful unless you already have a solid guess for the running time. Even in the case that you have a solid guess for running time though, it is still a guess and more likely to lead you down the wrong path than unrolling the recursion, so why not just unroll it from the start in order to save yourself time, frustration and guesswork? These two substitution sections at the end are my main issue with the section 5.1 since they seem to add now value, at least in my mind. Maybe I simply don't understand them though. | ||
| - | ====== Section 5.2 Further Recurrence Relations | + | ===== Section 5.2 Further Recurrence Relations ===== |
| This section explores a more general version of recurrence problem than the previous one did. This type of recurrence creates recursive calls on q subproblems of size n/2. Mergesort uses q = 2, but can in fact be 1 or >2. Generalizing the inequality from the previous section gives us: T(n) <= qT(n/2) + cn when n > 2. | This section explores a more general version of recurrence problem than the previous one did. This type of recurrence creates recursive calls on q subproblems of size n/2. Mergesort uses q = 2, but can in fact be 1 or >2. Generalizing the inequality from the previous section gives us: T(n) <= qT(n/2) + cn when n > 2. | ||
| Line 23: | Line 23: | ||
| Finally, we can consider what happens in the case that the algorithm " | Finally, we can consider what happens in the case that the algorithm " | ||
| + | |||
| + | ===== Section 5.3 Counting Inversions ===== | ||
| + | |||
| + | The motivation for this type of problem is based in the analysis of a set of rankings compared to a different set of rankings of the same items. A way to compare these sets of ordered items is to look at how many pairs are "out of order" from one ranking to another. An inversion occurs when indices i<j but a< | ||
| + | |||
| + | Looking at every pair of numbers would require O(n< | ||
| + | |||
| + | Merge-and-count works by maintaining pointers to the front of each sublist and removing the lesser one from its sublist when it is added to what will become the sorted list. The algorithm must also count the number of inversions in addition to ordering the list. It does this by increasing the count of inversions by whatever the remaining size of the first subset of rankings is. This operation counts a possibly large number of inversions in constant time. Thus, O(n log n) running time. | ||
| + | |||
| + | I found this chapter fairly straightforward to understand. | ||
