Lecture 32 CSE 331 Nov 14, 2011
HW related stuff HW 8 solutions at the end of the lecture HW 7 available for pickup
Review Sessions this week Tuesday, 1:00-1:50pm– Jesse Commons 9 (Review of covered material) Friday, 11:30am-12:20pm– Atri Bell 242 (problem solving session) For more details, check out for the blog post
Need a blog and scribe volunteer
Counting Inversions Input: n distinct numbers a1,a2,…,an Inversion: (i,j) with i < j s.t. ai > aj Output: Number of inversions
Solve the stronger problem of counting inversions + sorting Divide and Conquer Divide up the problem into at least two sub-problems Solve the stronger problem of counting inversions + sorting 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
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)
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