0% found this document useful (0 votes)
18 views26 pages

Algorithm CH 3 Lec 1

The document discusses the Divide and Conquer algorithm design strategy, which involves breaking a problem into smaller instances, solving them recursively, and combining their solutions. An example of this strategy is detecting a counterfeit coin among 16 coins, where the approach reduces the number of comparisons needed to identify the counterfeit coin from 8 to 4. The document also covers the time analysis of various algorithms, including binary search and finding maximum and minimum values, highlighting the efficiency of the Divide and Conquer approach.

Uploaded by

Safwan Samad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views26 pages

Algorithm CH 3 Lec 1

The document discusses the Divide and Conquer algorithm design strategy, which involves breaking a problem into smaller instances, solving them recursively, and combining their solutions. An example of this strategy is detecting a counterfeit coin among 16 coins, where the approach reduces the number of comparisons needed to identify the counterfeit coin from 8 to 4. The document also covers the time analysis of various algorithms, including binary search and finding maximum and minimum values, highlighting the efficiency of the Divide and Conquer approach.

Uploaded by

Safwan Samad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Divide and Conquer

 The most-well known algorithm design strategy


 Divide instance of problem into two or more smaller
instances
 Solve smaller instances recursively
 Obtain solution to original (larger) instance by
combining these solutions

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.

Task: Determine whether the bag contains a counterfeit


coin.

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.

Now look at the another approach (Divide and Conquer)

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

Let a = 2, b = 2, T(1) = 2 and f(n) = n


T(n) = 2T( n/2) + n
= 2[2T( n/4 )+ n/2] + n
= 4T( n/4 )+ 2n
= 4[2T( n/8 )+ n/4] + 2n
= 8T( n/8 )+ 3n after i substitutions
= 2iT( n/2i )+in where log2 n ≥ i ≥ 1
for i = log2n i.e. n = 2i ,
T(n) = 2log2n T(n/ 2log2n ) + n log2n {Let y= 2 log2n , log2y = log2 2 log2n
T( n) = n T( n/n)+ n log2n log2y = log2n log2 2
T( n) = n T( 1)+ n log2n log2y = log2n
T( n) = 2n + n log2n since T( 1) =2 y = n or 2 log2n = n}
Therefore T(n) = O(n log n)
9/2/2024
Solve using Substitution method
 T(n) = T( n/2 ) + cn for n > 1 T(n) =T(1) for n = 1
T(n) = T( n/2) + cn
= [T( n/4 )+ cn/2] + cn
= T( n/4 )+ [1/2 + 1) cn
= [T( n/8 )+ cn/4] + [1/2 + 1] cn
= T( n/8 )+ [1/4+1/2 + 1] cn after k substitutions
= T( n/2k )+ [1/2k-1+1/2k-2 + … + 1/2 + 1] cn where log2n ≥ k ≥ 1
= T( n/2k )+ [1+22 + … + 2k-2 + 2k-1 ] cn / 2k-1
= T( n/2k )+ [2k - 1] cn / 2k-1 for k = log2n (i.e. n = 2k)

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

 Int BinSrch(Type a[], int l, int r, Type x) {


// Given an array a[l:r] of elements in non decreasing order, determine
//whether x is present, and if so, return j such that x == a[j]; else return 0.
if (l==r) { // If Small(P)
if (x==a[l]) return l;
else return 0; }
else { // Reduce P into a smaller sub problem.
int mid = (l+r)/2;
if (x == a[mid]) return mid;
else if (x < a[mid])
return BinSrch(a,l,mid-1,x);
else return BinSrch(a,mid+1,r,x); } }
9/2/2024
Non Recursive Bianary Search
int BinSearch(Type a[], int n, Type x)
{
// Given an array a[1:n] of elements in non-decreasing order, n>=0, determine
// whether x is present, and if so, return j such that x == a[j]; else return 0.

int low = 1, high = n;


while (low <= high){
int mid = (low + high)/2;
if (x < a[mid]) high = mid - 1;
else if (x > a[mid]) low = mid + 1;
else return(mid);
}
return(0);
} 9/2/2024
Binary Search- Three Examples

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

When n is a power of two, n = 2k for some positive integer k, then

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

You might also like