Professional Documents
Culture Documents
Divide and Conquer - Part2
Divide and Conquer - Part2
‣ 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
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
・ b∈B
A A b
list A list B
sort A sort B
ai bj
A B
・ (a, b) a∈A b∈B
・ A B C
A B
・ (a, b) a∈A b∈B
・ A B C
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 C
x=5
inversions = 5
Merge and count demo
A B
・ (a, b) a∈A b∈B
・ A B C
sorted list C
x=4
inversions = 5
Merge and count demo
A B
・ (a, b) a∈A b∈B
・ A B C
sorted list C
x=3
inversions = 5
Merge and count demo
A B
・ (a, b) a∈A b∈B
・ A B C
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 C
x=2
inversions = 7
Merge and count demo
A B
・ (a, b) a∈A b∈B
・ A B C
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 C
x=1
inversions = 8
Merge and count demo
A B
・ (a, b) a∈A b∈B
・ A B C
sorted list C
x=0
inversions = 8
Merge and count demo
A B
・ (a, b) a∈A b∈B
・ A B C
sorted list C
x=0
inversions = 8
Merge and count demo
A B
・ (a, b) a∈A b∈B
・ A B C
sorted list C
x=0
inversions = 8
Counting inversions: divide-and-conquer algorithm implementation
L
L L
SORT-AND-COUNT(L)
_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
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
2δ
Closest pair of points: divide-and-conquer algorithm
O(n log2 n)