Module Ii
Module Ii
Solve(I)
n= size(I);
if(n≤ smallsize)
solution=directlysolve(I);
else
divide I into I1,..Ik;
for each i I1,..Ik
Si=Solve(Ii);
solution=Combine(S2,…Sk);
return solution.
1
Q) Write and explain the control abstraction for divide- and- conquer method.
Q) Give the control abstraction of divide-and-conquer.
Ans:-
Control Abstraction For Divide and Conquer:-
A control abstraction is a procedure that reflects the way an actual program
based on DAndC will look like. A control abstraction shows clearly the flow of control
but the primary operations are specified by other procedures. The control abstraction
can be written either iteratively or recursively.
If we are given a problem with ‘n’ inputs and if it is possible for splitting the ‘n’
inputs into ‘k’ subsets where each subset represents a sub problem similar to the
main problem then it can be achieved by using divide and conquer strategy.
If the sub problems are relatively large then divide and conquer strategy is
reapplied. The sub problem resulting from divide and conquer design are of the same
type as the original problem. Generally divide and conquer problem is expressed using
recursive formulas and functions.
A general divide and conquer design strategy(control abstraction) is illustrated
as given below-
Algorithm DAndC (P)
{
if small(P) then return S(P) //termination condition
else
{
Divide P into smaller instances P1, P2, P3… Pk k≥1; or 1≤k≤n
Apply DAndC to each of these sub problems.
Return Combine (DAndC(P1), DAndC (P2), DAndC (P3)…
DAndC (Pk)
}
}
The above blocks of code represents a control abstraction for divide and
conquer strategy. Small (P) is a Boolean valued function that determines whether the
input size is small enough that the answer can be computed without splitting. If small
(P) is true then function ‘S’ is invoked. Otherwise the problem ‘P’ is divided into sub
problems. These sub problems are solved by recursive application of Divide-and-
conquer. Finally the solution from k sub problems is combined to obtain the solution
of the given problem.
If the size of ‘P’ is ‘n’ and if the size of ‘k’ sub problems is n1,n2,…. nk
Respectively then the computing time of DAndC is described by the recurrence
relation.
T(n) = g(n) when n is small
= T(n1)+ T(n1)+ T(n2)+…….+ T(nk)+ f(n) otherwise.
T(n) denotes 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 of sub problems.
2
Q) Give the analysis of the complexity of the divide and conquer strategy.
Ans:-
To analyze the running time of a divide and conquer algorithms we make use of
a recurrence relation. A function T(n) denotes the running time of the algorithm on an
input of size ‘n’ and characterize T(n) using an equation that relates T(n) to values of
the function T for problem sizes smaller than n.
The recurrence relations are of the form.
Example:-
Consider the case in which a=2 and b=2. Let T(1)=2 and f(n)=n. then we have
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
= .
.
= 2i T(n/2i)+in , for any log 2 n ≥ I ≥ 1.
=2 log 2 nT(n/ log 2 n)+n log 2 n, corresponding to the choice of
i= log 2 n. Thus
T(n) = n T(1)+n log 2 n
= n log 2 n+2n
3
Q) Write an algorithm for performing binary search for any element in an array.
Ans:-
Problem:- Let ai, 1≤ I ≤ n , be a list of elements that are sorted in non decreasing
order. We need to determine whether a given element ‘x’ is present in the list. If ‘x’ is
present, then ‘j’ is determined such that aj=x. if x is not in the list, then j is to be set to
zero.
Solution:- Every call to the binary search splits the partition into two in the middle
according to the formula
low high
Mid = where low is the index of the first element of
2
the partition and high is the last element of the partition. Generally the ‘n’ inputs are
sorted in A[1..n] array. The splitting process terminates when the size of the partition
becomes ‘1’ or required element is found.
Explanation:
The problem is subdivided into smaller problems until only one single element
is left out.
1. If low = high then it means there is only one single element. So compare it with
the search element ‘x’. if both are equal then return the index, else return 0.
2. If low high then divide the problem into smaller subproblems.
a. Calculate mid = (low+high)/2.
b. Compare x with element at mid if both are equal the return index mid.
c. Else if x is less than A[mid] then the element x would be in the first
partition A[l:mid-1]. So make a recursive call to BinSearch with low as l
and high as mid-1.
4
d. Else x is greater than A[mid] then the element x would be in the second
partition A[mid+1: h]. So make a recursive call to BinSearch with low as
mid+1 and high as h.
Analysis:-
Example:-
-15,-6,0,7,9,23,25,54,82,101,112,125,131,142,151.
The no. of comparisons needed for a successful search for the above given keys
can be determined using a decision tree as follows. A decision tree is drawn using the
values of mid for every call. Circle( O ) represents an internal node and rectangle
represents an external node.
2 3 4 5 6 7 8 9 10 11 12 13 14 15
-15 -6 0 7 9 23 25 54 82 101 112 125 131 142 151
Low mid high
5
7
3 11
1 5 9 13
2 4 6 8 10 12 14
Each and every internal node represents a successful search and the values
denoted by internal nodes are the various values taken by the variable mid. If the
value of mid is at level zero then the no. of comparisons for an element present at the
position is one.
The no. of comparisons for a successful search if the mid value is at level one is
two and the max. No. of comparisons for a successful search in the above decision tree
is equal to four. i.e., the max. No. of comparisons is directly proportional to the height
of the tree. Therefore the time complexity for a successful search is given by O (log2 n).
Each and every unsuccessful search terminates at an external node. The no. of
comparisons needed for an unsuccessful search of an element less than -15 is 3. For
the entire remaining unsuccessful search the no. of comparisons made is 4. Therefore
the average no. of elemental comparisons of an unsuccessful search is
3 * 1 4 * 14 59
15 15 3.93 4
Except for one case the no. of comparisons for unsuccessful search is constant
and is equal to the height of the tree. Therefore the time for unsuccessful search is
given by O (log2 n).
6
Q) Describe about Merge sort.
Q) Explain how divide and conquer strategy can be applied for merge sort, and develop
an algorithm for the same. Give its time and space complexity.
Ans:-Merge Sort: -
The divide and conquer strategy for the merge sort divides the problem with ‘n’ inputs
into ‘k’ sub problems by dividing each partition recursively in the middle until the ‘p’ (small) is
satisfied i.e. until the size of the partition is one. Then the merge algorithm will merge the sub
problems recursively until the solution for the entire problem is being obtained.
The merge algorithm is applied on two sub lists which are in sorted order.
1. Divide:- Divide the n-element sequence to be sorted into two sub sequences of n/2
elements each.
2. Conquer:- Sort the two subsequences recursively using merge sort.
3. Combine:- Merge the two sorted subsequences to produce the sorted answer.
310, 285, 179, 652, 351, 423, 861, 254, 450, 520
7
1, 10
1, 5 6, 10
1, 3 4, 5 6,8 9, 10
1, 2 3, 3 4, 4 5, 5 6, 7 8, 8 9, 9 10,
10
6, 7 2, 2 6, 6 7, 7
285, 310/ 179/ 652, 351/ 423, 861, 254, 450, 520
8
Algorithm Merge (low, mid, high)
// a (low: high) is a global array containing
// two sorted subsets in a (low: mid) and in
// a (mid+1, high). The goal is to merge these two
//subsets in to a single set residing in a (low: high)
// b[ ] is a temporary global array.
{
h:=low, i:=low; j=mid+1;
while ((h≤mid) and (j≤ high)do
{ if (a(h) ≤ a(j)) then
{ b(i) = a(h); :=h+1;
}
i:=i+1;
}
if (h>mid) then
for k=j to high do
{ b(i): = a(k);
i: = i+1;
}
else
for k:=h to mid do
{
b(i)=a(k); i:=i+1;
}
for k: = low to high do
a(k): = b(x);
}
Simulate merge sort on the following sub list.
Merge (low, mid, high)
H=1, i=1, j=6, mid=5, high=10
While ((h ≤ mid) and (j ≤ high))
9
Out of while (h ≤mid) – false
K=10 to (j to high)
i.e. 861 is copied to b
179 285 310 351 652 254 423 450 520 861
b
1 2 3 4 5 6 7 8 9 10
1, 1, 2 6, 6, 7
1, 2, 3 4, 4, 5 6, 7, 8 9, 9, 10
6, 8, 10
1, 3, 5
1, 5, 10
Q)By means of an example demonstrate how merge sort works. Derive the timing
and space complexities of the same.
Analysis: -
If the time for merging operation is proportional to ‘n’ then the computing time for
merge sort is described by the recursion relation.
a n=1, a is constant
T(n) 2 T(n/2) + cn, n>1, c is constant
10
n
= 4T + 2 Cn
4
n
= 8 T + 3cn
8
= 2k T(1) + Kcn 2k = n
= an+cn logn k = log2 n
2 < n ≤ 2 K+1
k
T(n) = 0 (n log2n)
Q)Give the quick sort algorithm and illustrate the method with suitable example.
Also derive the time complexity for an average case.
Q) Describe quick sort algorithm and explain it with an example. Discuss its time
complexity.
Q) Develop an algorithm for quick sort using divide and conquer strategy. Find
its time and space complexity both for average and worst cases.
Q) Give an algorithm for quick sort.Analyse its complexity. On what input data
does quick sort exibhit its best and worst behavior.
Q) Give the partition and Quick sort algorithms and analyse them in detail.
Q) Show how quick sort sorts the following sequence of keys.
5 5 8 3 4 3 2.
Q) Show how procedure QUICKSORT sorts the following sets of keys:
(1,1,1,1,1,1,1) and (5,5,8,3,4,3,2).
Q) Write and explain the Quick sort Algorithm.
Q) Describe about quicksort.
Q) Derive the average-case timing complexity of quicksort.
Q) Do the average case analysis of the quicksort algorithm.
Quick Sort: -
In the divide and conquer of quick sort we recursively divide the inputs into
sub lists until the small (p) condition is occurred. The partitions in this are formed
unlike the other problems involved in divide and conquer. In every execution of quick
sort the partition location ‘j’ is determined where ‘j’ is the location of an element place
in its sorted order. Then we form two partitions from 1st element of the partition to j-1
and j+1 to the lout element of the partition.
11
Steps in Partition algorithm: -
Step - 1: - Initially we take ‘p’ pointing to the first element and q pointing to the last
element i.e. p is the location of 1st element q is the location of last element.
Step - 2: - Initialize i:=p and j:q+1
Step - 3: - Increment I until we find an element which is greater than the element
present at location ‘p’
Step - 4: - Decrement j by 1 until we find an element which is less than the element
presser at location ‘p’
Step - 5: - If i<j then swap (a(1), a(j))
Step - 6: - If i>j swap (a(i), a(p))
Step – 7: - If i>j then we get two partitions i.e. two sub lists from (p,j-1) and
(j+1, q)
Step – 8: - Solve each sub problem recursively until p becomes greater than q
Algorithm Quick sort (p, q)
// sorts the elements a(p)……., a(q)
// which reside in global array a(1:n) in to assending
// order; a (n+1) is considered to be defined and must be
// all the elements in a (1,n)
{ if (p<q) then // if there is more than one element
{//divide p into sub problems
j:= partition (a,p,q+q);
//j is position of partitioning element
//solve the sub problems
quicksort (p,j-1);
quicksort (j+1,q)
//need not combine the solutions
}
}
Algorithm partition (a,m,p)
12
Example:
26, 5, 37, 1, 61, 11, 59, 15, 48, 19
1 2 3 4 5 6 7 8 9 10
26, 5, 37, 1, 61, 11, 59, 15, 48, 19
ip q j (q+1)
Ii j p i j i< j
Partition
i j j i I>j
[1, 5] 11 [19 15] 26 [ 48, 37] 58 [61]
1 5 11 15 19 26 37 48 59 61
Analysis: -
In analyzing the quick sort we count the no/: of element comparisons. Quick sort
requires O (n2) comparisons in the worst case and 0 (along) comparisons in the
average case.
13
Stressen’s matrix multiplication
Let A and B be the 2 n*n Matrix. The product matrix C=AB is calculated by using the
formula,
Divide and conquer method suggest another way to compute the product of n*n matrix.
We assume that N is a power of 2 .In the case N is not a power of 2 ,then enough rows
and columns of zero can be added to both A and B .SO that the resulting dimension are
the powers of two.
If n=2 then the following formula as a computed using a matrix multiplication operation
for the elements of A & B.
If n>2,Then the elements are partitioned into sub matrix n/2*n/2..since ‘n’ is a power of 2
these product can be recursively computed using the same formula .This Algorithm will
continue applying itself to smaller sub matrix until ‘N” become suitable small(n=2) so
that the product is computed directly .
The formula are
14
For EX:
2222 1 1 11
4*4= 2222 1 11 1
2222 * 1 11 1
2222 1 11 1
2 2 2 2 1 1 1 1 4 4 4 4
2 2 2 2 * 1 1 1 1 = 4 4 4 4
2 2 2 2 1 1 1 1 4 4 4 4
2 2 2 2 1 1 1 1 4 4 4 4
P=(4*4)+(4+4)=64
Q=(4+4)4=32
R=4(4-4)=0
S=4(4-4)=0
T=(4+4)4=32
U=(4-4)(4+4)=0
V=(4-4)(4+4)=0
C11=(64+0-32+0)=32
C12=0+32=32
C21=32+0=32
C22=64+0-32+0=32P
15
So the answer c(i,j) is 32 32
32 32
since n/2n/2 &matrix can be can be added in Cn for some constant C, The overall computing
time T(n) of the resulting divide and conquer algorithm is given by the sequence.
That is T(n)=O(n^3)
* Matrix multiplication are more expensive then the matrix addition O(n^3).We can attempt to
reformulate the equation for Cij so as to have fewer multiplication and possibly more addition .
Stressen has discovered a way to compute the Cij of equation (2) using only 7
multiplication and 18 addition or subtraction.
Strassen’s formula are
P= (A11+A12)(B11+B22)
Q= (A12+A22)B11
R= A11(B12-B22)
S= A22(B21-B11)
T= (A11+A12)B22
U= (A21-A11)(B11+B12)
V= (A12-A22)(B21+B22)
C11=P+S-T+V
C!2=R+t
C21=Q+T
C22=P+R-Q+V
16