0% found this document useful (0 votes)
108 views41 pages

DAA Divide and Conquer

The document discusses algorithms for finding maximum and minimum elements using divide and conquer. It describes dividing a problem into smaller subproblems of finding max and min on halves of the input list, solving the subproblems recursively, and combining the results to find the overall max and min. Binary search and its recursive implementation are also covered, with analysis showing O(log n) time complexity. General methods for divide and conquer algorithms are introduced.

Uploaded by

saliniprabha
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)
108 views41 pages

DAA Divide and Conquer

The document discusses algorithms for finding maximum and minimum elements using divide and conquer. It describes dividing a problem into smaller subproblems of finding max and min on halves of the input list, solving the subproblems recursively, and combining the results to find the overall max and min. Binary search and its recursive implementation are also covered, with analysis showing O(log n) time complexity. General methods for divide and conquer algorithms are introduced.

Uploaded by

saliniprabha
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/ 41

Design and Analysis of

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)

Periyar Govt. Arts College


2 Dr. R. Bhuvaneswari Cuddalore
Divide and Conquer

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.

Periyar Govt. Arts College


3 Dr. R. Bhuvaneswari Cuddalore
Divide and Conquer

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

Periyar Govt. Arts College


4 Dr. R. Bhuvaneswari Cuddalore
Divide and Conquer

Control Abstraction of Divide and Conquer


Algorithm DAndC(P)
{
if small(P) then
return S(P);
else
{
divide P into smaller instance P1, P2…….., Pk, k≥1;
apply DAndC to each of these subproblems;
return combine(DAndC(P1), DAndC(P2), ……., DAndC(Pk));
}
}

Periyar Govt. Arts College


5 Dr. R. Bhuvaneswari Cuddalore
Divide and Conquer

Computing time of DAndC is:

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

Periyar Govt. Arts College


6 Dr. R. Bhuvaneswari Cuddalore
Binary Search

• Let ai be a list of elements that are in non-decreasing order. 1≤i ≤n.


• It is a problem of determining whether a given element x is present
in the list.

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:

we can rewrite it as:


n = 2k
by taking log both side, we get
log2n = log22k
log2n = klog22
k = log2n (since logaa = 1)
The time complexity of binary search is log2n
Periyar Govt. Arts College
10 Dr. R. Bhuvaneswari Cuddalore
Finding the maximum and minimum

• The problem to find the maximum and


minimum items in a set of n elements. 1 2 3 4 5
Algorithm StraightMaxMin(a,n,max,min) 37 78 45 12 92
// set max to maximim and min to the
// minimum of a[1:n] max = min = 37
{ i=2
max := min := a[1]; max = 78; min = 37
for i := 2 to n do i=3
{ max = 78; min = 37
if (a[i] > max) then max := a[i]; i=4
if (a[i] < min) then min := a[i]; max = 78; min =12
} i=5
} max = 92; min = 12
• StraightMaxMin requires 2(n-1) element
comparisons in the best, average and worst
cases. Periyar Govt. Arts College
11 Dr. R. Bhuvaneswari Cuddalore
Finding the maximum and minimum

• 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];
• When the elements are in the increasing order the number of
element comparisons is n-1.
• When the elements are in the decreasing order the number of
element comparisons is 2(n-1).

Periyar Govt. Arts College


12 Dr. R. Bhuvaneswari Cuddalore
Finding the maximum and minimum
Divide and Conquer Algorithm
• Let P = (n, a [i],……,a [j]) denote an arbitrary instance of the
problem.
• Here ‘n’ is the no. of elements in the list (a[i],….,a[j]) and we are
interested in finding the maximum and minimum of the list.
• If the list has more than 2 elements, P has to be divided into smaller
instances.
• We divide ‘P’ into the 2 instances,
P1=([n/2], a[1],……..a[n/2]) and
P2= (n-[n/2], a[[n/2]+1],….., a[n])
• After having divided ‘P’ into 2 smaller sub problems, we can solve
them by recursively invoking the same divide-and-conquer algorithm.
• max(P) is the maximum of max(P1) and max(P2)
• min(P) is the minimum of min(P1) and min(P2) Periyar Govt. Arts College
13 Dr. R. Bhuvaneswari Cuddalore
Finding the maximum and minimum

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

Example: find max and min in the array:


22, 13, -5, -8, 15, 60, 17, 31, 47 ( n = 9 )

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

mid = (1+5)/2 mid = (6+9)/2


1, 5, 22, -8 6, 9, 60, 17
=3 =7

mid = (1+3)/2 4, 5, 15, -8


1, 3, 22, -5 6, 7, 60, 17 8, 9, 47, 31
=2

1, 2, 22, 13 3, 3, -5,-5
Periyar Govt. Arts College
15 Dr. R. Bhuvaneswari Cuddalore
Finding the maximum and minimum

The number of element comparisons T(n) is represented as recurrence


