Algorithm Inversion Counting Lecture
Algorithm Inversion Counting Lecture
Greedy Recap
Greedy algorithms are usually very natural. Many problems have nice greedy solutions:
1 2 3 4 5 6 7 8 9
Topological Sorting (ch. 3) Interval Scheduling (4.1) Interval Partitioning (4.1) Minimizing Lateness (4.2) Optimal Scheduling (4.3) Shortest Paths (Dijkstras) (4.4) Minimum Spanning Tree (4.5) Maximum Separation Clustering (4.7) Matroids: Max-Weight
Greedy Recap, 2
Weve seen some general patterns for algorithms:
1 2
Greedy stays ahead: any choice the OPT can make, the greedy can make and will Exchange: we can transform an OPT solution into greedy with series of small changes. Matroid: Hereditary Subset System with Augmentation Property
k <n
You use this box on some subset of the input items to get
partial answers
You combine these partial answers to get the full answer.
But: you construct the box by recursively applying the same idea until the problem is small enough to be solved by brute force.
Merge Sort
MergeSort(L): if |L| = 2: return [min(L), max(L)] else: L1 = MergeSort(L[0, |L|/2]) L2 = MergeSort(L[|L|/2+1, |L|-1]) return Combine(L1, L2)
In practice, you sort in-place rather than making new lists. Combine(L1,L2) walks down the sorted lists putting the
To Solve a Recurrence
Given a recurrence such as T (n) 2T (n/2) + cn, we want a simple upper bound on the total running time. Two common ways to solve such a recurrence:
1
Unroll the recurrence and see what the pattern is. Typically, youll draw the recursion tree. Guess an answer and prove that its right.
Solving Recurrences
Draw the rst few levels of the tree. Write the amount of work done at each level in terms of the level. Figure out the height of the tree. Sum over all levels of the tree. T (n) 2T (n/2) + cn Each level is cn. There are log n levels, so T (n) is O(n log n).
n/4
n/2
n/2
n/4
n/4
n/4
Substitution Method
Substitution method is based on induction. We:
1 2 3
Show T (k) f (k) for some small k. Assume T (k) f (k) for all k < n. Show T (n) f (n).
T (n) 2T (n/2) + cn Base Case: 2c log 2 = 2c T (2) Induction Step: T (n) 2T (n/2) + cn 2c(n/2) log(n/2) + cn = cn[(log n) 1] + cn = cn log n
Counting Inversions
Comparing Rankings
Suppose two customers rank a list of movies.
similar
more dierent
A measure of distance
A measure of distance
Whats a good measure of how dissimilar two rankings are? We can count the number of inversions:
Assume one of the rankings is 1, 2, 3, . . . , n. Denote the other ranking by a1 , a2 , . . . , an . An inversion is a pair (i, j) such that i < j but aj < ai .
Two identical rankings have no inversions. How many inversions do opposite rankings have?
A measure of distance
Whats a good measure of how dissimilar two rankings are? We can count the number of inversions:
Assume one of the rankings is 1, 2, 3, . . . , n. Denote the other ranking by a1 , a2 , . . . , an . An inversion is a pair (i, j) such that i < j but aj < ai .
Two identical rankings have no inversions. How many inversions do opposite rankings have?
n 2
Count the number of inversions in the sequence a1 , . . . , an . Suppose I told you the number of inversions in the rst half of the list and in the second half of the list:
an/2+1, Inv2
...,
an
Half-Crossing Inversions
an/2+1,
...,
an
ai
>
aj
The crux is that we have to count these kinds of inversion in O(n) time.
ai
bj
min
Merge-and-Count
MergeAndCount(SortedList A, SortedList B): a = b = CrossInvCount = 0 OutList = empty list While a < |A| and b < |B|: // not at end of a list next = min(A[a], B[b]) OutList.append(next) If B[b] == next: b = b + 1 CrossInvCount += |A| - a //inc by # left in A Else a = a + 1 EndWhile Append the non-empty list to OutList Return CrossInvCount and OutList
Sorted!
Note that MergeAndCount will produce a sorted list as well as the number of cross inversions.
SortAndCount
SortAndCount(List L): If |L| == 1: Return 0 A, B = first & second halves of L invA, SortedA = SortAndCount(A) invB, SortedB = SortAndCount(B) crossInv, SortedL = MergeAndSort(SortedA, SortedB) Return invA + invB + crossInv and SortedL
Algorithm Schematic
Divide it into 2 parts a1, ..., an/2 an/2+1, ..., an
Compute the answer (and maybe some additional info) on each part separately
Recursive Box
Recursive Box
Inv1
Inv2
Merge
Inv1 + Inv2
+ inversions that cross between the rst half and the second half
Running time?
Running time?
Whats the running time of SortAndCount? Break the problem into two halves. Merge takes O(n) time. T (n) 2T (n/2) + cn = Total running time is O(n log n).