DAA Unit 2
DAA Unit 2
Divide and conquer: General method, Defective chess board, Binary search, Finding the maximum and minimum, Merge Sort,
Quick sort, Randomized Sorting algorithms.
Control Abstraction:
Control abstraction is a procedure whose flow of control is clear but whose primary operations are
specified by other procedures whose precise meanings are left undefined.
Control abstraction that mirrors the way an algorithm is based on divide-and-conquer is shown
below.
1 Algorithm DAndC(P)
2 {
3 if Small(P) then return S(P);
4 else
5 {
6 divide P into smaller instances P1, P2, . . Pk k≥1
7 Apply DAndC to each of these problems;
8 return combine(DAndC(P1), DAndC(P2) . . . DAndC(Pk));
9 }
10 }
1
Design and Analysis of Algorithms Unit-II
DAndC strategy produces subproblems of type original problem, Therefore it is convenient to write these
algorithms using recursion.
To solve this recurrence relation, we assume that T(1) is known, n is a power of b (i.e., n bk , log bn k )
Substitution method of solving Recurrence Relation repeatedly makes substitution for each occurrence of
the function in right hand side until all such occurrences disappear.
Example: consider the case in which a=2 and b=2, T(1)=2 and f(n)=n.
T(1) n 1
T(n) n
a.T f(n) n 1
b
T(n) n n
2.T 2
n
2 2.T n
n
4 2
n
4.T 2.n
4
n n
4 2.T 2n
8 4
n
3.n
8.T 8
:
n
In general 2 .T i.n
i
for any log 2n i 1
2 i
n
In particular T(n) 2log 2 n.T n.log n
log 2 n 2
2
T(n) n.T(1) n.log2n
T (n) n.log2n 2n
2
Design and Analysis of Algorithms Unit-II
When k = 0, the size of the chess board is 1 X 1 and there is only one possible defective chessboard.
When k = 1, the size of the chess board is 2 X 2, and there can be 4 possible defective chessboards.
Therefore, for any k, there are exactly 22𝑘 defective chessboards.
Triomino :
A triomino is an L shaped object that can cover three squares of a chessboard.
A triomino has four orientations. Following figure shows triominoes with different orientations.
Solution :
Divide and Conquer leads to an elegant solution to this problem.
The method suggests reducing the problem of tiling a 2𝑘 𝑋 2𝑘 defective chessboard to tiling a smaller
defective chessboard.
A 2𝑘 𝑋 2𝑘 chessboard can be partitioned into four 2𝑘−1 𝑋 2𝑘−1 chessboards.
3
Design and Analysis of Algorithms Unit-II
Only one board has a defective square. To convert the remaining three boards into defective
chessboards, we place triomino at the corner formed by these three.
This partitioning technique is recursively used to tile the entire 2𝑘 𝑋 2𝑘 chessboard.
This recursion terminates when the chessboard size has been reduced to 1 X 1
4
Design and Analysis of Algorithms Unit-II
Time Complexity:
Let t(k) denote the time taken by TileBoard to tile a defective chessboard.
When k=0, a constant amount of time is spent. Let the constant be d.
When k>0, recursive calls are made. These calls take 4.t(k – 1) time.
This can be represented by the following recurrence equation.
Binary Search:
Let ai be a list of elements that are sorted in nondecreasing order.
Here i is in the range of 1 to n, 1 ≤ i ≤ n.
Binary search is a problem of determining whether an element x is present in the list or not.
o If x is present in the list, then we need to determine value j such that aj=x.
o If x is not in the list, then j is to be set to zero.
Let P n, a low ,...,a high , x denote an instance of search problem. Here
o n is number of elements in the list.
o alow ,...,ahigh list of elements
o x is the element searched for.
If P has more than one element, it can be divided into subproblems, as follows.
Pick and index q in the range low to high and compare x with aq. There are three possibilities.
1. x = aq : The problem P is immediately solved in this case.
2. x < aq : x has to be searched in the sublist in this case.
The sublist is alow ,...,aq1 .
Therefore, P reduces to P n, a low ,...,aq1 , x
3. x > aq : In this case also x has to be searched in the sublist
Therefore P reduces to P n, a q1 ,...,a high , x.
Dividing the problem P into subproblem takes (1) time.
After a comparison with aq, the remaining problem instance can be solved by using divide-and-
conquer scheme again.
low high
If q is choosen that aq is the middle element, then that search algorithm is called Binary Serach.
i.e., q 8074816703
2
5
Design and Analysis of Algorithms Unit-II
There is no need of combining answers in binary search, because answer of the subproblem is also
the answer of the original problem P.
The variables low, high, mid need to be traced to simulate this algorithm.
6
Design and Analysis of Algorithms Unit-II
Example: The number of element comparisons needed to find each of the 14 elements is
45
Average Comparisons for successful search : 3.21
14
There are (14+1=15) possible ways that an unsuccessful search may terminate.
If x<a[1], then the algorithm requires 3 element comparisons to determine that x is not present. For
remaining cases, the algorithm requires 4 element comparisons. 3 14 * 4 59
Average number of element comparisons for unsuccessful search = 3.93
15 15
But we prefer a formula for n elements. A good way to derive a formula is to consider sequence of mid
values that are produced by all possible values of x. A Binary Decision Tree used to describe this. Each tree
in this node is the value of mid.
Example : if n=14, a Binary Decision Tree that traces the way in which these values are produced is shown
below.
The first comparison is x with a[7]. If x<a[7], then the next comparison is with a[3]; If x>a[7], then
the next comparison is with a[11].
Each path through the tree represents a sequence of comparisons in the binary search method.
If x is present then the algorithm will end at one of the circular nodes that lists the index into the
array where x was found.
7
Design and Analysis of Algorithms Unit-II
If x is not present, the algorithm will terminate at one of the square nodes.
Circular nodes are called internal nodes, and square nodes are referred to as external nodes.
If n is in the range [2k-1, 2k] then binary search makes atmost k element comparisons for a successful search
and either k-1 or k comparisons for an unsuccessful search.
i.e., the time for a successful search is O(log n) and for an unsuccessful search is (log n)
By mathematical induction, we can say that “for any binary tree with n internal nodes, E and I are
related by the formula”
E=I+2n.
Let As(n) be the average number of comparisons in a successful search, and Au(n) be the average
number of comparisons in an unsuccessful search.
The number of comparisons needed to find an element represented by an internal node is one more
than the distance of this node from the root.
I
Hence A (n) 1
s
n
Since every binary tree with n internal nodes has n+1 external nodes, it follows that
E
Au (n)
(n 1)
Using these three formulas,
I
A (n) 1
s
n
E - 2n
1
n
A (n).(n 1) 2n
1 u
n
A (n).n Au (n)
1 u 2
1 n
1 A (n) 1
u
n
From this, we see that As(n) and Au(n) are directly related.
From BDT, we conclude that average and worst case comparisons for Binary Search are same within a
constant time.
Best-case : For a successful search only one element comparison is needed and for unsuccessful search log n
element comparisons are needed in best case.
8
Design and Analysis of Algorithms Unit-II
After dividing P into smaller subproblems, we can solve them by recursively invoking the
same divide and conquer algorithm.
Combining the solutions :
Let P is the problem and P1 and P2 are its subproblems, then
o MAX(P) is larger of MAX(P1) and MAX(P2) and
o MIN(P) is smaller of MIN(P1) and MIN(P2)
Algorithm : to find maximum and minimum recursively.
Algorithm MaxMin(i, j, max, min)
// a[1 : n] is a global array. Parameters i and j are integers, 1 ≤ i ≤ j ≤ n.
//sets the max and min to the smallest and largest values.
{
if (i = j) then max := min := a[i];
else if ( i = j – 1) then
{
if (a[i] < a[j]) then
{
max := a[j]; min := a[i];
}
else
{
max := a[i]; min := a[j];
}
}
else
{
mid := (i + j) / 2;
9
Design and Analysis of Algorithms Unit-II
10
Design and Analysis of Algorithms Unit-II
Merge Sort:
Merge Sort is a sorting algorithm with the nice property that its worst case complexity is O(n log n).
Given a sequence of ‘n’ elements a[1],…,a[n] the general idea is to imagine them split into two sets
n n
a[1],…..,a[ ] and a[ +1],….a[n].
2 2
Each set is individually sorted, and the resulting sorted sequences are merged to produce a single
sorted sequence of „n‟ elements.
11
Design and Analysis of Algorithms Unit-II
17 if (h<mid) then
18 for k := j to high do
19 {
20 b[i]:=a[k]; i:=i+1;
21 }
22 else
23 for k:=h to mid do
24 {
25 b[i]:=a[k]; i:=i+1;
26 }
27 for k:=low to high do
28 a[k]:=b[k];
29 }
Example :
Consider an array of 10 elements.
a[1:10] = (310, 285, 179, 652, 351, 423, 861, 254, 450, 520)
Algorithm MergeSort begins by splitting a[] until they become one-element subarrays. Now merging
begins. This division and merging is shown in the below figure.
Following figure is a tree that represents the sequence of recursive calls that are produced by
MergeSort when it is applied to 10 elements.
The pair of values in each node is the values of the parameters low and high.
12
Design and Analysis of Algorithms Unit-II
Following figure is a tree representing the calls to procedure Merge. For example, the node
containing 1, 2, and 3 represents the merging of a[1:2] with a[3].
If the time for the merging operation is proportional to n, then computing time for merge sort is described by
the recurrence relation.
a n n 1
T(n) a.T c.n n 1
2
2
n
22.T 4 2cn
2 n
2 2.T c. 2cn
n
8 4
n
23.T 8 3cn
after k substitutions
2k.T 1 kcn
an cn.log n
Quick Sort:
In Quick sort, the division into two subarrays is made in such a way that, the sorted subarrays do not
need to be merged later.
This is achieved 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 merge is needed.
The rearrangement of the elements is accomplished
o By picking some element of a[], say t=a[s].
o And then reordering the elements so that all elements appearing before t in a[1:n] are less
than or equal to t and all elements appearing after t are greater than or equal to t.
o The rearranging is referred to partitioning.
Following algorithm accomplishes partitioning of elements of a[m:p]. It is assumed that a[m] is the
partitioning element.
1 Algorithm Partition(a, m, p)
2 {
3 pivot:=a[m], i:=m+1, j:=p;
4 while (i<j) do
5 {
6 while (a[i]≤pivot and i≤j) do
7 i:=i+1;
8 while (a[j]≥pivot and j≥i) do
9 j:=j-1;
10 if (i<j) then
11 interchange(a, i, j);
12 }
13 interchange(a, m, j);
14 return j;
15 }
Now the elements are partitioned about pivot element and the remaining elements are unsorted.
Using this method of partitioning, we can directly devise a divide and conquer method for
completely sorting n elements.
Two sets S1 and S2 are produced after calling partition. Each set can be sorted independently by
reusing the function partition.
Analysis:
In quicksort, the pivot element we chose divides the array into 2 parts.
o One of size k.
o Other of size n-k.
Both these parts still need to be sorted.
This gives us the following relation.
T(n) = T(k)+T(n-k)+c.n
Where T(n) refers to the time taken by the algorithm to sort n elements.
Worst-case Analysis:
Worst case happens when pivot is the least element in the array.
Then we have k=1 and n-k=n-1
n-2
T (n) T(1) (n -1).T(1) c. (n - j)
j0
n-2
n.T(1) c. (n - j)
j0
O(n )
2
Best-case Analysis:
Best case of Quick Sort occurs when pivot we pick divide the array into two equal parts, in every step.
n n
k , n - k , for array of size n
2 2
We have T(n) T(k) T(n - k) c.n
n
c.n
2.T 2
Solving this gives O(n log n).
1 Algorithm RQuickSort(p, q)
2 {
3 if (p<q) then
4 {
5 if ((q-p)>5) then
6 interchange(a, Random() mod (q-p+1)+p,p);
7 j := partition(a, p, q+1);
8 RQuickSort(p, j-1);
9 RQuickSort(j+1, q);
10 }
11 }