L4 DC en
L4 DC en
L4 DC en
APPLIED ALGORITHMS
1 Background
2 Merge Sort
3 Binary Search
3 / 35
Divide and Conquer
4 / 35
Standard divide and conquer algorithms
Quicksort
Mergesort
Karatsuba algorithm
Strassen algorithm
Many algorithms from computational geometry
I Convex hull
I Closest pair of points
5 / 35
Applications of D&C
6 / 35
Analysis of D&C
7 / 35
Time complexity
8 / 35
Time complexity
9 / 35
Decrease and conquer
10 / 35
Merge Sort
11 / 35
Merge Sort – Merge Function
Merge is the key operation in merge sort
Suppose the (sub)sequence(s) are stored in the array A. Moreover,
A[p . . . q] and A[q + 1 . . . r ] are two sorted subsequences.
MERGE(A,p,q,r) will merge the two subsequences into sorted
sequence A[p . . . r ]
MERGE(A,p,q,r) takes Θ(r − p + 1)
1 MERGE_SORT (A ,p , r ){
2 if (p < r ) {
3 q =( p + r )/2
4 MERGE_SORT (A ,p , q )
5 MERGE_SORT (A , q +1 , r )
6 MERGE (A ,p ,q , r )}
7 }
12 / 35
Analysis of Merge Sort
13 / 35
Binary Search
14 / 35
Binary search
1 bool binary_search ( const vector < int > & arr , int lo , int hi , int x ){
2 if ( lo > hi ) {
3 return false ;
4 }
5
6 int m = ( lo + hi ) / 2;
7 if ( arr [ m ] == x ) {
8 return true ;
9 } else if ( x < arr [ m ]) {
10 return binary_search ( arr , lo , m - 1 , x );
11 } else if ( x > arr [ m ]) {
12 return binary_search ( arr , m + 1 , hi , x );
13 }
14 }
15
16 binary_search ( arr , 0 , arr . size () - 1 , x );
T (n) = T (n/2) + 1
O(log n)
15 / 35
Binary search - iterative
1 bool binary_search ( const vector < int > & arr , int x ) {
2 int lo = 0 ,
3 hi = arr . size () - 1;
4
5 while ( lo <= hi ) {
6 int m = ( lo + hi ) / 2;
7 if ( arr [ m ] == x ) {
8 return true ;
9 } else if ( x < arr [ m ]) {
10 hi = m - 1;
11 } else if ( x > arr [ m ]) {
12 lo = m + 1;
13 }
14 }
15
16 return false ;
17 }
16 / 35
Binary search over integers
This might be the most well known application of binary search, but
it’s far from being the only application
More generally, we have a predicate p : {0, . . . , n − 1} → {T , F }
which has the property that if p(i) = T , then p(j) = T for all j > i
Our goal is to find the smallest index j such that p(j) = T as quickly
as possible
17 / 35
Binary search over integers
1 int lo = 0 ,
2 hi = n - 1;
3
4 while ( lo < hi ) {
5 int m = ( lo + hi ) / 2;
6
7 if ( p ( m )) {
8 hi = m ;
9 } else {
10 lo = m + 1;
11 }
12 }
13
14 if ( lo == hi && p ( lo )) {
15 printf ( " lowest index is % d \ n " , lo );
16 } else {
17 printf ( " no such index \ n " );
18 }
18 / 35
Binary search over integers
1 bool p ( int i ) {
2 return arr [ i ] >= x ;
3 }
19 / 35
Binary search over reals
An even more general version of binary search is over the real numbers
We have a predicate p : [lo, hi] → {T , F } which has the property that
if p(i) = T , then p(j) = T for all j > i
Our goal is to find the smallest real number j such that p(j) = T as
quickly as possible
Since we’re working with real numbers (hypothetically), our [lo, hi]
can be halved infinitely many times without ever becoming a single
real number
Instead it will suffice to find a real number j 0 that is very close to the
correct answer j, say not further than EPS = 2−30 away
We can do this in O(log( hi−lo
EPS )) time in a similar way as when we
were binary searching an array
20 / 35
Binary search over reals
1 double EPS = 1e -10 ,
2 lo = -1000.0 ,
3 hi = 1000.0;
4
8 if ( p ( mid )) {
9 hi = mid ;
10 } else {
11 lo = mid ;
12 }
13 }
14
21 / 35
Binary search over reals
1 bool p ( double j ) {
2 return j * j >= x ;
3 }
1 bool p ( double x ) {
2 return f ( x ) >= 0.0;
3 }
22 / 35
Practicing problem
Pie
23 / 35
Binary search the answer
24 / 35
Other types of divide and conquer
25 / 35
Binary exponentiation
7 return res ;
8 }
26 / 35
Binary exponentiation
27 / 35
Binary exponentiation
28 / 35
Binary exponentiation
28 / 35
Binary exponentiation
28 / 35
Binary exponentiation
28 / 35
Binary exponentiation
What about the third identity?
I n/2 is not an integer when n is odd, so let’s only use it when n is even
29 / 35
Binary exponentiation
What about the third identity?
I n/2 is not an integer when n is odd, so let’s only use it when n is even
29 / 35
Binary exponentiation
What about the third identity?
I n/2 is not an integer when n is odd, so let’s only use it when n is even
29 / 35
Binary exponentiation
What about the third identity?
I n/2 is not an integer when n is odd, so let’s only use it when n is even
29 / 35
Binary exponentiation
30 / 35
Fibonacci words
31 / 35
Fibonacci words
Let’s try starting with a pair of strings, and let + denote string
concatenation:
I g1 = A
I g2 = B
I gn = gn−2 + gn−1
32 / 35
Fibonacci words
How long is gn ?
I len(g1 ) = 1
I len(g2 ) = 1
I len(gn ) = len(gn−2 ) + len(gn−1 )
Looks familiar?
len(gn ) = fibn
34 / 35
Fibonacci words
34 / 35
Fibonacci words
34 / 35
Practicing problem
Fibonacci Words
35 / 35