17
Lecture 34 CSE 331 Nov 19, 2010

Lecture 34 CSE 331 Nov 19, 2010. HW 9 due today Q1 in one pile and Q 2+3 in another I will not take any HW after 1:15pm

  • View
    213

  • Download
    0

Embed Size (px)

Citation preview

Lecture 34

CSE 331Nov 19, 2010

HW 9 due today

Q1 in one pile and Q 2+3 in another

I will not take any HW after 1:15pm

No new HW out this week

Solutions to HW 9 at the end of the lecture

Graded HW 8

Individual pickups from next week

Jeff: Recitations and Wed office hours

Alex: Tuesday and Thursday office hours

My office hour today

Canceled after 2:35 pm

Next Monday’s lecture

I’ll be out of town: no office hours either

Jeff will teach the lecture

Last plug for Feedback Forms

Link for the survey on the blog

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

Solve the stronger problem of counting inversions + sorting

Solve the stronger problem of counting inversions + sorting

Three kinds of inversion

10 7 21 20100 1

Non-crossing inversions are counted recursively

Mergesort-Count algorithmInput: a1, a2, …, an Output: Numbers in sorted order+ #inversion

MergeSortCount( a, n )

If n = 2 return the order ( a1 > a2, min(a1,a2); max(a1,a2))

aL = a1,…, an/2 aR = an/2+1,…, an

return (c+cL+cR,a)

(cL, aL) = MergeSortCount(aL, n/2)

(cR, aR) = MergeSortCount(aR, n/2)

(c, a) = MERGE-COUNT(aL,aR) Counts #crossing-inversions+ MERGE

Counts #crossing-inversions+ MERGE

O(n)O(n)

T(2) = c

T(n) = 2T(n/2) + cn

O(n log n) timeO(n log n) time

HW 9 due today

Q1 in one pile and Q 2+3 in another

I will not take any HW after 1:15pm

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)

Output: Points p and q that are closest

d(pi,pj) = ( (xi-xj)2+(yi-yj)2)1/2

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