relation
𝑛 𝑛
𝑇 + 𝑇 + 2 𝑛>2
𝑇 𝑛 = 2 𝑛
1 𝑛=2
0 𝑛=1
When n is a power of two, n = 2k for some positive integer k, then
T(n) = 2T(n/2)+2
= 2(2T(n/4)+2)+2
= 4T(n/4) + 4 + 2
= 4(2T(n/8) + 2) +4 + 2
= 8T(n/8) + 8 + 4 + 2
…….
= 2kT(n/2k) + 2k + 2k-1 + 2k-2 + …… + 2
Periyar Govt. Arts College
16 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.

Periyar Govt. Arts College


17 Dr. R. Bhuvaneswari Cuddalore
Merge Sort

 Sort a sequence of n elements into non-decreasing order.


 Merge sort is a sorting technique based on divide and conquer
technique.
 Merge sort first divides the unsorted list into two equal halves.
 Sort each of the two sub lists recursively until we have list size
of length 1, in which case the list itself is returned.
 Merge the two sorted sub lists back into one sorted list.

Periyar Govt. Arts College


18 Dr. R. Bhuvaneswari Cuddalore
Merge Sort

Periyar Govt. Arts College


19 Dr. R. Bhuvaneswari Cuddalore
Merge Sort

Algorithm MergeSort(low,high) 38 27 43 3 9 82 10
{
If (low < high) then
{ mid = 4 1, 7

mid = (low+high)/2; mid = 2 1, 4


MergeSort(low,mid);
MergeSort(mid+1,high); mid = 1 1, 2
Merge(low,mid,high);
} 1, 1 2, 2
}

Periyar Govt. Arts College


20 Dr. R. Bhuvaneswari Cuddalore
Merge Sort
Algorithm Merge(low,mid,high) if(h > mid) then
//b[] is an auxiliary global array. {
{ for k = j to high do
h=low; i=low; j=mid+1; {
while((h≤mid) and (j≤high)) do b[i] = a[k]; i = i+1;
{ }
if(a[h] ≤ a[j]) then }
{ else
b[i] = a[h]; h = h+1; {
} for k = h to mid do
else {
{ b[i] = a[k]; i = i+1;
b[i] = a[j]; j = j+1; }
} }
i = i+1; for k = low to high do
} a[k] = b[k];
}
Periyar Govt. Arts College
21 Dr. R. Bhuvaneswari Cuddalore
Merge Sort

Computing time for merge sort is described by the recurrence relation,


𝑎 𝑛 = 1, 𝑎 𝑖𝑠 𝑎 𝑐𝑜𝑛𝑠𝑡𝑎𝑛𝑡
𝑇 𝑛 = 𝑛
2𝑇 + 𝑐𝑛 𝑛 > 1, 𝑐 𝑖𝑠 𝑎 𝑐𝑜𝑛𝑠𝑡𝑎𝑛𝑡
2
when n = 2k
T(n) = 2T(n/2) + cn
= 2[2T(n/4) + cn/2] + cn
= 4T(n/4) + cn + cn Since,
= 4T(n/4) + 2cn T(n/2k = 1)
= 4[2T(n/8) + cn/4] + 2cn n = 2k
= 8T(n/8) + cn + 2cn log2n = log22k
= 8T(n/8) + 3cn = klog22
…………. =k
= 2kT(n/2k) + kcn
= 2kT(1) + kcn
= an + cnlogn Periyar Govt. Arts College
22 Dr. R. Bhuvaneswari Cuddalore
Quick Sort

• 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.

Periyar Govt. Arts College


23 Dr. R. Bhuvaneswari Cuddalore
Quick Sort

• Quick sort picks an element as pivot element and partitions the


given array around the picked pivot.
• There are many different versions of quick sort that pick pivot in
different ways.
 pick first element as pivot.
 pick last element as pivot.
 Pick a random element as pivot.
 Pick median as pivot.
• The role of the pivot value is to assist with splitting the list.
• The actual position where the pivot value belongs in the final
sorted list, commonly called the split point, will be used to divide
the list for subsequent calls to the quick sort.

Periyar Govt. Arts College


24 Dr. R. Bhuvaneswari Cuddalore
Quick Sort Example

Periyar Govt. Arts College


25 Dr. R. Bhuvaneswari Cuddalore
Quick Sort
Algorithm Quicksort(p,q) repeat
{ j := j-1;
if (p<q) then until (a[j] ≤ v);
{ if (i < j) then Interchange(a, i, j);
j:= Partititon (a,p,q+1); }until ( i ≥j);
Quicksort(p,j-1); a[m] := a[j];
Quicksort(j+1,q); a[j] := v;
} return j;
} }

Algorithm Partition(a,m,p) Algorithm Interchange(a, i, j)


