CS 506 Homework 2. Due by in class thursday Feb 20. Accepted without penalty until NOON Friday, Feb 21.
Unless otherwise stated, you may assume that objects are in general position. For example, you may assume that no two points have the same x-coordinate, no three points are collinear, no four points are cocircular. Also, unless otherwise stated, you may assume that any geometric primitive involving a constant number of objects each of constant complexity can be computed in O(1) time. Unless otherwise stated, you may use any method that you recall from class as a subroutine. More credit for clear answers that solve part of a question than for obscure confusing answers.
let the flipped diagonal be f.
define e1 = e.next define e2 = e.next.next define e3 = e.twin.next define e4 = e.twin.next.next f.next = e4 e4.prev = f; e4.next = e1 e1.prev = e4 e1.next = f f.prev = e1 f.twin.next = e2 e2.prev = f.twin e2.next = e3 e3.prev = e2 e3.next = f.twin f.twin.prev = e3
calculate the intersection of each input line with the first and second vertical line. Sort each line by its intersection point (say, from bottom to top). Let fi be the position of line i in the sorted list, so if line i has the LOWEST intersection with the first vertical line, then fi = 1. Let gi be the position of line i in the sorted list of intersections with the second vertical line. The sum, for all lines i, of absolute value(fi - gi) = 2 times the total number of intersections. Why? consider one line. If it is 3rd from the bottom as it crosses the left vertical line, and 6th from the bottom as it crosses the right vertical line, then it must have crossed at least three lines. ok... so this solution is mostly right. until you actually try an example. Try it, really, right now. with three lines. it doesn't work. (if it did, try a different three lines). So what is really going on? think of the lines in order from bottom to top along the left vertical line. (label these lines 1,2,3,4,5,...n) as i slide this vertical line slowly to the right, the order of these lines starts to change. That is, if the first intersection is between the very bottom-most lines, then this order becomes 2,1,3,4,5,... every intersection is a change in this order. The total number of order changes is the number of intersection. So how can we count the number of order changes? Naive algorithm: (bubble sort). for i = 1:n-1 for j = 1:n-1 for k = 1:n-1 if list(k)>list(k+1) swap(list(k),list(k+1)) increment swap_counter end end end end output("The total number of lines is: ", swap_counter) It turns out that there is an O(n log n) algorithm for this, instead of the O(n^3) bubble sort algorithm. How does this algorithm work? Algorithm Inversion Counter(L) Input: Given a permutation of the numbers from 1..n. Output: L, sorted, and the number of swaps that would have been necessary to sort L CopOut: Assume n is a power of 2. if |L| = 1 return L and 0 (one element list is already sorted with no swaps) else L1,count1 = InversionCounter( first half of L) (recurse 1) L2,count2 = InversionCounter( second half of L) (recurse 2) count = 0 while |L2| > 0 if first(L2) < first(L1) count = count + |L1| % how many swaps would it % take to move this element % past all the elements in L1 into the % first spot... pop(L2) else pop(L1) end return (sorted merged lists L1,L2), count+count1+count2 end