Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous 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 9: | Line 9: | ||
| 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 ===== | ||
| + | |||
| + | 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. | ||
| + | |||
| + | For algorithms that split the input into more than 2 subproblems we can still use the same unrolling technique discussed in the previous section. Consider a problem where q = 3. The first level will have 1 problem of size n, which will be split into 3 problems of size n/2, which is then split into 9 problems of size n/4 and so on. This makes it obvious that work is increasing throughout the running of the problem. To identify the pattern, we can notice that the number of problems per level is simply our q (3) raised to the power which is equal to the level number. Additionally, | ||
| + | |||
| + | Again the section mentions an alternative method of finding the bound using partial substitution, | ||
| + | |||
| + | Another case to be considered is one where q = 1. In the first level, there is one problem of size n. Then one problem of size n/2, then one problem of size n/4 and so on down the line. We can see that the amount of work is decreasing. It is clear from analyzing just these few levels that layer r will have size n/ | ||
| + | |||
| + | It is interesting to note these differences in running times all fall around q = 2, with radically different behavior observed when q = 1 and when q = 3. It leads me to wonder what happens when q is a non-integer number between 1 and 2. | ||
| + | |||
| + | 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. | ||
