DAA Divide and Conquer
DAA Divide and Conquer
Algorithms
Unit - II
Periyar Govt. Arts College
Cuddalore
Dr. R. Bhuvaneswari
Assistant Professor
Department of Computer Science
Periyar Govt. Arts College, Cuddalore.
1 Dr. R. Bhuvaneswari
Divide and Conquer
Syllabus
UNIT-II
Divide and Conquer: General Method – Binary Search – Finding
Maximum and Minimum – Merge Sort – Greedy Algorithms:
General Method – Container Loading – Knapsack Problem.
Text Book:
Ellis Horowitz, Sartaj Sahni and Sanguthevar Rajasekaran,
Computer Algorithms C++, Second Edition, Universities Press,
2007. (For Units II to V)
General Method:
• Given a function to compute on ‘n’ inputs, divide-and-conquer
strategy suggests splitting the inputs into ‘k’ distinct subsets,
1<k≤n, yielding ‘k’ subproblems.
• These subproblems must be solved, and then a method must be
found to combine sub solutions into a solution of the whole.
• If the subproblems are still relatively large, then the divide-and-
conquer strategy can possibly be reapplied.
• For those cases the re-application of the divide-and-conquer
principle is naturally expressed by a recursive algorithm.
a problem of size n
(instance)
subproblem 1 subproblem 2
of size n/2 of size n/2
a solution to a solution to
subproblem 1 subproblem 2
a solution to
the original problem
g n n small
T n =
T n1 + T n2 + … . +T nk + f n otherwise
where
T(n) is the time for DAndC on any input of size n
g(n) is the time to compute the answer directly for small inputs
f(n) is the time for dividing P and combining the solutions to
subproblems
1 2 3 4 5 6 7 8 9 10
10 20 30 40 50 60 70 80 90 100
mid = (low+high)/2 x = 60
1.(x<a[mid] ) then
1. low = 1, high = 10 high = mid-1
mid = (1+10)/2 = 5, 60 > 50, low = 6 2.else if (x>a[mid]) then
2. low = 6, high = 10 low = mid+1
mid = (6 + 10)/2 = 8, 60 < 80, high = 7 3.else return mid;
3. low = 6, high = 7
mid = (6 + 7)/2 = 6
Periyar Govt. Arts College
7 Dr. R. Bhuvaneswari Cuddalore
Binary Search
1 2 3 4 5 6 7 8 9 10
10 20 30 40 50 60 70 80 90 100
Algorithm BinSearch(a,n,x)
//Given an array a[1:n] of elements in mid = (low+high)/2 x = 30
//nondecreasing order, n ≥ 0 1. low = 1, high = 10
{ mid = (1+10)/2 = 5, 30 < 50, high = 4
low = 1; high = n; 2. low = 1, high = 4
while (low ≤ high) do mid = (1 + 4)/2 = 2, 30 > 20, low = 3
{ 3. low = 3, high = 4
mid := (low+high)/2; mid = (3 + 4)/2 = 3
if (x < a[mid]) then high = mid-1;
else if (x > a[mid]) then low := mid+1;
else return mid;
}
return 0;
}
Periyar Govt. Arts College
8 Dr. R. Bhuvaneswari Cuddalore
Binary Search using recursion
1 2 3 4 5 6 7 8 9 10
10 20 30 40 50 60 70 80 90 100
Algorithm BinSrch(a,i,l,x)
{ mid = (i+l)/2 x = 30
if (l = i) then 1. i = 1, l = 10
{ mid = (1+10)/2 = 5, 30 < 50, l = 4
if (x = a[i]) then return i; 2. i = 1, l = 4
else return 0; mid = (1 + 4)/2 = 2, 30 > 20, i = 3
} 3. i = 3, l = 4
mid = (3 + 4)/2 = 3
else
{
mid = (i+l)/2;
if (x = a[mid]) then return mid;
else if (x < a[mid]) then return BinSrch(a,i,mid-1,x);
else return BinSrch(a,mid+1,l,x);
}
} Periyar Govt. Arts College
9 Dr. R. Bhuvaneswari Cuddalore
Binary Search
Time Complexity
1. If the search element is the middle element of the array, in this case,
time complexity will be O(1), the best case.
2. Otherwise, binary search algorithm breaks the array into half in each
iteration.
The array is divided by 2 until the array has only one element:
Algorithm MaxMin(i,j,max,min) {
//a[1:n] is a global array. mid = (i+j)/2;
{ MaxMin(i,mid,max,min);
if (i = j) then max = min = a[i]; MaxMin(mid+1,j,max1,min1);
else if (i = j-1) then if(max < max1) then max = max1;
{ if (min > min1) then min = min1;
if (a[i] < a[j]) then }
{ }
max = a[j]; min = a[i];
}
else
{
max = a[i]; min = a[j];
}
}
else Periyar Govt. Arts College
14 Dr. R. Bhuvaneswari Cuddalore
Finding the maximum and minimum
Index: 1 2 3 4 5 6 7 8 9
Array: 22 13 -5 -8 15 60 17 31 47
mid = (1+9)/2
=5 1, 9, 60,-8
1, 2, 22, 13 3, 3, -5,-5
Periyar Govt. Arts College
15 Dr. R. Bhuvaneswari Cuddalore
Finding the maximum and minimum
Taking T(2) = 1
𝑛
ie. =2
2𝑘
T(n) = 2k + 2k + 2k-1 + 2k-2 + ……. + 2 𝑛
k 𝑘 𝑗 𝑗
𝑥𝑛 − 1
𝑗 =1 2
=2 + 𝑆𝑖𝑛𝑐𝑒, 𝑥 =𝑥∗
𝑥−1
𝑗 =1
(2𝑘 −1)
= 2k + 2 ∗
2−1
𝑛 𝑛
= + 2 ∗ ( − 1)
2 2
𝑛
= +𝑛−2
2
3𝑛
= −2
2
Therefore, 3n/2- 2 is the best, average and worst case number of comparisons where
n is power of 2.
Algorithm MergeSort(low,high) 38 27 43 3 9 82 10
{
If (low < high) then
{ mid = 4 1, 7
• In merge sort, the array a[1:n] was divided at its midpoint into
sub arrays which were independently sorted and later merged.
• In quick sort, the division into 2 sub arrays is made so that the
sorted sub arrays do not need to be merged later.
• This is accomplished by rearranging the elements in a[1:n] such
that a[i] ≤ a[j] for all i between 1 and m and all j between (m+1)
and n for some m, 1 ≤ m ≤ n.
• Thus the elements in a[1:m] and a[m+1:n] can be independently
sorted.
• No merging is needed.
• This rearranging is referred to as partitioning.
T(n) = 2T(n/2) + n
= 2[2T(n/4) + n/2] + n
= 4T(n/4) + n + n
= 4T(n/4) + 2n Since,
= 4[2T(n/8) + n/4] + 2n T(n/2k = 1)
= 8T(n/8) + n + 2n n = 2k
= 8T(n/8) + 3n log2n = log22k
…………. = klog22
= 2kT(n/2k) + kn =k
= nT(1) + kn
= nlogn
Periyar Govt. Arts College
27 Dr. R. Bhuvaneswari Cuddalore
Selection Sort
General Method:
• In the method, problems have n inputs and requires to obtain a
subset that satisfies some constraints.
• Any subset that satisfies these constraints is called feasible
solution.
• A feasible solution should either maximizes or minimizes a given
objective function is called an optimal solution.
• The greedy technique in which selection of input leads to optimal
solution is called subset paradigm.
• If the selection does not lead to optimal subset, then the decisions
are made by considering the inputs in some order. This type of
greedy method is called ordering paradigm.
wi xi ≤ C 1 ≤i ≤n
i=1 n
xi = 6
• Given a set of items, each with a weight and a profit, determine the
number of each item to include in a collection so that the total weight is
less than or equal to a given limit and the total profit is as large as
possible.
• Items are divisible; you can take any fraction of an item.
• And it is solved using greedy method.
subject to wi x i ≤ m − − − − − ②
1≤ i ≤n
0 ≤ xi ≤ 1, 1 ≤ i ≤ n − − − − −③
• A feasible solution is any set (x1 …. xn) satisfying equations ② and
• An optimal solution is a feasible solution for which equation ① is
maximized. Periyar Govt. Arts College
38 Dr. R. Bhuvaneswari Cuddalore
Knapsack Problem
Example: n = 3, m = 20
Weight wi 18 15 10
Profits pi 25 24 15
Algorithm GreedyKnapsack(m, n)
//n objects are ordered such that p[i]/w[i] ≥ p[i+1]/w[i+1].
{
for i:= 1 to n do x[i] := 0.0; Weight wi 15 10 18
U := m; Profits pi 24 15 25
for i := 1 to n do
{ x[i] = 0.0 m = 20, n = 3
if (w[i] > U) then break; x[2] = 0.0
x[3] = 0.0
x[i] :=1.0;
U = 20
U := U-w[i];
i=1
}
x[1] = 1; U = 5
if (i ≤ n) then i = 2, 10 > 5
x[i] = U/w[i]; x[2] = 5/10 = 1/2
} x[1] = 1, x[2] = 1/2, x[3] = 0
Periyar Govt. Arts College
41 Dr. R. Bhuvaneswari Cuddalore