2 Notes
2 Notes
2 Notes
CAI Leizhen
CSE-CUHK-HK-CHN
1 Complexity
1. Length of input (input size): Usually some natural measures, such as O(lg n) for integer
n, the number of items in a set, and O(m + n) for a graph with n vertices and m edges.
2. Basic step: We assume that each elementary operation (such as addition, subtraction,
multiplication, division, Boolean operations, comparisons, assignment and build-in func-
tions) takes a constant amount of time.
For this assumption, operands must be of reasonable size.
3. Time complexity T (n) of algorithm A: Among all legal inputs i of length n, the maximum
amount of time (in big-O notation in terms of the number of basic steps as a function of
n) that A takes to produce the required output f Π (i).
4. The algorithm Fibonacci(n) uses Θ(n) elementary operations, but the operands i and j
have length Θ(k), therefore each subtraction and addition takes Θ(k), instead of O(1)
time, and the algorithm takes Θ(n2 ) time.
2 Divide-and-conquer
1
CSCI3160-18F Design and Analysis of Algorithms 2
where a is the number of subinstances, n/b the size of subinstances and f (n) the time
for Divide and Combine.
5. Master Theorem for T (n) ≤ aT (n/b) + O(n d ) with constants a ≥ 1, b > 1 and d ≥ 0:
d
O(n )
if d > log b a
T (n) = O(nd log n) if d = log b a
O(nlogb a )
if d < log b a.
Note that the master theorem covers most common cases for T (n) ≤ aT (n/b) + f (n).
7. Multiplication (Karatsuba and Ofman (1962)): O(n 1.59 ) to compute the product of two
n-digit integers.
Let x and y be two n-digit integers (for convenience, we may assume that n is a power
of 2). We split x (y respectively) into left and right halves x 0 and x1 (y0 and y1 ). Then
n n1.59 n2 /n1.59
10 39 2.5
102 1514 6.6
103 58884 17.0
104 2290868 43.7
10. Find the skyline of a set of n rectangular buildings sharing a common horizon.
Building Bi is represented by a triple (li , ri , hi ), where li and ri , resp., are left and right
x-coordinates of the building, and h i the height of the building.
The skyline is represented by a sequence of pairs (x i , hi ), arranged with increasing x-
coordinators xi , such that the height in [xi , xi+1 ] is hi .
Divide: Evenly divide all buildings into two groups B and B 0 .
Conquer: Recursively find skylines S and S 0 of B and B 0 respectively.
Combine: Merge S and S 0 into a single skyline.
The Combine step is similar to merge sort and takes O(n) time. Total time T (n) =
2T (n/2) + O(n), which yields T (n) = O(n log n).
11. Find a closest pair of points among n points P in the plane: O(n log n).
Divide: Use a vertical line L to divide P “evenly” into two sets P l and Pr .
Conquer: Recursively find closest pairs p l and pr , respectively, in Pl and Pr and let
δ = min{dist(pl ), dist(pr )}.
Combine: Find a closet pair pL that consists of one point in the δ-wide strip S l just to
the left of L and one point in the δ-wide strip S r just to the right of L. One of pl , pr and
pL is a solution. Why does this step take O(n) time?
Consider an arbitrary point p in Sl . With the fact that pairwise distance for points inside
Sr is at least δ, how many points are there inside S r that are not below p and at distance
less than δ from p? The answer is at most 4. Therefore we need only consider 4 points
in Sr for each point in Sl , and similar for each point in Sr , which gives us O(n) time for
the Combine step. The recurrence T (n) = 2T (n/2) + O(n) yields T (n) = O(n log n).