Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
courses:cs211:winter2014:journals:emily:entryseven [2014/03/10 21:27] – [Chapter 5.2] hardye | courses:cs211:winter2014:journals:emily:entryseven [2014/03/25 19:10] (current) – [Seventh Entry] hardye | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== | + | ====== Entry Seven ====== |
====== Chapter 5.2 ====== | ====== Chapter 5.2 ====== | ||
Line 35: | Line 35: | ||
**Counting Inversions** | **Counting Inversions** | ||
+ | In this section we apply some divide and conquer algorithms to solve the problem of counting inversions. It is similar to merge sort. This problem is about matching preferences etc. for a website like Netflix that compares your rankings and another person' | ||
+ | |||
+ | **The Algorithm: | ||
+ | * if we looked at every pair of number to see if they were inverted would take O(n< | ||
+ | * our goal is O(n log n) | ||
+ | * divide the set of numbers into two pieces of size n/2 and count inversions in each half separate then count the inversions between the halves | ||
+ | * to count which numbers belong in the other half we recursively sort the halves | ||
+ | * after recursively sorting and counting inversions to each half apply Merge-and-Count | ||
+ | * we should produce a single sorted list of the union of the two halves | ||
+ | * also should produce the number of inversions between the two lists | ||
+ | * have 2 pointers that point to the current element in each list | ||
+ | * idea is to append the smallest element to the union list, and if the 2nd half at the pointer is smaller than the first half at that current pointer there is an inversion | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | While both lists are nonempty: | ||
+ | Let ai and bj be the elements pointed to by the current pointer | ||
+ | | ||
+ | if bj is the smaller element then | ||
+ | | ||
+ | endif | ||
+ | | ||
+ | | ||
+ | Once one list is empty, append the remainder of the other list to the output | ||
+ | | ||
+ | |||
+ | **Analysis: | ||
+ | |||
+ | Everything happening in the while loop is constant O(1) time. The while loop executes at most n times so the run time of Merge-and-Count is O(n). | ||
+ | |||
+ | The merge and count function is used in another recursive algorithm sort-and-count | ||
+ | |||
+ | Sort-and-Count(L) | ||
+ | If the list has one element then | ||
+ | there are no inversions | ||
+ | Else | ||
+ | | ||
+ | A contains the first [n/2] elements | ||
+ | B contains the remaining [n/2] elements | ||
+ | | ||
+ | | ||
+ | | ||
+ | Endif | ||
+ | Return totalInversions = inversionsA + inversionsB + totalInversions, | ||
+ | | ||
+ | The recurrence relation is T(n) =< 2T(n/2) + cn. This is O(n log n) time. | ||
+ | |||
+ | This section was very short and easy to read. No problems here. The pictures showed on the slides in class were especially helpful in understanding this section. | ||
+ | Readability: | ||
+ | |||
+ | |||
====== Chapter 5.4 ====== | ====== Chapter 5.4 ====== | ||
**Finding the Closest Pair of Points** | **Finding the Closest Pair of Points** | ||
- | In this section we apply some divide and conquer algorithms | + | The Problem: |
+ | |||
+ | We are given a plane with a set of n points. Our goal is to find the pair of points that is closest together with a runtime of O(n log n). | ||
+ | |||
+ | **The Algorithm: | ||
+ | |||
+ | We are given the set of points P where each point p< | ||
+ | |||
+ | **(5.10)** If s, s' in S have the property that d(s, s') < z, then s and s' are within 15 positions of each other in the sorted list Sy. | ||
+ | |||
+ | Proof: any two points in the same box are within the distance z*(sqrt2/2) < z which is a contradiction that z is the min distance between any pair in Q or R so each box has only one point in S. | ||
+ | |||
+ | **Analysis: | ||
+ | |||
+ | Proof by induction that the algorithm correctly outputs a closest pair of points in P. The runtime of the algorithm is O(n log n). Sorting the points by their x and y coordinates takes O(n log n) time and the recurrence is T(n) =< 2T(n/2)+cn is O(n log n), so the whole algorithm is bounded by n log n. | ||
+ | |||
+ | This section got really confusing when we were considering the subset S with all of the boxes and the distances. I'm still kind of confused by why the points have to be within 15 positions of each other. Overall it was readability: |