Chapter Two

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

CHAPTER TWO

Divide and Conquer

By: Yeshiwas Y.(M.Tech)


2022 G.C
2. General Method

❑In divide and conquer approach, a problem is divided into


smaller problems, then the smaller problems are solved
independently, and finally the solutions of smaller problems are
combined into a solution for the large problem.
➢Given: a function to compute on n inputs the divide and conquer
strategy suggests splitting the input k distinct subsets 1<k<=n
yielding k sub problems. This sub problems must be solved and a
method must be found to combine sub solutions into a solution of
the whole.
CONT’D…

o Divide the problem into a number of sub problems that are


smaller instances of the same problem.
o Conquer the sub problems by solving them recursively. If the
sub problem sizes are small enough, however, just solve the
sub problems in a straightforward manner.
o Combine the solutions to the sub problems into the solution
for the original problem.

Typical Case of Divide and Conquer Problems


Application Of Divide And Conquer Approach

Following are some problems, which are solved using divide and
conquer approach.
• Binary search
• Finding the maximum and minimum of a sequence of
numbers
• Strassen’s matrix multiplication
• Merge sort
• Quick sort
• Selection sort
2.2. Binary Search
The binary search algorithm uses the divide-and-conquer strategy
to search key through an array
Binary search is a Θ(log n), highly efficient search algorithm, in a
sorted array.
• It works by comparing a search key K with the array’s middle
element A[m].
If they match, the algorithm stops; otherwise, the same operation is
repeated recursively for the first half of the array if K < A[m], and
for the second half if K > A[m].
• Unsuccessful Search
Search K = 10
l=0 r=12 m=6
l=0 r=5 m=2
l=0 r=1 m=0
l=1 r=1 m=1
l=1 r=0 STOP!!
Binary search technique searches “data” in minimum possible
comparisons. Suppose the given array is a sorted one, otherwise
first we have to sort the array elements.
• Then apply the following conditions to search a “data”.
1. Find the middle element of the array (i.e., n/2 is the middle
element if the array or the sub-array contains n elements).
2. Compare the middle element with the data to be searched,
then there are following three cases.
a) If it is a desired element, then search is successful.
b) If it is less than desired data, then search only the first half of
the array, i.e., the elements which come to the left side of the
middle element.
c) If it is greater than the desired data, then search only the
second half of the array, i.e., the elements which come to the
right side of the middle element.
• Repeat the same steps until an element are found or exhaust the
search area.
Algorithm for binary search3. Repeat step 4 and 5 while (LB
Let A be an array of n elements <= UB) and (A[mid] ! = data)
A[1],A[2],A[3],...... A[n]. “Data” is 4. If (data < A[mid])
an element to be searched. “mid” a) UB = mid–1
denotes the middle location of a
segment (or array or sub-array) of 5. Else
the element of A. LB and UB is the a) LB = mid + 1
lower and upper bound of the 6. Mid = int ((LB + UB)/2)
array which is under
consideration. 7. If (A[mid]== data)
a) Display “the data found”
8. Else
a) Display “the data is not
1. Input an array A of n elements found”
and “data” to be sorted
9. Exit
2. LB = 0, UB = n; mid = int
((LB+UB)/2)
Suppose we have an array of 7 elements
Following steps are generated if we binary search a data
= 45 from the above array.
Step 1:
LB = 0; UB = 6
mid = (0 + 6)/2 = 3
A[mid] = A[3] = 30
Step 2:
Since (A[3] < data) - i.e., 30 < 45 - reinitialize the variable
LB, UB and mid
LB = 3 UB = 6
mid = (3 + 6)/2 = 4
A[mid] = A[4] = 40
Step 3:
Since (A[4] < data) - i.e., 40 < 45 - reinitialize the variable
LB, UB and mid
LB = 4 UB = 6
mid = (4 + 6)/2 = 5
A[mid] = A[5] = 45
Step 4:
Since (A[5] == data) - i.e., 45 == 45 - searching is successful.
Example: Find 22 in the following array.
2.3 Find Maximum And Minimum
❑Problem Statement
The Max-Min Problem in algorithm analysis is finding the maximum
and minimum value in an array.
➢Solution
To find the maximum and minimum numbers in a given array
numbers[] of size n, the following algorithm can be used.
First we are representing the naive method and then we will
present divide and conquer approach.
❖Naïve Method
Naïve method is a basic method to solve any problem. In this
method, the maximum and minimum number can be found
separately. To find the maximum and minimum numbers, the
following straightforward algorithm can be used.
Algorithm: Max-Min-Element (numbers[])
max := numbers[1]
min := numbers[1]
for i = 2 to n do
if numbers[i] > max then
max := numbers[i]
if numbers[i] < min then
min := numbers[i]
return (max, min)
Analysis
The number of comparisons in Naive method is 2n - 2.
The number of comparisons can be reduced using the divide
and conquer approach. Following is the technique.
In divided and conquer approach, the array is divided into two
halves. Then using recursive approach maximum and minimum
numbers in each halves are found. Later, return the maximum
of two maxima of each half and the minimum of two minima of
each half.
• Max−Min(x,y) will return the maximum and minimum values of
an array numbers[x...y]
• Algorithm: Max - Min(x, y)
if x – y ≤ 1 then
return (max(numbers[x], numbers[y]), min((numbers[x],
numbers[y]))
else
(max1, min1):= maxmin(x, ⌊((x + y)/2)⌋)
(max2, min2):= maxmin(⌊((x + y)/2) + 1)⌋,y)
Return (max(max1, max2), min(min1, min2))
ANALYSIS
2.4 MERGE SORT
merge sort uses divide and conquer strategy and its time
complexity is O(nlogn).
Merge sort is a perfect example of a successful application of the
divide-and conquer technique. It sorts a given array A [O ... n - 1]
by dividing it into two halves A [0 .. \n/2]-1] and A [ ⎝n/2] .. n-1],
sorting each of them recursively, and then merging the two smaller
sorted arrays into a single sorted one.
1. Divide the array in to two halves.
2. Recursively sort the first n/2 items.
3. Recursively sort the last n/2 items.
4. Merge sorted items (using an auxiliary array).
Example: Sort The Following List
Using Merge Sort Algorithm.
2.5 Quick Sort

