1 / 23

Lecture 30

Lecture 30 . CSE 331 Nov 11, 2016. Mini project video due Mon. Homework 8. Solutions to Homework 7. At the END of the lecture. Counting Inversions. Input: n distinct numbers a 1 ,a 2 ,…,a n. Inversion: (i,j) with i < j s.t. a i > a j. Output: Number of inversions.

rosettad
Download Presentation

Lecture 30

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 30 CSE 331 Nov 11, 2016

  2. Mini project video due Mon

  3. Homework 8

  4. Solutions to Homework 7 At the END of the lecture

  5. Counting Inversions Input: n distinct numbers a1,a2,…,an Inversion: (i,j) with i < j s.t. ai > aj Output: Number of inversions

  6. Divide and Conquer Divide up the problem into at least two sub-problems Solve all sub-problems: Mergesort Recursively solve the sub-problems Solve some sub-problems: Multiplication Solve stronger sub-problems: Inversions “Patch up” the solutions to the sub-problems for the final solution

  7. Handling crossing inversions aL aR 1 100 2 4 Why should aL and aR be sorted? http://www.dovecoteidea.com/ Sort aL and aRrecursively!

  8. Mergesort-Count algorithm Input: a1, a2, …, an Output: Numbers in sorted order+ #inversion T(2) = c MergeSortCount( a, n ) T(n) = 2T(n/2) + cn If n = 1 return( 0 , a1) O(n log n) time If n = 2 return( a1 > a2, min(a1,a2); max(a1,a2)) aL = a1,…, an/2 aR = an/2+1,…, an O(n) (cL, aL) = MergeSortCount(aL, n/2) (cR, aR) = MergeSortCount(aR, n/2) Counts #crossing-inversions+ MERGE (c, a) = MERGE-COUNT(aL,aR) return (c+cL+cR,a)

  9. 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

  10. Group Talk time O(n2) time algorithm? 1-D problem in time O(n log n) ?

  11. 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

  12. A property of Euclidean distance yj d(pi,pj) = ( (xi-xj)2+(yi-yj)2)1/2 yi xj xi The distance is larger than the x or y-coord difference

  13. Rest of Today’s agenda Divide and Conquer based algorithm

  14. Dividing up P R Q First n/2 points according to the x-coord

  15. Recursively find closest pairs R Q δ = min (blue, green)

  16. An aside: maintain sorted lists Px and Pyare P sorted by x-coord and y-coord Qx, Qy, Rx, Rycan be computed from Px and Pyin O(n) time

  17. An easy case R > δ Q All “crossing” pairs have distance > δ δ = min (blue, green)

  18. Life is not so easy though R Q δ = min (blue, green)

  19. Rest of Today’s agenda Divide and Conquer based algorithm

  20. Euclid to the rescue (?) yj d(pi,pj) = ( (xi-xj)2+(yi-yj)2)1/2 yi xj xi The distance is larger than the x or y-coord difference

  21. Life is not so easy though δ δ >δ R Q > δ > δ δ = min (blue, green)

  22. All we have to do now δ δ R Q S Figure if a pair in S has distance <δ δ = min (blue, green)

  23. The algorithm so far… O(n log n) + T(n) Input: n 2-D points P = {p1,…,pn}; pi=(xi,yi) Sort P to get Px and Py O(n log n) T(< 4) = c Closest-Pair (Px, Py) T(n) = 2T(n/2) + cn If n < 4 then find closest point by brute-force Q is first half of Pxand R is the rest O(n) Compute Qx, Qy, Rxand Ry O(n) (q0,q1) = Closest-Pair (Qx, Qy) O(n log n) overall (r0,r1) = Closest-Pair (Rx, Ry) O(n) δ = min ( d(q0,q1), d(r0,r1) ) O(n) S = points (x,y) in P s.t. |x – x*| < δ return Closest-in-box (S, (q0,q1), (r0,r1)) Assume can be done in O(n)

More Related