Algorithm CH 3 Lec 1
Algorithm CH 3 Lec 1
9/2/2024
Divide and Conquer (Example)
Example 3.1: Detecting a Counterfeit Coin
You are given a bag with 16 coins and told that one of
these coins may be counterfeit. Further, you are told that
counterfeit coins are lighter than genuine ones.
9/2/2024
Strategy (Counterfeit coin detection)
1. Straight forward method:
Compare weights of coin 1 and 2 -> decide whether
any difference in weight. If found any difference, the
counterfeit coin has detected. Otherwise,
Compare weights of coin 3 and 4 -> decide whether
any difference in weight. If found any difference, the
counterfeit coin has detected. Otherwise,
Compare weights of coin 5 and 6 -> decide whether
any difference in weight. If found any difference, the
counterfeit coin has detected. Otherwise,
9/2/2024
Strategy (Counterfeit coin detection) cont.
Continuing the process can determine the existence of
counterfeit coin.
How many comparisons are there?
- At most 8 comparison.
Is it a good approach?
In this case C(n) = n/2, where n is the input size.
9/2/2024
Divide and Conquer Approach
Step 1: Divide the original instance into two or more
smaller instances. Here in this case, divide the 16 coins into
two sets A and B of 8 coins.
Step 2. Use a machine and compare the weights of two sets
A and B of coins.
Decide which set is lighter or whether both sets have
the same weight.
-The set with the lighter weight contains counterfeit
coin.
- The sets with the same weight do not contain any
counterfeit coin.
9/2/2024
Identifying the Counterfeit Coin
Divide the set with lighter weight into smaller parts of
sets each containing 4 coins.
Continue the process until the lighter set has only two
coins.
Final step: Compare the two coins and decide which
one is lighter to decide the counterfeit coin.
Result: This approach needs at most 4 comparisons.
That means, c(n) = log2n where n is the input size.
9/2/2024
Counterfeit coin (Cont.)
If n is odd (n-1) is even
Basic Idea
Keep one extra coin aside
Split (n-a) coins into two sets and put each set of coins in
the balance scale.
If the sets weigh the same, the coin put aside must be fake.
If the balance is not even, then we chose the lightest set of
coin to be the one containing fake coin.
Proceed with the same manner with the lighter set of coins.
9/2/2024
Divide and Conquer (Cont.)
9/2/2024
Control Abstruction for Divide and Conquer
Algorithm DAndC( P ) {
if Small( P ) then return S( P );
else
{
divide P into smaller instances P1, P2, …, Pk, k >= 1;
Apply DAndC to each these sub-problems;
return Combine(DAndC(P1), DAndC(P1),…, DAndC(Pk));
}
}
9/2/2024
Divide and Conquer Recurrence
For DAndC algorithms that produce the sub-problems of
the same type
In general, a problem instance of size n can be divided into
b instances of size n/b, with a of them needing to be solved.
Here, a and b are constants; a≥ 1 and b > 1.
We get the following recurrence for the running time T(n)
T(n) = T(1) for n = 1
T(n) = a T( n/b ) + f(n) for n > 1
where
n is power of b i.e. n = bk
f(n) accounts for the time spent on dividing the
problem into smaller ones and on combining their solutions.
9/2/2024
Solving Recurrence using Substitution Method
Example 3.2: Given the general recurrence for DAndC
T(n) = T(1) for n = 1
T(n) = a T( n/b ) + f(n) for n > 1
T( n) = T( n/n )+ [n - 1] 2 cn/n
T( n) = T(1)+ 2cn - 2c
T( n) = c1 + 2cn – 2c (Assuming T(1) = c1 )
Therefore T(n) = O(n)
9/2/2024
Divide-and-Conquer Examples
Binary Search
Sorting: mergesort and quicksort
Binary tree traversals
Multiplication of large integers
Matrix multiplication: Strassen’s
algorithm
Closest-pair and convex-hull algorithms
13
Binary Search
Binary Search is a searching algorithm. In each step, the algorithm
compares the input element x with the value of the middle element
in array. If the values match, return the index of middle. Otherwise,
if x is less than the middle element, then the algorithm recurs for
left side of middle element, else recurs for right side of middle
element.
Binary Search (Cont.)
Very efficient algorithm for searching in sorted array:
K
vs
A[0] . . . A[m] . . . A[n-1]
If K = A[m], stop (successful search); otherwise,
continue searching by the same method in A[0..m-1] if
K < A[m] and in A[m+1..n-1] if K > A[m]
9/2/2024
Recursive Binary Search
9/2/2024
9/2/2024
Time Anlysis of Binary Search
Finding Maximum and Minimum
Algorithm3.5Straightforward maximum and minimum
Algorithm StraightMaxMin(a, n, max, min)
//Set max to the maximum and min to the minimum of
a[l:n].
{ max := min := a[l];
for i :=2 to n do
{
if (a[i] > max) then max:=a[i];
if (a[i) < min) then min :=a[i];
}
}
9/2/2024
Maximum and Minimum (Cont.)
StraightMaxMin requires 2(n-1) element comparisons in the best,
average, and worst cases. An immediate improvement is possible by
realizing that the comparison a[i] < min is necessary only when a[i] >
max is false.
Hence we can replace the contents of the for loop by
if (a[i] > max)then max:= a[i];
else if (a[i] < min) then min :=a[i];
Now the best case occurs when the elements are in increasing order.
The number of element comparisons is n-1. The worst case occurs when
the elements are in decreasing order. In this case the number of
element comparisons is 2(n-1).
The average number of element comparisons is less than 2(n-1).On the
average, a[i] is greater than max half the time, and so the average
number of comparisons is = (n+1) + (n-1)/2 = 3(n -1)/2.
Finding Maximum and Minimum (D & C)
AlgorithmMaxMin(i, j, max, min)
// a[l:n] is a global array. Parameters i and j are integers, 1<=i<=j<=n. The effect is to set max and min to the largest
and smallest values in a[i:j] respectively.
{
if (i = j) then max := min := a[i]; // Small(P)
else if (i = j-1) then // Another case of Small(P)
{
if (a[i] < a[j]) then
max :=a[j]; min :=a[i]
else
max:=a[i]; min:=a[j];
}
else
{ // If P is not small, divide P into sub problems.
mid:=[(i+j)/2] ; // Find where to split the set.
// Solve the sub problems.
MaxMin(i,mid,max,min);
MaxMin(mid+l, j, maxl,minl);
// Combinethe solutions.
if (max < maxl) then max:= maxl;
if (min >min1) then min:=mini1
}
}
Trees of Recursive calls of MaxMIn
Suppose we simulate MaxMin on the following nine elements
a: [1] [2] [3] [4] [5] [6] [7] [8] [9]
22 13 -5 -8 15 60 17 31 47
9/2/2024
Time Analysis
9/2/2024
Time Analysis (Cont.)
Note that 3n/2 -2 is the best-, average-, and worst-case
number of comparisons when n is a power of two.
Compared with the 2(n-1) = 2n-2 comparisons for the
straightforward method, this is a saving of 25% in
comparisons.
It can be shown that no algorithm based on comparisons uses
less than 3n/2 -2 comparisons. So, in this sense, algorithm
MaxMin is optimal.
9/2/2024