Quick sort is the fastest known algorithm. It uses divide and conquer
strategy and in the worst case its complexity is O (n2). But its
expected complexity is O(nlogn).
Algorithm:
1. Choose a pivot value (mostly the first element is taken as the
pivot value)
2. Position the pivot element and partition the list so that:
a. the left part has items less than or equal to the pivot value
b. the right part has items greater than or equal to the pivot
value
3. Recursively sort the left part
4. Recursively sort the right part
else
The following algorithm can be Right--;
used to position a pivot value and }
create partition.
Left=0; else
Right=n-1; // n is the total number {
of elements in the list if(Data[Left]>Data[Right])
PivotPos=Left; {
while(Left<Right) swap(data[Left], Data[Right]);
{ PivotPos=Left;
if(PivotPos==Left) Right--;
{ }
if(Data[Left]>Data[Right]) else
{ Left++;
swap(data[Left], Data[Right]); }
PivotPos=Right; }
Left++;
}
2.6 Selection Sort
• Selection sort algorithm finds the smallest element of the array and
interchanges it with the element in the first position of the array.
Then it finds the second smallest element from the remaining
elements in the array and places it in the second position of the
array and so on.
• Let A be a linear array of ‘n’ numbers,A [1],A [2],A [3] ...A [n].
Step 1: Find the smallest element in the array of n numbers A[1],
A[2], ...... A[n]. Let LOC is the location of the smallest number in the
array. Then interchange A[LOC] and A[1]by swap = A[LOC]; A[LOC]
= A[1];A[1] = Swap.
Step 2: Find the second smallest number in the sub list of n – 1
elements A [2] A [3]...... A [n – 1] (first element is already sorted).
Now we concentrate on the rest of the elements in the array. Again
A [LOC] is the smallest element in the remaining array and
• LOC the corresponding location then interchange A [LOC] and A
[2].Now A [1] and A [2] is sorted, since A [1] less than or equal to
A [2].
Step 3: Repeat the process by reducing one element each from
the array
Step n – 1: Find the n – 1 smallest number in the sub array of 2
elements (i.e., A(n–1), A (n)). Consider A [LOC] is the smallest
element and LOC is its corresponding position.
• Then interchange A [LOC] and A(n – 1). Now the array A [1], A
[2],A [3],A [4]…A [n] will be a sorted array.
• Following figure is generated during the iterations of the
algorithm to sort 5 numbers 34,8,64,51,32,21:

Analysis
How many comparisons?
(n-1)+(n-2)+...+1= O(n2)
3. Initialize j = i + 1 and repeat
Algorithm through step 4 if (j < n – 1)
Let A be a linear array of n 4. if (a[j] < min)
numbers A [1], A [2], A [3] … A (a) min = a[j]
[k], A [k+1]… A [n]. Swap be a (b) loc = j
temporary variable for swapping 5. if (loc ! = i)
(or interchanging) the position of
the numbers. Min is the variable to (a) swap = a[i]
store smallest number and Loc is (b) a[i] = a[loc]
the location of the smallest (c) a[loc] = swap
element. 6. . Display “the sorted numbers of
1. Input n numbers of an array A array A”
2. Initialize i = 0 and repeat 7. Exit
through step5 if (i < n – 1)
(a) min = a[i]
(b) loc = I

You might also like