0% found this document useful (0 votes)
48 views

DAA Module-2

Divide and conquer is a general algorithm design technique that works as follows: 1) A problem is divided into smaller instances of the same problem. 2) The smaller instances are solved recursively or using a different algorithm. 3) The solutions to the smaller instances are combined to solve the original problem. Quicksort uses divide and conquer to sort an array by partitioning it around a pivot element and recursively sorting the subarrays. Merge sort also uses divide and conquer to sort an array by dividing it into halves, sorting the halves, and merging the results.

Uploaded by

Zeha 1
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)
48 views

DAA Module-2

Divide and conquer is a general algorithm design technique that works as follows: 1) A problem is divided into smaller instances of the same problem. 2) The smaller instances are solved recursively or using a different algorithm. 3) The solutions to the smaller instances are combined to solve the original problem. Quicksort uses divide and conquer to sort an array by partitioning it around a pivot element and recursively sorting the subarrays. Merge sort also uses divide and conquer to sort an array by dividing it into halves, sorting the halves, and merging the results.

Uploaded by

Zeha 1
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/ 48

Module-2 Divide and Conquer

• Divide and Conquer: General method, Binary


search, Recurrence equation for divide and
conquer, Merge sort, Quick sort, Strassen’s
matrix multiplication,
• Decrease and Conquer Approach:
– Topological Sort.
• Divide-and-conquer is the best-known general
algorithm design technique.
Divide-and-conquer algorithms work according to
the following general plan:
1. A problem's instance is divided into several smaller
instances of the same problem, ideally of about the
same size.
2. The smaller instances are solved (typically
recursively, though sometimes a different algorithm
is employed when instances become small enough).
3. If necessary, the solutions obtained for the smaller
instances are combined to get a solution to the
original instance.
Divide and Conquer
Control Abstraction for Divide
&Conquer
Quick Sort
• Quicksort divides (or partitions) array according to
the value of some pivot element A[s]

• Divide-and-Conquer:
– If n=1 terminate (every one-element list is already sorted)
– If n>1, partition elements into two; based on pivot
element
• Prtition, a situation where all the elements
before some position s are smaller than or
equal to A[s] and all the elements after
positions are greater than or equal to A[s]:
• Since we want elements smaller than the pivot to
be in the first part of the subarray, this scan skips
over elements that are smaller than the pivot and
stops on encountering the first element greater
than or equal to the pivot.

• The right-to-left scan, denoted below by index j,


starts with the last element of the subarray. Since
we want elements larger than the pivot to be in
the second part of the sub array, this scan skips
over elements that are larger than the pivot and
stops on encountering the first element smaller
than or equal to the pivot.
I) i < j, we simply exchange A[i] and A[j] and
resume the scans by incrementing i and
decrementing j,

II) If the scanning indices have crossed over, i.e.,


i > j, we will have partitioned the array after
exchanging the pivot with A[J]:
III) Finally, if the scanning indices stop while
pointing to the same element, i.e., i = j, the
value they are pointing to must be equal top
Thus, we have the array partitioned, with the
split position s = i = j:
• Apply quicksort to sort the list
5,3,1,9,8,2,4,7
• Best case-Average C(n) =nlogn;
• Worst case C(n)= (n2)
1) Apply quicksort to sort the list
65 ,70, 75, 80, 85, 60, 55, 50 ,45

2) Apply quicksort to sort the list


E, X, A, M, P, L, E
Solving recurrence relation using
Master theorem
It states that, in recurrence equation T(n) = aT(n/b) + f(n),
If f(n)∈ Θ (nd ) where d ≥ 0 then

Analogous results hold for the Ο and Ω notations, too.


Example:
Here a = 2, b = 2, and d = 0; hence, since a >bd,
Merge Sort
• Merge sort is a perfect example of divide-and
conquer technique.
• It sorts a given array by
– dividing it into two halves,
– sorting each of them recursively, and
– then merging the two smaller sorted arrays into a single
sorted one.
Original Sequence Sorted Sequence
18 26 32 6 43 15 9 1 1 6 9 15 18 26 32 43

18 26 32 6 43 15 9 1 6 18 26 32 1 9 15 43
43

18 26 32 6 43
43 15
15 9 1 18 26 6 32 15 43 1 9

18 26 32 6 43
43 15 9 1 18 26 32 6 43 15
15 9 1

18 26 32 6 43 15 9 1
Max Min
Problem statement
• Given a list of n elements, the problem is to find the
maximum and minimum items.
A simple and straight forward algorithm to achieve
this is given below.
Binary Search
• Problem definition:
• Let ai, 1 ≤ i ≤ n be a list of elements that are sorted in
non-decreasing order.
• The problem is to find whether a given element x is
present in the list or not.
– If x is present we have to determine a value j (element’s
position) such that aj=x.
– If x is not in the list, then j is set to zero.
• Solution:
• Let P = (n, ai…al , x) denote an arbitrary
instance of search problem
• - where n is the number of elements in the
list,
• - ai…al is the list of elements and
• - x is the key element to be searched
Binary Search
• Pseudocode
• Step 1: Pick an index q in the middle range [i, l] i.e. q = (n +1)/2
and compare x with aq.
• Step 2: if x = aq i.e key element is equal to mid element, the
problem is immediately solved.
• Step 3: if x < aq in this case x has to be searched for only
in the sub-list ai, ai+1, ……, aq-1. Therefore problem reduces to (q-
i, ai…aq-1, x).
• Step 4: if x > aq , x has to be searched for only in the
sub-list aq+1, ...,., al . Therefore problem reduces to (l-i,
aq+1…al, x).
Analysis

• Time Complexity
Recurrence relation (for worst case) T(n) = T(n/2) + c
STRASSEN'SMATRIXMULTIPLICATION
Matrix Multiplication
Direct Method:
• Suppose we want to multiply two n x n matrices, A
and B.
• Their product, C=AB, will be an n by n matrix and will
therefore have n2 elements.
• The number of multiplications involved in producing
the product in this way is Θ(n3)
• Divide and Conquer method for Matrix multiplication

• How many Multiplications?


• 8 multiplications for matrices of size n/2 x n/2 and 4 additions.
• Addition of two matrices takes O(n2) time. So the time
complexity can be written as T(n) = 8T(n/2) + O(n2) which
happen to be O(n3); same as the direct method
• n is a power of 2, that is,that there exists a
nonnegative integer k such that n = 2^k. In
case? n is not a power of two, then enough
rows and columns of zero scan be added to
both A and B so that the resulting dimensions
are a power of two
• O(n^3)
• Strassen has discovered a way to compute the
Cij s of using only 7 multiplications and 18
additions or subtractions. His method involves
first computing the seven n/2x n/2matrices P,
Q, R, S, T, U, and V as

You might also like