Professional Documents
Culture Documents
Chapter 04
Chapter 04
July 8, 2016
1 / 38
1 Background
2 Merge Sort
3 Binary Search
2 / 38
Divide and Conquer
3 / 38
Standard divide and conquer algorithms
Quicksort
Mergesort
Karatsuba algorithm
Strassen algorithm
Many algorithms from computational geometry
I Convex hull
I Closest pair of points
4 / 38
Applications of D&C
Solving difficult problems: breaking the problem into sub-problems, solving
the trivial cases and combining sub-problems to the original problem
Parallelism: naturally adapted for execution in multi-processor machines,
especially shared-memory systems where the communication of data
between processors does not need to be planned in advance, because distinct
sub-problems can be executed on different processors
Memory access: naturally tend to make efficient use of memory caches.
Once a sub-problem is small enough, it and all its sub-problems can, in
principle, be solved within the cache, without accessing the slower main
memory
Roundoff control: may yield more accurate results than a superficially
equivalent iterative method with rounded arithmetic. For example, one can
add N numbers either by a simple loop that adds each datum to a single
variable, or by a D&C algorithm called pairwise summation that breaks the
data set into two halves, recursively computes the sum of each half, and
then adds the two sums. While the second method performs the same
number of additions as the first, and pays the overhead of the recursive
calls, it is usually more accurate
5 / 38
Analysis of D&C
6 / 38
Time complexity
7 / 38
Time complexity
8 / 38
Decrease and conquer
9 / 38
1 Background
2 Merge Sort
3 Binary Search
10 / 38
Merge Sort
11 / 38
Merge Sort – Merge Function
13 / 38
1 Background
2 Merge Sort
3 Binary Search
14 / 38
Binary Search
15 / 38
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)
16 / 38
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 }
17 / 38
1 Background
2 Merge Sort
3 Binary Search
18 / 38
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
19 / 38
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 }
20 / 38
Binary search over integers
1 bool p ( int i ) {
2 return arr [ i ] >= x ;
3 }
21 / 38
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
22 / 38
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
23 / 38
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 }
24 / 38
Practicing problem
Pie
25 / 38
Binary search the answer
26 / 38
1 Background
2 Merge Sort
3 Binary Search
27 / 38
Other types of divide and conquer
28 / 38
Binary exponentiation
7 return res ;
8 }
29 / 38
Binary exponentiation
30 / 38
Binary exponentiation
31 / 38
Binary exponentiation
31 / 38
Binary exponentiation
31 / 38
Binary exponentiation
31 / 38
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
32 / 38
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
32 / 38
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
32 / 38
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
33 / 38
Fibonacci words
34 / 38
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
35 / 38
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
37 / 38
Fibonacci words
37 / 38
Fibonacci words
37 / 38
Practicing problem
Fibonacci Words
38 / 38