Professional Documents
Culture Documents
n ! ≤ ` ≤ 2h
h ≥ log(n!)
So,
h = Ω(n log n)
Proof: The O(n lg n) upper bounds on the running times for heapsort
and merge sort match the Ω(n lg n) worst-case lower bound.
COUNTING SORT
Idea:
1. Count the number of occurrences of each key in the input array A
2. Determine the accumulated sum of the count
If C[i] is the count of occurrences of key i, then the corresponding
accumulated sum (from left to right) is
Claim. B[i] is the position in the output array in which the last
occurrence of key i must be placed.
3. Use a right to left scan of the input to place each input entry in its
correct position in the output
Example:
Pseudocode:
COUNTING-SORT(A, n, k)
1 let B[1 : n] and C[0 : k] be new arrays
2 for i ← 0 to k // Set counter to 0
3 C [i ] ← 0
4 for j ← 1 to n // Count the keys
5 C[A[j]] ← C[A[j]] + 1
6 for i ← 1 to k // Compute the accumulated sum
7 C [i ] ← C [i ] + C [i − 1 ]
8 for j ← n downto 1 // Copy input to output from left to right
9 B[C[A[j]]] ← A[j]
10 C[A[j]] ← C[A[j]] − 1 // decrease accumulated counter
11 return B
Lemma
The running time of Counting Sort is Θ(n + k).
Counting Sort is stable.
RADIX SORT
Idea:
I Input keys consist of digits (or columns, or fields)
I The input is sorted according to each of the digits, from least to
most significative.
Example:
Pseudocode:
RADIX-SORT(A, n, d)
1 for i ← 1 to d
2 Use a stable sort to sort array A[1 : n] on digit i
Lemma
Given n d-digit numbers in which each digit can take on up to k
possible values, RADIX-SORT correctly sorts these numbers in
Θ(d(n + k)) time if the stable sort it uses takes Θ(n + k) time.
Lemma
Given n b-bit numbers and any positive integer r ≤ b, RADIX-SORT
correctly sorts these numbers in Θ ((b/r ) (n + 2r )) time if the stable
sort it uses takes Θ(n + k) time for inputs in the range 0 to k.
Proof:
I For a value r ≤ b, view each key as having d = db/r e digits of r bits
each.
I Each digit is an integer in the range 0 to 2r − 1, so that we can use
counting sort with k = 2r − 1.
I (For example, we can view a 32-bit word as having four 8-bit digits,
so that b = 32, r = 8, k = 2r − 1 = 255, and d = b/r = 4.)
I Each pass of counting sort takes Θ(n + k) = Θ (n + 2r ) time and
there are d passes, for a total running time of
Θ (d (n + 2r )) = Θ ((b/r ) (n + 2r )) .
How to chose r ?:
I Given n and b, what value of r ≤ b minimizes (b/r ) (n + 2r ) ?
I As r increases, the factor b/r decreases, but 2r increases.
For b < blg nc:
Then r ≤ b implies (n + 2r ) = Θ(n).
Thus, choosing r = b yields a running time of
(b/b) n + 2b = Θ(n),
Outline:
SELECT(A, i) : where n = |A|
1. Divide the n elements of A into n5 groups of 5 elements each.
7n
Claim 1: The recursion in step 5 is on at most elements
10
Proof. In the figure, nodes are entries, groups correspond to columns,
red nodes are the medians of the groups and x is the median of the
medians. The yellow nodes are all ≥ x and the blue ones are ≤ x (x is
both blue and yellow). The yellow region includes 3 elements in at
least g/2 columns, so it has ≥ 3g/2 elements/nodes. The same is true
for the blue region. Therefore, either of the two recursions in step 5
includes at most
3g 7g
5g − =
2 2
elements. Since g ≤ n/5, then either of the two recursions in step 5
includes at most
7 n 7n
· =
2 5 10
elements.
Claim 2: SELECT finds the i th order statistic of A in O(n) worst-case
time.
Proof. We must evaluate the recurrence T (n) for SELECT.
n steps that take O(n) time.
Steps 1, 2, and 4 are non-recursive
Step 3 is a recursive call over 5 elements which takes time
l n m
T .
5
7n
Step 3 is a recursive call over elements which takes time
10
7n
T .
10
Thus,
l n m 7n
T (n) ≤ T +T + Θ(n).
5 10
T (n) ≤ c · n (?)
for n ≥ 1.
I For 1 ≤ n < 20 this method requires O(1) time, so certainly this
time satisfies T (n) ≤ c0 n (for 1 ≤ n < 20) for an appropriate c0 .
I We take 1 ≤ n < 20 as the induction basis and show (?) for n > 20
(using the induction hypothesis).
I Substituting the inductive hypothesis into the recurrence gives:
lnm 7n
T (n) ≤ c +c + an
5 10
cn 7cn
≤ +c+ + an
5 10
9cn
= + c + an
10
cn
= cn + − + c + an
10
This is ≤ cn if
cn
− + c + an ≤ 0,
10
which holds as long as
n
c ≥ 10a .
n − 10
Because n ≥ 20 then
n/(n − 10) ≤ 2,
Lemma
SELECT runs in O(n) time.
MATRIX MUTIPLICATION
T (n) = 8T (n/2) + O n2
T (n) = 7T (n/2) + O n2 ,
Divide-and-Conquer Solution
I To obtain an O(n log n) time, the divide will not be arbitrary, rather,
the set of points is split into two halves by a line (vertical for simplicity).
I The outline is
CLOSEST-PAIR-DC (P)
Partition P with a vertical halving line ` into
points to the left Q and to the right R
(q, q0 ) ← CLOSEST-PAIR-DC(Q)
(r , r 0 ) ← CLOSEST-PAIR-DC(R)
(s, s0 ) ← CP-MERGE(Q, R)
return closest pair among (q, q0 ), (r , r 0 ), (s, s0 )
From the recursion, we know closest pairs with both points on the left
and on the right of `. It remains to check for a closest pair with one
point on the left and one on the right. In principle, we would have to
check every such pair, and that would be very time consuming.
However, it is observed that:
(i) Only points within a strip of bounded by lines `− and `+ parallel
to ` and at distance δ on the left and right need to be considered:
a point outside this strip would be at least distance δ away from a
point on the opposite side. Let Pstrip denote the subset of P in this
strip.
ℓ− ℓ ℓ+
ℓ− ℓ ℓ+
PL PR
δ δ δ
(ii) Each point in this strip only need checked against four other
points, that can be easily determined:
To see this, consider the lowest point q of a possible closest pair.
Suppose q is on the right.
Then a candidate p to be closest to q = (q1 , q2 ) must lie in the
square determined by the lines `− , `, x2 = q2 and x2 = q2 + δ .
This square contains at most 4 points: if there are more than 4,
then there are 2 in one of its 4 “subsquares” (of size δ/2 × δ/2) and
so the distance between them is at most
q √
(δ/2)2 + (δ/2)2 = δ/ 2 < δ,
a contradiction.
Pstrip is computed by simply scanning P and selecting the points wihtin
distance δ from `. If Pstrip is sorted in increasing order of x2 -coordinate,
then (ii) implies that each point in Pstrip only need to be considered to
the next 7 points in that list (at most 4 points on the opposite side are
relevant, but other 3 on the same side may appear between them in
the list).
I The final algorithm sorts the initial set P of points according to the
x1 and x2 coordinates. These sorted sets are denoted by P1 and P2 .
These sets can be stored in arrays, but it seems more convenient to use
lists. The not so detailed pseudocode below is not precise about how
P1 , P2 are stored.
I Below, CLOSEST-PAIR(P) simply sorts P according to x1 and x2 to
obtain P1 and P2 and then calls CLOSEST-PAIR-REC(P1 , P2 ).
Pseudocode:
CLOSEST-PAIR(P)
1 Construct P1 and P2 in O(n log n) time
2 return CLOSEST-PAIR-REC(P1 , P2 )
CLOSEST-PAIR-REC (P1 , P2 )
if |P| ≤ 3
1 find closest pair by measuring all pairwise distances
2 Determine halving line ` (using the sorting by x1 ) and the sets Q, R
3 Construct the sorted versions Q1 , Q2 , R1 , R2 of Q, P
from the sorted sets P1 , P2
4 (q, q0 ) ← CLOSEST-PAIR-REC (Q1 , Q2 )
5 (r , r 0 ) ← CLOSEST-PAIR-REC (R1 , R2 )
6 δ ← mı́n (d (q, q0 ) , d (r , r 0 ))
// Merge
7 construct Pstrip = points in P within distance δ of ` sorted by x2
by scanning P2
8 for each point x ∈ Pstrip
9 compute distance from x to each of next 7 points in Pstrip
10 and let s, s0 be pair achieving minimum over all of these pairs
11 return closest pair among (q, q0 ), (r , r 0 ), (s, s0 )
MASTER THEOREM
nlogb a
T (n) = Θ nlogb a .
logb a k
2. If there exists a constant k ≥ 0 such that f (n) = Θ n lg n ,
then
logb a k+1
T (n) = Θ n lg n
3. If there exists a constant > 0 such that f (n) = Ω nlogb a+ , and if
af (n/b) ≤ cf (n)
The cost of each leaf is T (1) = B, and each leaf is at depth logb n, since
n/blogb n = 1.
There are alogb n = nlogb a leaves in the tree.
We can obtain the lemma by summing the costs of the nodes at each
depth in the tree.
In the underlying divide-and-conquer algorithm, the sum represents
the costs of dividing problems into subproblems and then
recombining the subproblems.
The cost of all the leaves, which isthe cost of doing all nlogb a
subproblems of size 1, is Θ nlogb a .
The next lemma provides asymptotic bounds on the summation’s
growth.
Lemma
Let a ≥ 1 and b > 1 be constants, and let f (n) be a nonnegative
function defined on exact powers of b. A function g(n) defined over
exact powers of b by
logb n−1
X
aj f n/bj
g(n) =
j =0
g(n) = O nlogb a .
g(n) = Θ nlogb a lg n .
In terms of the recursion tree, the three cases of the master theorem
correspond to cases in which the total cost of the tree is
(1) the cost increases with the level geometrically and is dominated
by the costs in the leaves,
(2) the cost is equal over the levels of the tree, or
(3) the cost decreases with the level geometrically and is dominated
by the cost of the root.
Proof.
logb a− j
Case
1: We have f
(n ) = O n , which implies that f n / b =
log a−
O n/bj b
. Substituting into he sum yields
logb n−1 n logb a−
X
g(n) = O aj .
j =0
bj
logb n−1 logb n−1 n logb a
X X
j j j
g(n) = a f n/b = Θ a
j =0 j =0
bj
logb n−1
X a j
= Θ nlogb a
j =0
blogb a
logb n−1
X
logb a
= Θ n 1
j =0
= Θ nlogb a logb n
= Θ nlogb a lg n .
Case 3: Since f (n) appears in the defining sum of g(n), we have that
1 − b− logb n
= nlogb a−
1 − b−
1
≤ nlogb a
b −1
= O(nlogb a )
It is assumed that af (n/b) ≤ cf (n) for some constant c < 1 and all
sufficiently large n.
Form this condition, f (n/b) ≤ (c/a)f (n) and iterating j times results in
f n/bj ≤ (c/a)j f (n) or, equivalently,
aj f n/bj ≤ cj f (n)