{ {
v:=a[m]; i:=m; j:=p; p := a[i];
repeat a[i] := a[j];
{ a[j] := p;
repeat }
i:=i+1;
until (a[i] ≥ v);
Periyar Govt. Arts College
26 Dr. R. Bhuvaneswari Cuddalore
Quick Sort

Computing time for Quick sort


T(n) = 2T(n/2) + n for n > 1, T(1) = 0

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

• Selection sort is the most simplest sorting algorithm.


• Following are the steps involved in selection sort(for sorting a
given array in ascending order):
 Starting from the first element, search the smallest element in
the array, and replace it with the element in the first position.
 Then move on to the second position, and look for smallest
element present in the subarray, starting from index 2 till the
last index.
 Replace the element at the second position in the original
array with the second smallest element.
 This is repeated, until the array is completely sorted.

Periyar Govt. Arts College


28 Dr. R. Bhuvaneswari Cuddalore
Selection Sort Example

Periyar Govt. Arts College


29 Dr. R. Bhuvaneswari Cuddalore
Selection Sort
Algorithm Selection(a, n)
{
for i := 1 to n-1 do
{
min := a[i];
loc := i;
for j := i+1 to n do
{
if (min > a[j] ) then
{
min := a[j];
loc :=j;
}
}
temp := a[i]; a[i] := a[loc]; a[loc] := temp;
}
}
Periyar Govt. Arts College
30 Dr. R. Bhuvaneswari Cuddalore
Selection Sort

Number of comparisons in selection sort:


(n-1) + (n-2) + (n-3) +……. + 2 + 1
n(n-1)/2 comparisons

Periyar Govt. Arts College


31 Dr. R. Bhuvaneswari Cuddalore
Greedy Method

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.

Periyar Govt. Arts College


32 Dr. R. Bhuvaneswari Cuddalore
Greedy Method

Control Abstraction of Greedy Method


Algorithm Greedy(a,n)
// a[1:n] contains n inputs
{
solution := 0;
for i :=1 to n do
{
x := select(a);
if feasible(solution, x) then
solution := Union(solution,x);
}
return solution;
}

Periyar Govt. Arts College


33 Dr. R. Bhuvaneswari Cuddalore
Container Loading
• Large ship to be loaded with cargo.
• All containers are of the same size but may be of different weights.
• Container i has weight wi.
• The capacity of the ship is C.
• Load the ship with maximum number of containers without exceeding
the cargo weight capacity.
• Find values xi  {0, 1} such that
n

wi xi ≤ C 1 ≤i ≤n
i=1 n

• And the optimum function xi is maximized.


i=1
• Every set of xi’s that satisfy the constraints is a feasible solution.
n

• Every feasible solution that maximizes xi is an optimal solution.


i=1 Periyar Govt. Arts College
34 Dr. R. Bhuvaneswari Cuddalore
Container Loading

• Ship may be loaded in stages.


• Greedy criterion: From the remaining containers, select the one with
least weight.
Example:
n=8
[w1, w2, w3, w4, w5, w6, w7, w8] = [100, 200, 50, 90, 150, 50, 20, 80]
C = 400
[x1, x2, x3, x4, x5, x6, x7, x8] = [1, 0, 1, 1, 0, 1, 1, 1]

xi = 6

Periyar Govt. Arts College


35 Dr. R. Bhuvaneswari Cuddalore
Container Loading

Algorithm ContainerLoading(c, capacity, numberofContainers, x)


// set x[i] = 1 if and only if container c[i], i ≥ 1 is loaded.
{
// sort into increasing order of weights.
Sort(C, numberofContainers);
n = numberofContainers;
for i = 1 to n do
x[i] = 0;
i = 1;
while ((i  n) && (c[i].weight  capacity))
{
x[c[i].id] = 1;
capacity = capacity – c[i].weight;
i++;
}
} Periyar Govt. Arts College
36 Dr. R. Bhuvaneswari Cuddalore
Knapsack Problem

• 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.

Periyar Govt. Arts College


37 Dr. R. Bhuvaneswari Cuddalore
Knapsack Problem

• Given n objects and a knapsack or bag.


• wi → weight of object i.
• m → knapsack capacity.
• If a fraction xi, 0 ≤ xi ≤ 1 of object i is placed into the knapsack, then a
profit of pixi is earned.
• Objective is to fill the knapsack that maximizes the total profit earned.
• Problem can be stated as
maximize pi x i −−−−−①
1 ≤ i ≤n

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

(x1, x2, x3) Σwixi Σpixi


1. (1/2, 1/3, 1/4) 16.5 24.25
2. (1, 2/15, 0) 20 28.2
3. (0, 2/3, 1) 20 31
4. (0, 1, 1/2) 20 31.5
5. (2/3, 8/15, 0) 20 29.5
6. (5/6, 1/3, 0) 20 28.8
Among all the feasible solutions ④ yields the maximum profit
Periyar Govt. Arts College
39 Dr. R. Bhuvaneswari Cuddalore
Knapsack Problem

The greedy algorithm:


Step 1: Sort pi/wi into nonincreasing order.
Step 2: Put the objects into the knapsack according to the sorted
sequence as possible as we can.
e. g.
Weight wi 15 10 18
n = 3, M = 20
(w1, w2, w3) = (18, 15, 10) Profits pi 24 15 25
(p1, p2, p3) = (25, 24, 15)
Sol: p1/w1 = 25/18 = 1.39
p2/w2 = 24/15 = 1.6
p3/w3 = 15/10 = 1.5
Optimal solution: x1 = 0, x2 = 1, x3 = 1/2

Periyar Govt. Arts College


40 Dr. R. Bhuvaneswari Cuddalore
Knapsack Problem

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

You might also like