Download presentation
Presentation is loading. Please wait.
1
Lecture 32 CSE 331 Nov 18, 2009
2
HW 8 solutions Friday
3
Counting Inversions Input: n distinct numbers a 1,a 2,…,a n Inversion: (i,j) with i a j Output: Number of inversions
4
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 Solve the stronger problem of counting inversions + sorting
5
Three kinds of inversion 10 7 2120 1001 Non-crossing inversions are counted recursively
6
Mergesort-Count algorithm Input: a 1, a 2, …, a n Output: Numbers in sorted order+ #inversion MergeSortCount( a, n ) If n = 2 return the order ( a1 > a2, min(a 1,a 2 ); max(a 1,a 2 )) a L = a 1,…, a n/2 a R = a n/2+1,…, a n return (c+c L +c R,a) (c L, a L ) = MergeSortCount(a L, n/2) (c R, a R ) = MergeSortCount(a R, n/2) (c, a) = MERGE-COUNT(a L,a R ) Counts #crossing-inversions+ MERGE O(n) T(2) = c T(n) = 2T(n/2) + cn O(n log n) time
7
Today’s agenda MERGE-COUNT Computing closest pair of points
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.