Unit 2 Introduction To Divide and Conquer Paradigm 1
Unit 2 Introduction To Divide and Conquer Paradigm 1
in
l 0
r n-1
while l ≤ r do
m ( l + r) / 2
if key = = A[m]
return m
else
r m-1
else
l m+1
return -1
fi
C(n) denote the number of times the fundamental operation is
performed. Then one, then Cworst t(n)= Performance in worst case.
Since the algorithm after each comparison is the issue is divided into
half the size we have,Cworst (n) = Cworst (n/2) + 1 for n > 1C(1) = 1●
Using the master theorem to solve the recurrence equation, to give the
number of when the search key is matched against an element in the
list, we have:
C(n) = C(n/2) + 1
a=1
b=2
f(n) = n0 ;
d=0
case 2 holds:
= Θ (n0 log n)
= Θ ( log n)
Application :
Guessing Number game
Word Lists/Dictionary Quest, etc
Limitations :
Interacting badly with the hierarchy of memory
Allows the list to be sorted
Because of the list element's random access, you need arrays instead
of a linked list.
Advantages :
E cient on an incredibly broad list
ffi
Iteratively/recursively applied can be
Q2) De ne quick sort ?A2) : Quick Sort● Quick sort algorithm works
fi
on the pivot element.● Select the pivot element (partitioning
element) rst. Pivot element can be a random number from a given
fi
unsorted array.● Sort the given array according to pivot as the
smaller or equal to pivot comes into the left array and greater than or
equal to pivot comes into the right array.● Now select the next pivot
to divide the elements and sort the sub arrays recursively.
Features ● Developed by C.A.R. Hoare ● E cient algorithm ●
ffi
NOT stable sort ● Signi cantly faster in practice, than other
fi
algorithms Algorithm
ALGORITHM Quicksort (A[ l …r ])
//i/p: A sub-array A[l..r] of A[0..n-1],de ned by its left and right indices l
fi
and r //o/p: The sub-array A[l..r], sorted in ascending order
if l < r
Quicksort(A[l..s-1])
Quicksort(A[s+1..r]
i→l
j→r + 1;
Repeat
swap(A[i], a[j])
//i/p: array A
if n > 1
Merge ( B, C, A )
I →0
j→0
k→0
if B[i] ≤ C[j]
A[k] →B[i]
i→i + 1
else
A[k] →C[j]
j→j + 1
k→k + 1
if i == p
else
Example :Apply merge sort for the following list of elements: 38, 27,
43, 3, 9, 82, 10
Analysis of merge sort: All cases having same e ciency as: O (n log n)
ffi
T (n) =T ( n / 2 ) + O ( n )T (1) = 0Space requirement: O (n) Advantages
● The number of comparisons carried out is almost ideal.●
Mergesort is never going to degrade to O (n2)● It is applicable to les
fi
of any size.Limitations ● Using extra memory O(n). Q4) What do you
mean by Strassen’s matrix?A4) : Strassen's MatrixThe standard
method of matrix multiplication of two n x n matrices takes T(n) =
O(n3). The following algorithm multiplies nxn matrices A and B: //
Initialize C.for i = 1 to nfor j = 1 to nfor k = 1 to n C [i, j] += A[i, k] *
B[k, j];Strassen’s algorithm is a Divide-and-Conquer algorithm that
beats the bound. The usual multiplication of two n x n matrices takes
If C=AB, then we have the following:c11 = a11 b11 + a12 b21c12 = a11 b12
+ b12 b22c21 = a21 b11 + a22 b21c22 = a21 b12 + a22 b22 8 n/2 * n/2
matrix multiples plus4 n/2 * n/2 matrix additions T(n) = 8T(n/2) +
O(n2)Plug in a = 8, b = 2, k = 2 →logba=3 → T(n)= O(n3)Strassen
showed how two matrices can be multiplied using only 7
multiplications and 18 additions:Consider calculating the following 7
products:q1 = (a11 + a22) * (b11 + b22)q2 = (a21 + a22) * b11q3 = a11*(
b12 – b22)q4 = a22 * (b21 – b11)q5 = (a11 + a12) * b22q6 = (a21 – a11) *
(b11 + b12)q7 = (a12 – a22) * (b21 + b22)It turns out thatc11 = q1 + q4 –
q5 + q7c12 = q3 + q5c21 = q2 + q4c22 = q1 + q3 – q2 + q6 Code for
Implementation of Strassen’s Matrix Multiplication
#include <assert.h>
#include <stdio.h>
#include <time.h>
#de ne M 2
fi
#de ne N (1<<M)
fi
typedef double datatype;
typedefstruct
} corners;
int i, j;
int i, j;
A[i][j] = k;
int i, j;
int i, j;
printf("\n");
printf("}\n");
int i, j;
+ b.ca];
}
{
int i, j;
+ b.ca];
}}
*b = a;
if (i == 0)
b->rb = rm;
else
b->ra = rm;
if (j == 0)
b->cb = cm;
else
b->ca = cm;
}
{
mat P[7], S, T;
int i, j, m, n, k;
m = a.rb - a.ra;
assert(m==(c.rb-c.ra));
n = a.cb - a.ca;
assert(n==(b.rb-b.ra));
k = b.cb - b.ca;
assert(k==(c.cb-c.ca));
assert(m>0);
if (n == 1)
return;
nd_corner(a, i, j, &aii[i][j]);
fi
nd_corner(b, i, j, &bii[i][j]);
fi
nd_corner(c, i, j, &cii[i][j]);
fi
}
}
p.rb = p.cb = m / 2;
fi
for (i = 0; i < LEN(P); i++)
set(P[i], p, 0);
ST0;
ST0;
ST0;
ST0;
ST0;
add(B, B, T, bii[0][0], bii[0][1], p);
ST0;
int main()
mat A, B, C;
cornersai = { 0, N, 0, N };
corners bi = { 0, N, 0, N };
corners ci = { 0, N, 0, N };
srand(time(0));
return 0
ff
it may also be useful. ● It is to be remembered that algorithms for
faster multiplication exist. Q6) Write about max and min heap ?A6) :
Max and Min HeapHeap is the special tree based data structure in
which it has to satisfy the condition of a complete binary tree.Following
are two types of heap:Max-heap: In this key present at root node must
be greater among all key present at all of its children.A [PARENT (i)
≥A[i] For all the sub-trees in that binary tree, the same property must
be recursively valid. The minimum main factor present at the root is
present in a Min-Heap. Below is the binary tree that satis es all of Min
fi
Heap's assets.Min-heap: In this key present at root node must be
minimum among all key present at all of its children.A [PARENT (i)
≤A[i]For all the sub-trees in that binary tree, the same property must
be recursively valid. The maximum main factor present at the root is in
the Max-Heap. Below is the binary tree that satis es all of Min Heap's
fi
assets.
Fig 1: min and max heap Q7) What do you mean by heap sort?A7) :
Heap SortHeap Sort is one of the best in-place sorting strategies with
no worst-case quadratic runtime. Heap sort requires the creation of a
Heap data structure from the speci ed array and then the use of the
fi
Heap to sort the array. You should be wondering how it can help to sort
the array by converting an array of numbers into a heap data structure.
To understand this, let's begin by knowing what a heap is. Heap is a
unique tree-based data structure that meets the following special heap
characteristics:
Shape property : The structure of Heap data is always a Complete
Binary Tree, which means that all tree levels are fully lled.
fi
2. Heap property : Every node is either greater or equal to, or less
than, or equal to, each of its children. If the parent nodes are larger
than their child nodes the heap is called Max-Heap and the heap is
called Min-Heap if the parent nodes are smaller than their child
nodes. The Heap sorting algorithm is split into two basic
components: ● Creating an unsorted list/array heap.● By
repeatedly removing the largest/smallest element from the heap, and
inserting it into the array, a sorted array is then created. Complexity
analysis of heap sortTime complexity :● Worst case : O(n*log n)●
Best case : O(n*log n)● Average case : O(n*log n) Space complexity :
O(1) Q8) Write any example of binary search ?A8) : Example of Binary
Search Let us explain the following 9 elements of binary search:
fi
the rst to pop from the heap. the heap is the largest element.
fi
Q10) What is the build heap ?A10) : Build Heap BUILDHEAP (array A,
int n) for i ← n/2 down to 1 do HEAPIFY (A, i, n) Heap-Sort Algorithm
HEAP-SORT (A)BUILD-MAX-HEAP (A)For I ← length[A] down to ZDo
exchange A [1] ←→ A [i]Heap-size [A] ← heap-size [A]-1MAX-
HEAPIFY (A,1) Analysis :Building a max-heap requires O(n) run time.
The Heap Sort algorithm makes a call to 'Construct Max-Heap' that we
use to address a new heap with O(n) time & each of the (n-1) calls to
Max-heap. We know it takes time to 'Max-Heapify' O (log n).The total
running time of Heap-Sort is O (n log n).