You are on page 1of 33

5.

D IVIDE AND C ONQUER

‣ mergesort
‣ counting inversions
‣ randomized quicksort
‣ median and selection
‣ closest pair of points

SECTION 5.3
Counting inversions

・ n

・ 1, 2, …, n
・ a1, a2, …, an
・ i j i < j ai > aj

A B C D E

me

you
2 inversions: 3-2, 4-2

Θ(n2)
Counting inversions: applications







Counting inversions: divide-and-conquer

・ A B

・ (a, b) a∈A b∈B

input

count inversions in left half A count inversions in right half B

5-4 6-3 9-3 9-7

count inversions (a, b) with a ∈ A and b ∈ B

4-2 4-3 5-2 5-3 8-2 8-3 8-6 8-7 10-2 10-3 10-6 10-7 10-9

output 1 + 3 + 13 = 17
Counting inversions: how to combine two subproblems?

(a, b) a∈A b∈B


A B

・ A B
・ b∈B
A A b

list A list B

sort A sort B

binary search to count inversions (a, b) with a ∈ A and b ∈ B


Counting inversions: how to combine two subproblems?

(a, b) a∈A b∈B A B


・ A B
・ ai bj
・ ai < bj ai B
・ ai > bj bj A
・ C

count inversions (a, b) with a ∈ A and b ∈ B

ai bj

merge to form sorted list C


Merge and count demo

A B
・ (a, b) a∈A b∈B
・ A B C

sorted list A sorted list B


Merge and count demo

A B
・ (a, b) a∈A b∈B
・ A B C

sorted list A sorted list B

compare minimum entry in each list: copy 2 and add x to inversion count

sorted list C

x=5
inversions = 0
Merge and count demo

A B
・ (a, b) a∈A b∈B
・ A B C

sorted list A sorted list B

compare minimum entry in each list: copy 3 and decrement x

sorted list C

x=5
inversions = 5
Merge and count demo

A B
・ (a, b) a∈A b∈B
・ A B C

sorted list A sorted list B

compare minimum entry in each list: copy 7 and decrement x

sorted list C

x=4
inversions = 5
Merge and count demo

A B
・ (a, b) a∈A b∈B
・ A B C

sorted list A sorted list B

compare minimum entry in each list: copy 10 and decrement x

sorted list C

x=3
inversions = 5
Merge and count demo

A B
・ (a, b) a∈A b∈B
・ A B C

sorted list A sorted list B

compare minimum entry in each list: copy 11 and add x to increment count

sorted list C

x=2
inversions = 5
Merge and count demo

A B
・ (a, b) a∈A b∈B
・ A B C

sorted list A sorted list B

compare minimum entry in each list: copy 14 and decrement x

sorted list C

x=2
inversions = 7
Merge and count demo

A B
・ (a, b) a∈A b∈B
・ A B C

sorted list A sorted list B

compare minimum entry in each list: copy 16 and add x to increment count

sorted list C

x=1
inversions = 7
Merge and count demo

A B
・ (a, b) a∈A b∈B
・ A B C

sorted list A sorted list B

compare minimum entry in each list: copy 18 and decrement x

sorted list C

x=1
inversions = 8
Merge and count demo

A B
・ (a, b) a∈A b∈B
・ A B C

sorted list A sorted list B

list A exhausted: copy 20

sorted list C

x=0
inversions = 8
Merge and count demo

A B
・ (a, b) a∈A b∈B
・ A B C

sorted list A sorted list B

list A exhausted: copy 23

sorted list C

x=0
inversions = 8
Merge and count demo

A B
・ (a, b) a∈A b∈B
・ A B C

sorted list A sorted list B

done: return 8 inversions

sorted list C

x=0
inversions = 8
Counting inversions: divide-and-conquer algorithm implementation

L
L L

SORT-AND-COUNT(L)
_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

IF (list L has one element)


RETURN (0, L).

Divide the list into two halves A and B.


(rA, A) ← SORT-AND-COUNT(A). T (n / 2)
(rB, B) ← SORT-AND-COUNT(B). T (n / 2)
(rAB, L) ← MERGE-AND-COUNT(A, B). Θ(n)

RETURN (rA + rB + rAB, L).


_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
Counting inversions: divide-and-conquer algorithm analysis

n O(n log n)

T(n)
5. D IVIDE AND C ONQUER

‣ mergesort
‣ counting inversions
‣ randomized quicksort
‣ median and selection
‣ closest pair of points

SECTION 5.4
Closest pair of points



Closest pair of points

Θ(n2)

O(n log n)

x
Closest pair of points: first attempt

・ x
・ y
Closest pair of points: first attempt

・ x
・ y
Closest pair of points: second attempt
Closest pair of points: second attempt

n/4
Closest pair of points: divide-and-conquer algorithm

・ L n/2


・ Θ(n2)

L
How to find closest pair with one point in each side?

d
・ d L

δ = min(12, 21)
How to find closest pair with one point in each side?

d
・ d L
・ 2d y

s9
L

s8
s7
s6
s5
s4

δ = min(12, 21)
s3

s2
s1
How to find closest pair with one point in each side?

si 2δ ith y

⎜j – i⎟ > 7 L
si sj δ

・ 2δ δ R sj
y y si
・ si sj
R ½δ
R ≥ δ
δ
・ R
½δ

・ si R ▪
si


Closest pair of points: divide-and-conquer algorithm

CLOSEST-PAIR(p1, p2, …, pn)


_______________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
__________________________________________

Compute vertical line L such that half the points O(n)


are on each side of the line.
δ1 ← CLOSEST-PAIR(points in left half). T(⎣n / 2⎦)
δ2 ← CLOSEST-PAIR(points in right half). T(⎡n / 2⎤)
δ ← min { δ1 , δ2 }.
A ← list of all points closer than δ to line L. O(n)
Sort points in A by y-coordinate. O(n log n)
Scan points in A in y-order and compare distance
between each point and next 7 neighbors. O(n)
If any of these distances is less than δ, update δ.
RETURN δ.
_______________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
__________________________________________
Closest pair of points: analysis

O(n log2 n)

You might also like