Counting Inversion
Counting Inversion
1
A Useful Recurrence Relation
Mergesort recurrence.
0 if n 1
T(n) T n /2 T n /2 n otherwise
merging
solve left half solve right half
Solution. T(n) = O(n log2 n).
2
Proof by Recursion Tree
0 if n 1
T(n) 2T(n /2) n otherwise
sorting both halves merging
T(n) n
...
T(2) T(2) T(2) T(2) T(2) T(2) T(2) T(2) n/2 (2)
n log2n
3
5.3 Counting Inversions
Counting Inversions
Songs
A B C D E
Inversions
Me 1 2 3 4 5
3-2, 4-2
You 1 3 4 2 5
5
Applications
Applications.
Voting theory.
Collaborative filtering.
Measuring the "sortedness" of an array.
Sensitivity analysis of Google's ranking function.
Rank aggregation for meta-searching on the Web.
Nonparametric statistics (e.g., Kendall's Tau distance).
6
Counting Inversions: Divide-and-Conquer
Divide-and-conquer.
1 5 4 8 10 2 6 9 12 11 3 7
7
Counting Inversions: Divide-and-Conquer
Divide-and-conquer.
Divide: separate list into two pieces.
Divide: O(1).
1 5 4 8 10 2 6 9 12 11 3 7
1 5 4 8 10 2 6 9 12 11 3 7
8
Counting Inversions: Divide-and-Conquer
Divide-and-conquer.
Divide: separate list into two pieces.
Conquer: recursively count inversions in each half.
Divide: O(1).
1 5 4 8 10 2 6 9 12 11 3 7
1 5 4 8 10 2 6 9 12 11 3 7 Conquer: 2T(n / 2)
5-4, 5-2, 4-2, 8-2, 10-2 6-3, 9-3, 9-7, 12-3, 12-7, 12-11, 11-3, 11-7
9
Counting Inversions: Divide-and-Conquer
Divide-and-conquer.
Divide: separate list into two pieces.
Conquer: recursively count inversions in each half.
Combine: count inversions where ai and aj are in different halves,
and return sum of three quantities.
Divide: O(1).
1 5 4 8 10 2 6 9 12 11 3 7
1 5 4 8 10 2 6 9 12 11 3 7 Conquer: 2T(n / 2)
9 blue-green inversions
Combine: ???
5-3, 4-3, 8-6, 8-3, 8-7, 10-6, 10-9, 10-3, 10-7
Total = 5 + 8 + 9 = 22.
10
Counting Inversions: Combine
3 7 10 14 18 19 2 11 16 17 23 25
6 3 2 2 0 0
2 3 7 10 11 14 16 17 18 19 23 25 Merge: O(n)
11
Merge and Count
i=6
auxiliary array
Total:
12
Merge and Count
i=6
2 auxiliary array
Total: 6
13
Merge and Count
i=6
2 auxiliary array
Total: 6
14
Merge and Count
i=6
2 3 auxiliary array
Total: 6
15
Merge and Count
i=5
2 3 auxiliary array
Total: 6
16
Merge and Count
i=5
2 3 7 auxiliary array
Total: 6
17
Merge and Count
i=4
2 3 7 auxiliary array
Total: 6
18
Merge and Count
i=4
2 3 7 10 auxiliary array
Total: 6
19
Merge and Count
i=3
2 3 7 10 auxiliary array
Total: 6
20
Merge and Count
i=3
2 3 7 10 11 auxiliary array
Total: 6 + 3
21
Merge and Count
i=3
2 3 7 10 11 auxiliary array
Total: 6 + 3
22
Merge and Count
i=3
2 3 7 10 11 14 auxiliary array
Total: 6 + 3
23
Merge and Count
i=2
2 3 7 10 11 14 auxiliary array
Total: 6 + 3
24
Merge and Count
i=2
2 3 7 10 11 14 16 auxiliary array
Total: 6 + 3 + 2
25
Merge and Count
i=2
2 3 7 10 11 14 16 auxiliary array
Total: 6 + 3 + 2
26
Merge and Count
i=2
2 3 7 10 11 14 16 17 auxiliary array
Total: 6 + 3 + 2 + 2
27
Merge and Count
i=2
2 3 7 10 11 14 16 17 auxiliary array
Total: 6 + 3 + 2 + 2
28
Merge and Count
i=2
2 3 7 10 11 14 16 17 18 auxiliary array
Total: 6 + 3 + 2 + 2
29
Merge and Count
i=1
2 3 7 10 11 14 16 17 18 auxiliary array
Total: 6 + 3 + 2 + 2
30
Merge and Count
i=1
2 3 7 10 11 14 16 17 18 19 auxiliary array
Total: 6 + 3 + 2 + 2
31
Merge and Count
2 3 7 10 11 14 16 17 18 19 auxiliary array
Total: 6 + 3 + 2 + 2
32
Merge and Count
i=0
2 3 7 10 11 14 16 17 18 19 23 auxiliary array
Total: 6 + 3 + 2 + 2 + 0
33
Merge and Count
i=0
2 3 7 10 11 14 16 17 18 19 23 auxiliary array
Total: 6 + 3 + 2 + 2 + 0
34
Merge and Count
i=0
2 3 7 10 11 14 16 17 18 19 23 25 auxiliary array
Total: 6 + 3 + 2 + 2 + 0 + 0
35
Merge and Count
i=0
2 3 7 10 11 14 16 17 18 19 23 25 auxiliary array
Total: 6 + 3 + 2 + 2 + 0 + 0 = 13
36
Counting Inversions: Implementation
Sort-and-Count(L) {
if list L has one element
return 0 and the list L
37
5.4 Closest Pair of Points
Closest Pair of Points
Closest pair. Given n points in the plane, find a pair with smallest
Euclidean distance between them.
Brute force. Check all pairs of points p and q with (n2) comparisons.
39
Closest Pair of Points: First Attempt
40
Closest Pair of Points: First Attempt
41
Closest Pair of Points: First Attempt
42
Closest Pair of Points
Algorithm.
Divide: draw vertical line L so that roughly ½n points on each side.
43
Closest Pair of Points
Algorithm.
Divide: draw vertical line L so that roughly ½n points on each side.
Conquer: find closest pair in each side recursively.
21
12
44
Closest Pair of Points
Algorithm.
Divide: draw vertical line L so that roughly ½n points on each side.
Conquer: find closest pair in each side recursively.
Combine: find closest pair with one point in each side. seems like (n2)
8
21
12
45
Closest Pair of Points
Find closest pair with one point in each side, assuming that distance < .
21
= min(12, 21)
12
46
Closest Pair of Points
Find closest pair with one point in each side, assuming that distance < .
Observation: only need to consider points within of line L.
21
= min(12, 21)
12
47
Closest Pair of Points
Find closest pair with one point in each side, assuming that distance < .
Observation: only need to consider points within of line L.
Sort points in 2-strip by their y coordinate.
L
7
5
4 21
= min(12, 21)
12 3
48
Closest Pair of Points
Find closest pair with one point in each side, assuming that distance < .
Observation: only need to consider points within of line L.
Sort points in 2-strip by their y coordinate.
Only check distances of those within 11 positions in sorted list!
L
7
5
4 21
= min(12, 21)
12 3
49
Closest Pair of Points
i 27
28 ½
Fact. Still true if we replace 12 with 7.
26
25
50
Closest Pair Algorithm
Closest-Pair(p1, …, pn) {
Compute separation line L such that half the points O(n log n)
are on one side and half on the other side.
1 = Closest-Pair(left half)
2T(n / 2)
2 = Closest-Pair(right half)
= min(1, 2)
return .
}
51
Closest Pair of Points: Analysis
Running time.
Q. Can we achieve O(n log n)?
52
5.5 Integer Multiplication
Integer Arithmetic
Multiply
0 0 0 0 0 0 0 0 0
1 1 0 1 0 1 0 1 0
1 1 0 1 0 1 0 1 0
1 1 1 1 1 1 0 1 1 1 0 1 0 1 0 1 0
1 1 0 1 0 1 0 1 1 1 0 1 0 1 0 1 0
+ 0 1 1 1 1 1 0 1 1 1 0 1 0 1 0 1 0
1 0 1 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0
Add 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 1 0
54
Divide-and-Conquer Multiplication: Warmup
x 2 n / 2 x1 x0
y 2 n / 2 y1 y0
xy 2 n/2
x1 x0 2 n/2
y1 y0 2 n x1 y1 2 n / 2 x1 y0 x0 y1 x0 y0
assumes n is a power of 2
55
Karatsuba Multiplication
x 2 n / 2 x1 x0
y 2 n / 2 y1 y0
xy 2 n x1 y1 2 n / 2 x1 y0 x0 y1 x0 y0
2 n x1 y1 2 n / 2 (x1 x0 ) (y1 y0 ) x1 y1 x0 y0 x0 y0
A B A C C
log 2 3
T(n) O(n ) O(n1.585 )
56
Karatsuba: Recursion Tree
0 if n 1 log 2 n
23
1 log 2 n
1
T(n)
3T(n /2) n otherwise
T(n) n
3 k
2
3 1
3nlog 2 3 2
k0 2
T(n) n
T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) 9(n/4)
... ...
... ...
57