Brute Force and Divide-and-Conquer Algorithms
Brute Force and Divide-and-Conquer Algorithms
UNIT II
BRUTE FORCE AND DIVIDE-AND-CONQUER
Brute Force - Closest-Pair and Convex-Hull Problems-Exhaustive Search - Traveling
Salesman Problem - Knapsack Problem - Assignment [Link] and conquer
methodology – Merge sort – Quick sort – Binary search – Multiplication of Large Integers
– Strassen’s Matrix Multiplication-Closest-Pair and Convex-Hull Problems.
PART A
1. Define Brute Force.
Brute Force is a straightforward approach to solve a problem, which is directly
based on the problem statement and definition of the concepts.
Brute Force strategy is one of the easiest approach.
The convex hull of a set S of points is the smallest convex set containing S. (The
“smallest” requirement means that the convex hull of S must be a subset of any
convex set containing S.)
7. Define exhaustive search.
An exhaustive search, also known as generate and test, is a very general problem-
solving technique that consists of systematically enumerating all possible candidates
for the solution and checking whether each candidate satisfies the problem's
statement.
8. What is Travelling Salesman Problem?
The Travelling Salesman Problem (TSP) is an NP-hard problem in combinatorial
optimization studied in operations research and theoretical computer science.
Given a list of cities and their pairwise distances, the task is to find a shortest possible
tour that visits each city exactly once.
9. What is knapsack?
The knapsack problem, another well-known NP-hard problem.
The Knapsack problem is, given n items of known weights w1, . . . , wn and values
v1, . . . , vn and a knapsack of weight capacity W, find the most valuable subset of the
items that fits into the knapsack.
[Link] is assignment problem?
The assignment problem is one of the fundamental combinatorial optimization
problems in the branch of optimization or operations research in mathematics.
It consists of finding a maximum weight matching in a weighted bipartite graph.
11. Define the divide and conquer method.
Divide & conquer technique is a top-down approach to solve a problem.
The algorithm which follows divide and conquer technique involves 3 steps:
Divide the original problem into a set of sub problems.
Conquer (or Solve) every sub-problem individually, recursive.
Combine the solutions of these sub problems to get the solution of original
problem.
12. What is the binary search?
If ‘q’ is always chosen such that ‘aq’ is the middle element
(that is, q = [(n+1)/2)], then the resulting search algorithm is known as binary
search.
13. What is the time complexity of Binary search? June 2011 & 12
CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS UNIT 2
2
though for large ‘n’ its computing time is O (n ).
The most-used orders are numerical order and lexicographical order. Efficient
sorting is important for optimizing the use of other algorithms (such as search
and merge algorithms) that require sorted lists to work correctly;
More formally, the output must satisfy two conditions:
The output is in non-decreasing order (each element is no smaller than the
previous element according to the desired total order)
The output is a permutation (reordering) of the input.
22. What do you meant by Divide and conquer strategy? May 2013
Divide & conquer technique is a top-down approach to solve a problem.
The algorithm which follows divide and conquer technique involves 3
steps:
Divide the original problem into a set of sub problems.
Conquer (or Solve) every sub-problem individually, recursive.
Combine the solutions of these sub problems to get the solution of
original problem.
23. What are the merits of binary search?
A binary search or half-interval search algorithm finds the position of a specified
value (the input "key") within a sorted array.
In each step, the algorithm compares the input key value with the key value of
the middle element of the array.
A binary search halves the number of items to check with each iteration, so
locating an item (or determining its absence) takes logarithmic time
It is faster than the sequential search.
It requires lesser number of key comparisons than the sequential search.
24. Is merge sort stable sorting algorithm?
Yes, merge sort is the stable sorting algorithm.
A sorting algorithm is said to be stable if it preserves the ordering of similar
elements after applying sorting method.
And merge sort is a method which preserves this kind of ordering. Hence merge
sort is a stable sorting algorithm.
25. Give efficiency analysis of divide and conquer?
The efficiency of divide and conquer algorithms is given by recurrences of the
form. T(n) = T(n) n=1
aT(n/b) + f(n) n>1
Where a and b are known constants. We assume that T(1) is known and
n is a power of b ( n=bk).
CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS UNIT 2
28. What is the difference between quick sort and merge sort? May 2013
Every element of the list may get Only the mid element of the list is
compared with the key element. compared with key element.
29. What is the difference between sequential and binary search Apr 2013
Sequential technique binary search technique
CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS UNIT 2
This is the simple technique of searching This is the efficient technique of searching
an element an element
This technique does not require the list to This technique require the list to be
be sorted sorted. Then only this method is applicable
The worst case time complexity of this The worst case time complexity of this
technique is O(n) technique is O(log n)
Every element of the list may get Only the mid element of the list is
compared with the key element. compared with key element.
where f(n) is the time to divide n elements and to combine their solution.
33. What is called substitution method? Jun 2010
A substitution method is one, in which we guess a bound and then use
mathematical induction to prove our guess correct.
It is basically two step process:
Step1: Guess the form of the Solution.
Step2: Prove your guess is correct by using Mathematical Induction.
Example 1.
Solve the following recurrence by using substitution method.
Solution:
Step1: The given recurrence is quite similar with that of MERGESORT, you guess
the solution is
Or
0 1 2 3 4 5 6 7 8 9 10 11 12 13
Iteration 0:
Left = 0
Right = 13
Mid = (Left + Right) / 2
= (0 + 13) / 2
Mid = 6
Midelement = 54
Search key = 9
Since 9 < 54, search the element 9 in the left of midelement 54.
Iteration 1:
Left = 0
Right = 5
Mid = (Left + Right) / 2
= (0 + 5) / 2
Mid = 2
Midelement = 0
Search key = 9
Since 9 > 0, search the element 9 in the right of midelement 0.
Iteration 2:
CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS UNIT 2
Left = 3
Right = 4
Mid = (Left + Right) / 2
= (3 + 4) / 2
Mid = 3
Midelement = 7
Search key = 9
Since 9 > 7, search the element 9 in the right of midelement 7.
Therefore 9 is found in the position 4.
[Link] a brute force algorithm for computing the value of a polynomial
(AU april/may 2015)
Problem: Find the value of polynomial
p(x) = anxn + an-1xn-1 +… + a1x1 + a0 at a point x = x0
Algorithm:
x := x0
p := 0.0
for i := n down to 0 do
power := 1
for j := power * x
p := p + a:= 1 to i do
power [i] * power
return p
Efficiency: Q(n2)
40. Derive complexity of binary search algorithm. (AU april/may 2015)
Worst Case Analysis
The worst case includes all arrays that do not contain a search key.
The recurrence relation for
Cworst(n) = Cworst (n/2) + 1, for n > 1 ----- (1)
R.H.S
Cworst(n/2)+1 = log 2(2i/2 )+ 1
= log 2i + 1
= log 2 2i + 1+ 1
= 2 + log 2i
Cworst(n/2) =2 + log 2i
L.H.S = R.H.S
Hence
Cworst(n) = log 2n + 1 and
Cworst(i) = log 2i + 1 are same
Hence
Cworst(n) = Ө(log n )
PART – B
1. Discuss in detail about brute force algorithm.
Brute force
Brute Force is a straightforward approach to solve a problem, which is
directly based on the problem statement and definition of the concepts.
Brute Force strategy is one of the easiest approaches.
For example
Computing an : for a given number a and a non negative integer n , find
the exponentiation as follows
an = a * a* a* ….a* for n times
Computing n! : The n! can be computed as 1 *2 * 3….*n
Performing multiplication of two matrices.
Searching a key value from given list of elements.
Brute Force Algorithm
Two types of Brute force algorithm
1. Consecutive integer checking algorithm for computing
the greatest common divisor of two integer[gcd (m,n)]
2. Definition based algorithm for matrix multiplication.
Advantages of Brute Force Approach
1. It is applicable to a variety of problems.
2. A brute Force algorithm cab be useful for solving small size
instances of a problem.
3. A brute Force algorithm cab be serve an important
theoretical or educational purpose.
4. The brute force approach provides reasonable algorithms of
atleast some practical value with no limitation on instance
CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS UNIT 2
=2
= (n − 1)n ∈ θ(n2).
= 2[(n − 1) + (n − 2) + . . . + 1]
Of course, speeding up the innermost loop of the algorithm could only decrease
the algorithm’s running time by a constant factor, but it cannot improve its
asymptotic efficiency class.
Convex-Hull Problem
Definition:
Given a set S { p1, p2 ,p3, …pn} of points in the plan , the convex hull H(S) is the
smallest convex polygon in the plane that contains all of the points of S. the set S is
called as coves set .
A polygon is convex if and only if any two points from the set forming a line segment
with end points entirely within the polygon
CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS UNIT 2
For example
P2
P8 1
1
P5
1
P3 P4
1 1
2
a b
5 8 7 3
c d
1
Tour Length
a -> b -> c -> d -> a I = 2+8+1+7 = 18
a -> b -> d -> c-> a I = 2+3+1+5 = 11
CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS UNIT 2
optimal
a-> c -> b -> d -> a I = 5+8+3+7 = 23
a-> c -> d -> b -> a I = 5+1+3+2 = 11
optimal
a-> d ->b -> c -> a I = 7+3+8+5 = 23
a-> d -> c -> b -> a I = 7+1+8+2 = 18
I Wi Vi
1 7 $42
2 3 $12
3 4 $40
4 5 $25
CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS UNIT 2
Person 2 6 4 3 7
Person 3 5 8 1 8
Person 4 7 6 9 4
<1, 2, 3, 4> Cost = 9 + 4 + 1 + 4 = 18
<1, 2, 4, 3> Cost = 9 + 4 + 8 + 9 = 30
<1, 3, 2, 4> Cost = 9 + 3 + 8 + 4 = 24
<1, 3, 4, 2> Cost = 9 + 3 + 8 + 6 = 26
<1, 4, 2, 3> Cost = 9 + 7 + 8 + 9 = 33
<1, 4, 3, 2> Cost = 9 + 7 + 1 + 6 = 23
... ... etc
The no. of permutation for assignment problem is n!.
Example 2
Person 2 7 5 4 8
Person 3 6 9 2 9
Person 4 8 7 10 5
(a0 + ….an-1)
(a0 + ….an-1)
a ≥ 1 and b > 1
The recurrence for the running time T(n) is
T(n) = aT(n/b) + f(n) ,
which is called as general divide and conquer recurrence where,
f(n) is a function that accounts for the time spent on dividing the problem into
smaller ones and on combining their solutions.
The order of growth of T(n) depends on the values of the constants ‘a’ and ‘b’
and the order of growth of the function f(n).
For example, the recurrence equation for the number of additions is
a(n) = 2a(n/2) + 1
Advantages of divide and conquer
The time spent on executing the problem using divide and conquer is smaller
than other methods.
The divide and conquer approach provides an efficient algorithm in computer
science.
The divide and conquer technique is ideally suited for parallel computation in
which each sum problem can be solved simultaneously by its own processor.
5. Explain the Merge Sort algorithm with the help of illustrative
Example. Dec 2011/12/13 & May 2008/14
The merge sort is a sorting algorithm that uses the divide and conquer strategy.
Division is dynamically carried out.
Merging is the process of combining two or more files into a new sorted file.
Merge sort on an input array with n elements consists of three steps:
Divide: partition array into two sub lists s1 and s2 with n/2 elements each
Conquer: then sort sub list s1 and sub list s2.
Combine: merge s1 and s2 into a unique sorted group.
Merge sort is a perfect example of a successful application of the divide and
conquer technique.
It sorts a given array A[0…..n − 1] by dividing it into two halves A[0…..[n/2]−1]
and A[[n/2]…..n − 1].
It sorts each half separately by using recursive procedure, and
Then, merging the two smaller sorted arrays into a single sorted one.
Steps to be followed
The first step of the merge sort is to chop the list into two.
If the list has even length, split the list into two equal sub lists.
If the list has odd length, divide the list in two by making the first sub list one
CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS UNIT 2
8 3 2 7 1 5
9 4
8 2 9 7 1 5 4
3
8 3 2 9 7 1 5 4
3 8 2 9 1 7 4 5
2 3 8 1 4 5
9 7
1 2 3 4 5 7 8
9
An example of Merge Sort operation
ALGORITHM
Algorithm Mergesort(A[0..n − 1])
//Sorts array A[0..n − 1] by recursive mergesort
//Input: An array A[0..n − 1] of orderable elements
//Output: Array A[0..n − 1] sorted in nondecreasing order
If n > 1
copy A[0..n/2] − 1] to B[0..n/2] − 1]
copy A[[n/2]..n − 1] to C[0..[n/2]] − 1]
Merge sort(B[0..[n/2] − 1])
Mergesort(C[0..[n/2] − 1])
Merge (B, C, A) //see below
The above steps are continued until one of the two given array is exhausted.
Then the remaining elements of the other array are copied to the end of the next
array.
Algorithm Descriptive and Implementation
ALGORITHM Merge(B[0..p − 1], C[0..q − 1], A[0..p + q − 1])
//Merges two sorted arrays into one sorted array
//Input: Arrays B[0..p − 1] and C[0..q − 1] both sorted
//Output: Sorted array A[0..p + q − 1] of the elements of B //and C
i ← 0; j ← 0; k ← 0
while i<p and j<q do
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
copy C[j..q − 1] to A[k..p + q − 1]
else
copy B[i..p − 1] to A[k..p + q − 1]
Tree of recursive calls to Quicksort with input values l and r of subarray bounds and
split position s of a partition obtained.
ALGORITHM FOR QUICK SORT
ALGORITHM Quicksort(A[l..r])
//Sorts a subarray by quicksort
//Input: Subarray of array A[0..n − 1], defined by its left and
//right indices l and r
//Output: Subarray A[l..r] sorted in nondecreasing order
if l<r
s ←Partition(A[l..r]) //s is a split position
Quicksort(A[l..s − 1])
Quicksort(A[s + 1..r])
C(n) = 2 C(n/2) +n
Here f(n) ∈ n1 therefore d = 1
Now , a = 2 and b = 2
As from case 2 we get a = bd i.e. 2 = 21
We get ,
T(n) i.e C(n) = Θ (nd log n )
Cbest(n) = Θ (n log n)
Best case time complexity of quick sort is Θ (n log n)
Using substitution method
C(n) = C (n/2) + C (n/2) + n ----------( 1 )
C(n) = 2C (n/2) +n
Assume n = 2K since each time the list is divide into two equal halves . then equation
CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS UNIT 2
becomes,
C(2K) = 2C(2k /2) + 2k
C(2K) = 2C(2k -1) + 2k
Now substitute C(2k -1
) = 2C(2k-2) + 2k-1
C(2K) = 2[2C(2k-2) + 2k-1] + 2k
C(2K) = 22C(2k-2) + 2.2k-1 + 2k
C(2K) = 22C( 2k-2) + 2k + 2k
C(2K) = 22C( 2k-2) +2.2k
If we substitute C(2k -2) then ,
C(2K) = 22C( 2k -2) +2. 2k
C(2K) = 22[2 C(2k -3) + 2k –2
] + 2.2k
C(2K) = 23C(2k -3) +22. 2k –2
+ 2.2k
C(2K) = 23C(2k -3) + 2k + 2.2k
C(2K) = 23C(2k -3) + 3.2k
Similarly we can write
C(2K) = 24C(2k -4) + 4.2 k
----
C(2K) = 2kC(2k-k) + k.2 k
Cworst(n) = (n -1) + n
Cworst(n) = (n - 1) +( n-2) + ... + 2 + 1
But as we know
1 + 2+ 3 +---- + n = n (n + 1)/2 = ½ n 2
Cworst(n) ∈ θ(n2)
The time complexity of worst case of quick sort is θ (n2)
Average Case Analysis (random array)
Let Cavg(n) be the average number of key comparison made by Quick Sort.
The partition split can be happen in each position S (0≤S≤n-1) with the
probability 1/n.
The recurrence relation is
Application
Internal sorting of large data sets.
To improve the efficiency of the Quick sort various methods are used to choose
the pivot element.
One such method is called, median of three partitioning that uses the pivot
element as the median of left most, right most and the middle element of the
array.
7. Write an algorithm to perform binary search on a sorted list of elements.
Analyse the algorithm for the best case , worst case and average case.
May 2011 / Dec 2008
Or
What is divide and conquer strategy and explain the binary
search with suitable example problem. Dec 2011
Or
Differentiate sequential search from binary search [Link] 2009
The binary search algorithm is one of the most efficient searching techniques which
CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS UNIT 2
Example:
The list of element are 3,14,27,31,39,42,55,70,81,85,93,98 and searching for k=70 in
the list.
0 1 2 3 4 5 6 7 8 9 10 11 12
3 14 27 31 39 42 55 70 74 81 85 93 98
m- middle element
m = n div 2
= 13 div 2
m=6
0 1 2 3 4 5 6 7 8 9 10 11 12
3 14 27 31 39 42 55 70 74 81 85 93 98
m
0 1 2 3 4 5 6 7 8 9 10 11 12
3 14 27 31 39 42 55 70 74 81 85 93 98
CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS UNIT 2
l m
Since k> a[m] , then , l=7.
So, the search element is present in second half.
Now the array becomes
7 8 9 10 11 12
70 74 81 85 93 98
l r
m = (l + r) div 2
= 19 div 2
m=9
7 8 9 10 11 12
70 74 81 85 93 98
l m r
Since k< a[m] and 70 < 81
So, the element is present in the first half
Now, the array becomes
7 8
70 74
l r
m = (l + r) div 2
= (7+8) div 2
m=7
7 8
70 74
l, m r
Now k = a[m] and 70 =70
Hence, the search key element 70 is found in the position 7 and the search operation is
completed.
Algorithm Description and Implementation
Establish the array a[0….n-1] and the value to be found k.
Assign the l and r variables to the array limits.
While l<r do
Compute the middle position of the remaining array segment to be searched.
If the value found is greater than current middle then
Adjust l value according
else
CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS UNIT 2
16 5
. .
c=1+1
c =2
if n = 8 , then
c = log2 n + 1
= log2 8 + 1
=3+1
C=4
Average number of comparison made by the binary search is slightly smaller than
worst case.
Cavg(n) log2 n
The average number of comparison in the successful search is Θ(log 2n)
Advantages
In this method elements are eliminated by half each time. So it is very faster
than the sequential search.
It requires less number of comparisons than sequential search to locate the
search key element.
Disadvantages
An insertion and deletion of a record requires many records in the existing table
be physically moved in order to maintain the records in sequential order.
The ratio between insertion/deletion and search time is very high.
Applications of binary search
The binary search is an efficient searching method and is used to search desired
record from database
For solving nonlinear equations with one unknown this method is used.
Time complexity of binary search
Best case Average case Worst case
Θ(1) Θ(log2n) Θ(log2n)
This is the simple technique of searching This is the efficient technique of searching
an element an element
This technique does not require the list to This technique require the list to be
be sorted sorted. Then only this method is applicable
CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS UNIT 2
The worst case time complexity of this The worst case time complexity of this
technique is O(n) technique is O(log n)
Every element of the list may get Only the mid element of the list is
compared with the key element. compared with key element.
the a’s digits and the sum of the b’s digits minus the sum of c2 and c0
The 2 digit numbers are
a = a1 a0
b = b1 b0
Let perform multiplication operation with the help of formula given in equation (1)
c=a∗b
c= 23 * 14
Where a1 = 2, a0 = 3 , b1 = 1, b0 = 4
Let us obtain c0, c1, c2 values
c2 = a1 ∗ b1
=2*1
c2 = 2
c0= a0 ∗ b0
=3*4
c0 = 12
c1 = (a1 + a0) ∗ (b1 + b0) − (c2 + c0)
= (2 + 3) *( 1 + 4) – (2 +12)
= 5 * 5 – 14
c1 =25-14
c1 = 11
Therefore
a * b = c2 102 + c1 101 + c0
= 2 * 100 +11 * 10 +12
= 200 +110 +12
a * b = 322
We can generalize this formula as
c=a∗b
c = c2102 + c1101 + c0
where , n is total number of digits in the integer
c2 = a1 ∗ b1
c0= a0 ∗ b0
c1 = (a1 + a0) ∗ (b1 + b0) − (c2 + c0)
Analysis
In this method there are 3 multiplication operations 1 digit numbers
i.e c2 = a1 ∗ b1 --> multiplication 1
CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS UNIT 2
c0 = a0 ∗ b0 --> multiplication 2
c1 = (a1 + a0) ∗ (b1 + b0) − (c2 + c0)->multiplication 3
The multiplication of n digit numbers requires three multiplications of n/2 digit
numbers, the recurrence equation for the number of multiplication M(n) will be,
M(n)=3M(n/2), for n>1
And M(1)=1 where n = 1
k
Now put n=2 Solving it by backward substitutions for
M(2k)=3 M(2k /2)
M(2k)=3 M(2k-1)
=3[3 M(2k-2)]
=32 M(2k-3)
……….
=3k M(2k-k)
=3k M(20) Since 20 = 1
=3k
Using equation (3) , M(n) = 1
Therefore M(2k) = 3k ------ (4)
as n= 2k we get k = log2n ,equation (4)
M(n)= 3 log 2n
=n log 2 3 therefore a log b c = c log2 a
≈ n 1.585
M(n) ≈ n1.585
9. Write an algorithm for performing matrix multiplication using
Strassen’s Matrix Multiplication
The Strassen’s matrix multiplication algorithm finds the product C of two 2 by 2
matrices A and B with just seven multiplication as opposed to eight required by the
brute force algorithm.
It is accomplished by using the following formula.
C=A×B
Where
m1 = (a00 + a11) * (b00 + b11)
m2 = (a10 + a11) * b00
m3 = a00 * (b01 - b11)
m4 = a11 * (b10 - b00)
m5 = (a00 + a01) * b11
m6 = (a10 + a00) * (b00 + b01)
m7 = (a01 + a11) * (b10 + b11)
Thus, to multiply two 2 by 2 matrices, strassen’s algorithm makes seven
multiplication and 18 additions/subtractions where as the brute force algorithm
requires eight multiplication and for additions.
These numbers should not lead us to multiplying 2 by 2 matrices by strassen’s
algorithm.
Its importance stems from its asymptotic superiority as matrix order n goes to
[Link] A and B be two n-by-n matrices where n is a power of two.
If n is not a power of two, matrices can be added with rows and column of
[Link] matrix A,B and their product is divided into 4,n/2 by n/2 submatrices each as
follows
The value C00 can be computed as either a00 * b00 or a01 * b10 or as M1 + M4
– M5 + M7 , where M1 , M4 , M5 and M7 are found by strassen’s formula with the
CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS UNIT 2
Let S be the list of points inside the strip of width 2d around the separating line,
obtained from Q and hence ordered in non decreasing order of their y coordinate.
We will scan this list, updating the information about dmin, the minimum
distance seen so far, if we encounter a closer pair of points.
Initially, dmin = d, and subsequently dmin ≤ d. Let p(x, y) be a point on this list.
For a point p(x, y) to have a chance to be closer to p than dmin, the point must
follow p on list S and the difference between their y coordinates must be less than
dmin.
ALGORITHM EfficientClosestPair(P, Q)
//Solves the closest-pair problem by divide-and-conquer
//Input: An array P of n ≥ 2 points in the Cartesian plane //sorted in non
decreasing order of their x coordinates and an //array Q of the same points
sorted in non decreasing order of //the y coordinates
//Output: Euclidean distance between the closest pair of //points
if n ≤ 3
return the minimal distance found by the brute-force algorithm
else
copy the first _n/2_ points of P to array Pl
copy the same _n/2_ points from Q to array Ql
copy the remaining _n/2_ points of P to array Pr
copy the same _n/2_ points from Q to array Qr
dl←EfficientClosestPair(Pl, Ql)
dr←EfficientClosestPair(Pr, Qr)
d ←min{dl, dr}
m←P[_n/2_ − 1].x
copy all the points of Q for which |x − m| < d into array S[0..num − 1]
CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS UNIT 2
dminsq ←d2
for i ←0 to num − 2 do
k←i + 1
while k ≤ num − 1 and (S[k].y − S[i].y)2 < dminsq
dminsq ←min((S[k].x − S[i].x)2+ (S[k].y − S[i].y)2, dminsq)
k←k + 1
return sqrt(dminsq)
The algorithm spends linear time both for dividing the problem into two
problems half the size and combining the obtained solutions. Therefore, assuming as
usual that n is a power of 2, we have the following recurrence for the running time of
the algorithm:T (n) = 2T (n/2) + f (n),where f (n) ∈ Ө(n). Applying the Master
Theorem (with a = 2, b = 2, and d = 1), we get T (n) ∈ Ө(n log n).
The necessity to presort input points does not change the overall efficiency class
if sorting is done by a O(n log n) algorithm such as merge sort.
Convex-Hull Problem
Let S be a set of n>1 points p1(x1, y1), . . . , pn (xn, yn) in the Cartesian plane.
We assume that the points are sorted in non decreasing order of their x
coordinates, with ties resolved by increasing order of the y coordinates of the points
involved.
It is not difficult to prove the geometrically obvious fact that the leftmost point
p1 and the rightmost point pn are two distinct extreme points of the set’s convex hull
(Figure 5.8).
Let p1 pn be the straight line through point’s p1 and pn directed from p1to pn.
This line separates the points of S into two sets:
S1 is the set of points to the left of this line, and S2 is the set of points to the
right of this line. We say that point q3 is to the left of the line q1q2 directed from point
q1 to point q2 if q1 q2 q3 forms a counterclockwise cycle. Later, we cite an analytical
way to check this condition, based on checking the sign of a determinant formed by
the coordinates of the three points.
The points of S on the line p1 pn, other than p1 and pn, cannot be extreme
points of the convex hull and hence are excluded from further consideration.
CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS UNIT 2
If there is a tie, the point that maximizes the angle pmax p pn can be selected.
(Note that point pmax maximizes the area of the triangle with two vertices at p1and pn
and the third one at some other point of S1.)
Pmax
1
Pn
P1 1
1
Then the algorithm identifies all the points of set S1 that are to the left of the
line p1 pmax; these are the points that will make up the set S1,
The points of S1 to the left of the line pmax pn will make up the set S1
It is not difficult to prove the following:
pmax is a vertex of the upper hull.
The points inside p1 pmax pn cannot be vertices of the upper hull (and hence
can be eliminated from further consideration).
There are no points to the left of both lines p1 pmax and pmax pn.
Therefore, the algorithm can continue constructing the upper hulls of p1 U S1,1
Upmax and pmax U S1,2 U pn recursively and then simply concatenate them to get the
upper hull of the entire set p1 U S1 U pn.
CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS UNIT 2
If q1(x1, y1), q2(x2, y2), and q3(x3, y3) are three arbitrary points in the
Cartesian plane, then the area of the triangle q1q2q3 is equal to one-half of the
magnitude of the determinant
x1 y1 1
x2 y2 1 = x1 y2 + x3 y1 + x2 y3− x3 y2 − x2 y1− x1 y3
x3 y3 1
while the sign of this expression is positive if and only if the point q3 = (x3, y3)
is to the left of the line q1 q2.
Using this formula, we can check in constant time whether a point lies to the left
of the line determined by two other points as well as find the distance from the point to
the line.
Example 2:
The merge procedure requires finding a bridge between two hulls that are adjacent to
each other . concatenate left part of left hull and right part of right hull
CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS UNIT 2
Quick hull has the same Ө(n log n) worst-case efficiency as quick sort.
In the average case Ө(n2), however, we should expect a much better
performance.
The average-case efficiency of quick hull turns out to be linear. ‘n’ its computing
time is O(n2).
11. Trace the steps of merge sort algorithm for the elements 122, 25, 70,
175, 89, 90, 95, 102, 123 and also compute its time complexity. Dec 2012
Refer Part B – Q. No. 5
12. Explain the binary search algorithm with an example. And find
the best, average and worst case complexity. Dec 2012
Refer Part B – Q. No. 7
13. Explain merge sort problem using divide and conquer technique. Give an
example. Apr 2010
Refer Part B – Q. No. 5
14. Write a pseudo code using divide and conquer technique for finding the
position of the largest element in the array of N numbers. Jun 2014
CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS UNIT 2
12 2 8 71 4 23 6 89 56
12 2 8 71 4 23 6 89 56
12 2 8 71 4 23 6 89 56
12 2 8 71 4 23 6 89 56
Swapping
2 12 8 4 71 6 23 56 89
2 12 8 4 71 6 23 56 89
2 8 12 4 71 6 23 56 89
2 4 8 12 71 6 23 56 89
2 4 6 8 12 56 71 89
16. Distinguish between quick sort and merge sort and arrange the
following numbers in increasing order using merge sort (18, 29, 68, 32, 43,
37, 87, 24, 47, 50). Jun 2013
Quicksort
Quicksort is another divide and conquer sorting algorithm, proposed by C. A. R.
Hoare. Here, the workhorse is the partition operation. Given an array of n elements,
partition takes one element (known as the pivot) and places it in the correct position.
That is, the array will not be sorted, but all the elements less than the pivot will
CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS UNIT 2
be to the left of the pivot, and all the elements greater than the pivot will be to the
right. The partition operation can be performed in linear time.
Quicksort then works as follows. First, it performs the partition operation on the
input array. Assume the pivot is placed in position p.
Now, quicksort sorts the sub-arrays A[0 : p - 1] and A[p + 1 : n - 1], using
quicksort itself. Here again, the base case is an array of size 1.
Various ways of selecting the pivot:
The simplest is to just take the left-most element every time as the pivot. But,
this leads to a fatal weakness.
In this case, if the array is already sorted, the pivot will get placed in the left
most position always, and quicksort will be sorting sub-lists of size 0 and n - 1.
This leads to a time complexity of O(n^2), which is no better than bubble sort.
This can be mitigated by choosing a random element as the pivot, or using the
median of three elements. In this case, the worst case time of O(n^2) is extremely
unlikely.
In the best case, when the pivot is always placed in the middle, the time
complexity will be O(n log n).
Also, if the pivot always gets placed somewhere in the middle 50% (25% - 75%)
part of the array, quick sort will take time proportional to O(n log n), even as in the
best case.
Advantage of quick sort:
Even though the asymptotic complexities are O(n log n), the constant muliplier
(hidden by the Big Oh notation) is much smaller for quicksort, which subsequently is
appreciably faster than mergesort in almost all cases.
Regarding space complexity, the space complexity of quicksort is O(log n),
taking into account the stack space used for recursion.
Mergesort
The real work of mergesort is done by the merge operation. Given two sorted
sub-arrays, together having a total of n elements,the merge operation uses an
auxiliary array of size n to mergethem together into a single sorted array in linear time
i.e. O(n) in Big Oh notation.
Having this merge operation, mergesort works as follows.
Given an array of elements A, it sorts the left and right sub-arrays using
mergesort itself, and then merges them together into one single sorted array.
The base case is a sub-array of size 1, which is implicitly sorted by definition.
CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS UNIT 2
If we analyze the time complexity of mergesort, it is O(n log n) in all cases. That
is, the time taken to sort n elements grows proportionally to n log n.
Merge sort also needs an extra array of size n for the merge operation. So its
space complexity is O(n).
Also, quicksort cannot be implemented iteratively, unlike mergesort, where an
iterative implementation, sometimes called bottom-up mergesort, is possible.
18 29 68 32 43 37 87 24 47 50
18 29 68 32 43 37 87 24 47 50
18 29 68 32 43 37 87 24 47 50
18 29 68 32 43 37 87 24 47 50
18 29 68 32 43 37 87 24 47 50
18 29 68 32 43 37 87 24 47 50
18 29 68 32 43 37 87 24 47 50
18 29 68 32 43 24 37 87 47 50
24
18 29 32 43 68 24 37 47 50 87
18 24 29 32 37 43 47 50 68 87
17. Briefly explain the procedure for strassen’s matrix multiplication. Mar 14
Refer Part B – Q. No. 9
18. Write a pseudo code for divide & conquer algorithm for merging two
sorted arrays in to a single sorted one. Explain with [Link] Part B – Q. No. 5
CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS UNIT 2
It is not a difficult to get the maximal number in the first sub-array and the minimal
number in the second sub-array. How about to get the maximal difference of all pairs in
two sub-arrays?
They are actually sub-problems of the original problem, and we can solve them via
recursion. The following are the sample code of this solution:
int MaxDiff_Solution1(int numbers[], unsigned length)
{
if(numbers == NULL || length < 2)
return 0;
In the function MaxDiffCore, we get the maximal difference of pairs in the first sub-
array (leftDiff), and then get the maximal difference of pairs in the second sub-array
(rightDiff).
We continue to calculate the difference between the maximum in the first sub-array
and the minimal number in the second sub-array (crossDiff). The greatest value of the
three differences is the maximal difference of the whole array.
We can get the minimal and maximal numbers, as well as their difference in O(1) time,
based on the result of two sub-arrays, so the time complexity of the recursive solution
is T(n)=2(n/2)+O(1). We can demonstrate its time complexity is O(n).
IMPORTANT QUESTIONS
Part A
1. What is the time complexity of Binary search? June 2011 & 12
Part A – Refer Q. No. 13
2. Give the recurrence equation for the worst case behavior of
merge sort? Part A – Refer Q. No. 19 Dec 2010
3. What do you meant by Divide and conquer strategy? May 2013
Part A – Refer Q. No. 22
4. Give the time efficiency and drawback of merge sort algorithm?
Dec 2005
Part A – Refer Q. No. 27
5. What is the difference between quick sort and merge sort? May 2013
Part A – Refer Q. No. 28
6. What is the difference between sequential and binary search
Apr 2013
Part A – Refer Q. No. 29
7. List out two drawbacks of binary search algorithm. Dec 2007
Part A – Refer Q. No. 31
8. Give the control abstraction for divide and conquer. Dec 2012
Part A – Refer Q. No. 32
9. What is called substitution method? Jun 2010
Part A – Refer Q. No. 33
10. What is called optimal solution? Jun 2010
Part A – Refer Q. No. 34
11. What do you mean by divide and conquer strategy? Jun 2013
CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS UNIT 2
Part B
1. Write an algorithm for performing The Closest-Pair and
Convex-Hull Problems by Divide-and-Conquer 2005/2007/2010/2012/2013
Refer Part B – Q. No. 10
2. Write an algorithm to perform binary search on a sorted list of
elements. Analyse the algorithm for the best case , worst case
and average case. May 2011 / Dec 2008
Or
What is divide and conquer strategy and explain the binary
search with suitable example problem. Dec 2011
Or
Differentiate sequential search from binary search technique.
May 2009
Refer Part B – Q. No. 7
3. Briefly explain the procedure for strassen’s matrix
multiplication. Mar 2014
Refer Part B – Q. No. 9
4. Trace the steps of merge sort algorithm for the elements 122,
25, 70, 175, 89, 90, 95, 102, 123 and also compute its time
complexity. Dec 2012
Refer Part B – Q. No. 5
5. Explain the binary search algorithm with an example. And find
the best, average and worst case complexity. Dec 2012
Refer Part B – Q. No. 7
6. Explain merge sort problem using divide and conquer
technique. Give an example. Apr 2010
Refer Part B – Q. No. 5
7. Write a pseudo code using divide and conquer technique for
finding the position of the largest element in the array of N
numbers. Jun 2014
Refer Part B – Q. No. 4