Proyecto ADA
Proyecto ADA
Proyecto ADA
CSE 780
1 Introduction
Given an instance x of a problem, the divide-and-conquer method works as follows.
function DAQ(x)
if x is sufficiently small or simple then
solve it directly
else
divide x into smaller subinstances x1 , . . . , xk ;
for i ← 1 to k do yi ← DAQ(xi );
combine the yi ’s to obtain a solution y for x;
return(y)
endif
It is often that x1 , . . . , xk are of the same size, say bn/bc. In that case, the time com-
plexity of DAQ can be expressed as a recurrence:
c, if n ≤ n0
T (n) =
kT (bn/bc) + f (n), if n > n0
1
2 Merge Sort
Sort an array:
procedure mergesort(A[1..n], i, j)
// Sort A[i..j]//
if i ≥ j then return
m ← (i + j) div 2
mergesort(A, i, m)
mergesort(A, m + 1, j)
merge(A, i, m, j)
end
function mergesort(i, j)
// Sort A[i..j]. Initially, Link[k] = 0, 1 ≤ k ≤ n.//
global A[1..n], Link[1..n]
if i ≥ j then return(i)
m ← (i + j) div 2
ptr1 ← mergesort(i, m)
ptr2 ← mergesort(m + 1, j)
ptr ← merge(ptr1, ptr2)
return(ptr)
end
2
3 Solving Recurrences
A function T (n) satisfies the following recurrence:
c, if n ≤ 1
T (n) =
3T (bn/4c) + n, if n > 1
where c ia a positive constant. Obtain a function g(n) such that T (n) = O(g(n)).
T (n) = n + 3T (n/4)
h i
= n + 3 n/4 + 3T (n/16)
h i
= n + 3(n/4) + 9 (n/16) + 3T (n/64)
≤ 4n + c4m
= 4n + cn
= O(n)
So, T (n) = O(n|n a power of 4). The function f (n) = n is smooth and T (n) can be
verified to be asymptotically nondecreasing. Thus, T (n) = O(n).
3
3.2 Guess and Prove
Solve
c, if n ≤ 1
T (n) =
3T (bn/4c) + n, if n > 1
• Then we prove the guess is right by showing T (n) ≤ bn for some constant b.
T (4m ) = 4m + 3T (4m−1 )
≤ (1 + 3b/4)4m
≤ b4m , if 1 ≤ b/4
4
3.3 Master’s Theorem
Definition 1 f (n) is polynomially smaller than g(n), denoted as f (n) g(n), if f (n) =
O(g(n)n− ), or f (n)n = O(g(n)), for some > 0.
Theorem 1 If T (n) = aT (n/b) + f (n), then T (n) is bounded asymptotically as follows.
1. If f (n) = O(nlogb a n− ), then T (n) = Θ(nlogb a ).
3. If f (n) ≈ nlogb a , then T (n) = Θ(f (n) log n) = Θ(nlogb a log n).
4. If f (n) ≈ nlogb a logk n, then T (n) = Θ(f (n) log n) = Θ(nlogb a logk+1 n).
Applying the Master theorem to
c, if n ≤ 1
T (n) =
3T (bn/4c) + n, if n > 1
we immediately obtain T (n) = Θ(n), as nlogb a = nlog4 3 < n.
• T (n) = T (2n/3) + 1
5
4 Strassen’s Algorithm for Matrix Multiplication
• Problem: Compute C = AB, where A and B are n × n matrices.
• The straightforward method requires Θ(n3 ) time, using the formula cij = aik bkj .
P
1≤k≤n
• Toward the end of 1960s, Strassen showed how to multiply matrices in O(nlog 7 ) =
O(n2.81 ) time. (For n = 100, n2.81 ≈ 416869.)
• The time complexity was reduced to O(n2.521813 ) in 1979, to O(n2.521801 ) in 1980, and
to O(n2.376 ) in 1986.
6
• Write ! ! !
A11 A12 B11 B12 C11 C12
A= , B= , C=
A21 A22 B21 B22 C21 C22
where each Aij , Bij , Cij is a n/2 × n/2 matrix. Then
! ! !
C11 C12 A11 A12 B11 B12
= .
C21 C22 A21 A22 B21 B22
Then !
M2 + M3 M1 + M2 + M5 + M6
C= .
M1 + M2 + M4 − M7 M1 + M2 + M4 + M5
• Time complexity:
T (n) = 7T (n/2) + Θ(n2 ).
7
5 The Closest Pair Problem
5.1 Problem Statement
Given a set of n points A = {(xi , yi ) : 1 ≤ i ≤ n} in the plane, find two points in A with
smallest distance.
Basic idea:
1. Break up A into A = B ∪ C.
5. Find a closest pair (p3 , q3 ) between B and C with distance less than δ, if such a pair
exists.
8
5.3 Main Program
Assume that the coordinates of the n points are stored in X[1..n] and Y [1..n], with (X[i], Y [i])
the ith point. For simplicity, let A[i] = (X[i], Y [i]).
Assume that the points have been sorted such that X[1] ≤ X[2] ≤ · · · ≤ X[n]
First Attempt
9
Refined Version
10
5.4 Subroutine
To find a closest pair between A[i..m] and A[m+1..j] with distance < δ, where m = (i+j)
div 2.
L1 and L2 : as shown.
We observe that:
2. For a point r in A[i..m], we need to consider only the points in A[m + 1..j] that are
inside the square of δ × δ. There are at most three such points.
3. Similarly, for a point r in A[m + 1..j], we need to consider at most three points in
A[i..m].
11
procedure Closest-Pair-Between-Two-Sets(A[i..j], ptr, δ, (p3 , q3 ))
// Find the closest pair between A[i..m] and A[m + 1..j] with dist < δ.
If there exists no such a pair, then return the dummy pair (0, n + 1)//
global X[0..n + 1], Y [0..n + 1], Link[1..n]
(p3 , q3 ) ← (0, n + 1)
b1 ← b2 ← b3 ← 0
c1 ← c2 ← c3 ← n + 1
m ← (i + j) div 2
k ← ptr
while k 6= 0 do
if |X[k] − X[m]| < δ then
case
k ≤ m: compute d ← min{dist(k, ci ) : 1 ≤ i ≤ 3};
if d < δ then update δ and (p3 , q3 );
b3 ← b2 ; b2 ← b1 ; b1 ← k
k > m: compute d ← min{dist(k, bi ) : 1 ≤ i ≤ 3};
if d < δ then update δ and (p3 , q3 );
c3 ← c2 ; c2 ← c1 ; c1 ← k
endcase
endif
k ← Link[k]
endwhile
12
6 Convex Hull
6.1 Problem Statement
• Given a set A of n points in the plane, we want to find the convex hull of A.
• The convex hull of A is the smallest convex polygon that contains all the points in A.
• Basic ideas:
13
6.3 Combine CH(B) and CH(C) to get CH(A)
1. We need to find the “upper bridge” and the “lower bridge” that connect the two
convex hulls.
14
7 Supplementary Problems
1. Why do we need to sort the given points by x at the beginning of the closest-pair
algorithm?
3. Let X[1..n] and Y [1..n] be two sets of integers, each sorted in nondecreasing order.
Write a divide-and-conquer algorithm that finds the nth smallest of the 2n combined
elements. Your algorithm must run in O(log n) time. You may assume that all the 2n
elements are distinct.
15