Lecture 31 CSE 331 Nov 11, 2011
HW 8 due today I will not take any HW after 1:15pm Q1 and Q 2 in separate piles I will not take any HW after 1:15pm
Other HW related stuff Solutions to HW 8 on Monday HW 7 should be available for pickup from Monday HW 9 has been posted on the blog
Review Session Details
Optimal MST algorithms
Need 2 blog volunteers
Counting Inversions Input: n distinct numbers a1,a2,…,an Inversion: (i,j) with i < j s.t. ai > aj Output: Number of inversions
Divide and Conquer Divide up the problem into at least two sub-problems Recursively solve the sub-problems “Patch up” the solutions to the sub-problems for the final solution
Three kinds of inversion 10 7 21 20 100 1 Non-crossing inversions are counted recursively
Today’s agenda Divide and Conquer algorithm for counting the # inversions
HW 8 due today I will not take any HW after 1:15pm Q1 and Q 2 in separate piles I will not take any HW after 1:15pm
Mergesort-Count algorithm Input: a1, a2, …, an Output: Numbers in sorted order+ #inversion T(2) = c T(n) = 2T(n/2) + cn MergeSortCount( a, n ) If n = 1 return ( 0 , a1) If n = 2 return ( a1 > a2, min(a1,a2); max(a1,a2)) O(n log n) time aL = a1,…, an/2 aR = an/2+1,…, an (cL, aL) = MergeSortCount(aL, n/2) (cR, aR) = MergeSortCount(aR, n/2) O(n) Counts #crossing-inversions+ MERGE (c, a) = MERGE-COUNT(aL,aR) return (c+cL+cR,a)
Rest of today’s agenda MERGE-COUNT Computing closest pair of points
Closest pairs of points Input: n 2-D points P = {p1,…,pn}; pi=(xi,yi) d(pi,pj) = ( (xi-xj)2+(yi-yj)2)1/2 Output: Points p and q that are closest
Group Talk time O(n2) time algorithm? 1-D problem in time O(n log n) ?
Sorting to rescue in 2-D? Pick pairs of points closest in x co-ordinate Pick pairs of points closest in y co-ordinate Choose the better of the two
Rest of today’s agenda Divide and Conquer based algorithm