0% found this document useful (0 votes)
19 views14 pages

Unit 2 DAA

The document is a question bank on the design and analysis of algorithms, focusing on concepts like divide and conquer, binary search, matrix multiplication, convex hulls, and triangulation. It includes explanations of various algorithms such as merge sort, Strassen's matrix multiplication, and the closest pair algorithm, along with their time complexities. The document also discusses the advantages and disadvantages of the divide and conquer strategy and provides insights into when it may fail.

Uploaded by

Om More
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)
19 views14 pages

Unit 2 DAA

The document is a question bank on the design and analysis of algorithms, focusing on concepts like divide and conquer, binary search, matrix multiplication, convex hulls, and triangulation. It includes explanations of various algorithms such as merge sort, Strassen's matrix multiplication, and the closest pair algorithm, along with their time complexities. The document also discusses the advantages and disadvantages of the divide and conquer strategy and provides insights into when it may fail.

Uploaded by

Om More
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/ 14

Design & Analysis of

Algorithms
(Unit 2: Question Bank)

1. What is divide & conquer? Explain with suitable example.


OR
What is divide and conquer? Explain advantages & disadvantages. Lists the factors on which
running time of divide and conquer is affected.
2. Explain binary search algorithm using iterative and recursive version.
3. Explain Matrix Multiplication algorithm with example. Give time complexity.
OR
Explain Strassen’s Matrix Multiplication algorithm with example. Give time complexity.
OR
Explain D&C Strassen’s algorithm and calculate its complexity.
4. Give the concept of closest pair. Explain the closest pair algorithm.
OR
Write the algorithm to find the closest pair between two points in a set of n points in a plane as an
example of Divide and Conquer.
5. Explain merge sort algorithm. Explain how D&C strategy is applicable to it. Also analyze the
algorithm.
6. What do you mean by triangulation? Explain different applications.
OR
What is triangulation? Explain different types of test performed on triangulation.
7. Explain Convex Hulls. Write algorithm for divide and conquer.
OR
Explain different types of Convex Hull algorithm.
OR
Explain the concept of convex hulls. Explain following algorithms with example to find convex
hulls on a plane of points.
a) Gift wrapping
b) Graham Scan
c) Merge Hull
d) Quick Hull
8. Where D&C does fails? Explain class of problems for which D&C are unstable.
OR
What are the drawbacks of Divide & Conquer strategy? Explain with one example.
Design & Analysis of
Algorithms
(Unit 2)

Convex Hull
 Given a set of points S, the convex hull problem is to find the smallest convex region that contains
all points in S.
 The boundary of the region is called the convex hull of S, and is usually specified by a set of points
and their neighbour relation.
 Convex hulls are well-defined in any dimension, a 2-dimensional example is given in below figure.

 The 2-D convex hull can be thought of as the shape that results when a rubber band is stretched to
surround a set of nails, one at each point in S.
 Similarly, the 3-D convex hull is similar to the shape that would be formed if a balloon were
stretched to tightly surround the points S.
 Algorithms for finding convex hulls based on D&C strategy.
 Various algorithms for finding convex hull are given below,

1. Gift wrapping
 The idea behind the giftwrapping algorithm (in 2-D) is to move from point to point along the convex
hull.
 To start with, an extreme point is chosen as the first point on the hull (this can be done easily by
choosing the point in S with largest x-value)
 Then, on each step, the point in S with the smallest angle a is chosen to be the next point in the hull,
where a is defined as the angle between a ray originating at the current point and continuing along
the ray that connects the previous and current points, and a ray connecting the current point and the
test point.
 This is explained clearly in below figure,

 Each step of the algorithm takes O (n) time.


 If all the points lie on the hull then the algorithm will take O (n2) time.
Design & Analysis of
Algorithms
(Unit 2)

2. Graham Scan Algorithm


 The Graham Scan algorithm constructs the convex hull in two pieces (the top and bottom), then
connects them together.
 For each half, it begins by sorting the points in decreasing order of their x-coordinates.
 Then, starting at the largest (or smallest) x, it scans through the points, building the hull along the
way.
 As it encounters a new point, it connects it to the previous point, assuming that it will be in the hull.
 If adding that particular point causes the hull to be non-convex, the algorithm adds a bridge to the
appropriate point to ensure that the hull remains convex.
 This is illustrated in below figure & the dashed lines are the bridges added.

 The running time for the Graham scan algorithm is T(n) = T(sort) + O(n), since the algorithm must
take n steps.

3. Mergehull
 The Mergehull algorithm is a divide-and-conquer algorithm that is similar in structure to merge sort.
 Mergehull has a trivial divide phase, and does most of its work during the merge phase.
 Mergehull's divide stage is simple: the algorithm merely splits the input set S into two subsets S1,
S2, by one of the coordinates.
 Then it recursively solves S1, and S2, (that is, it computes the convex hull for both S, and S).
 After the recursion phase, the algorithm must merge the two convex hulls for S1 & S2 into a single
convex hull for S.

 The complexity of merge hull is O(nlogn).

4. Quickhull
 The Quickhull algorithm is another variant on the recursive divide and conquer method for finding
convex hulls.
 Unlike the mergehull algorithm that we have just seen, quickhull concentrates its work on the divide
phase of the algorithm, and uses a trivial merge step.
 It is thus similar in structure to the Quicksort algorithm, and has similar time complexity.
Design & Analysis of
Algorithms
(Unit 2)

 Quickhull works (in 2-D) by locating three points A, B, and C on the convex hull of its input s (with
C above both A and B), then recursively refines the hull between AC and CB.
 Since both halves of the recursion meet at C, which is on the hull, the merge is essentially a no-op.
 When the algorithm completes, the upper half-hull between A and B will be complete.
 The algorithm is then repeated for the lower half-hull.
 Below figure details the process graphically.

 Following Algorithm is,


1. Find the farthest point from the line joining A and B. To do this the line-side test can be used, as it
returns the area of the triangle ABC, and the triangle with largest area is the one that has the farthest
point C.
2. Discard all points in triangle ABC, as they will not be on the boundary of the final hull.
3. Recurse on (A,C) and (C,B) to fully refine the hull between A and C, and between C and B.
4. Connect the two resulting hulls at C.
5. Repeat for the lower half-hull, and connect it at A and B.
 The worst-case running time of the Quickhull algorithm is O(n2).
Design & Analysis of
Algorithms
(Unit 2)

Triangulation
 Triangulation is a process that takes origin of space and divides it into subregions.
 The space may be of any dimension and the subregions typically are shaped as triangles.
 Triangulation is used in many Real world applications like finite element simulation in which partial
differential equation is simulated over continuous surface or space by first using a triangulation to
break the space into small discrete elements.
 Other applications includes,
1. Surface approximation: A continuous surface is approximated by a mesh of triangles.
This is used in graphical applications like face representation, etc.
2. Nearest neighbour structure identification: The triangulation establishes data identifying the nearest
neighbours of each point in the triangulated data set. This is potentially useful for clustering
application.

Line Side Test


 Given a point C and a line AB, the line side test answers the question on which side of line AB the
point C is.

 The line side test is easily implemented through a matrix determinant operation.
 Let the line be defined by points A = (xa,ya), B = (xb,yb) and the test point be C= (xc,yc)
 Therefore,

 The point C is above the line A->B iff D > 0.


 For Example, A=(0,0) B=(2,0) C=(1,1),

 in this case,
Design & Analysis of
Algorithms
(Unit 2)

In Circle Test
 Given four points A,B,C,D, the in-circle test answers the question whether D is inside the circle
define by A, B & C.

 Let A = (xa,ya), B = (xb,yb), C = (xc,yc), D = (xd,yd)


 The matrix determinant used for in circle test is:
Design & Analysis of
Algorithms
(Unit 2)

Merge Sort
 Merge Sort is a Divide and Conquer algorithm.
 It divides input array in two halves, calls itself for the two halves and then merges the two sorted
halves.
 The merge() function is used for merging two halves.
 The merge(arr, l, m, r) is key process that assumes that arr[l..m] and arr[m+1..r] are sorted and
merges the two sorted sub-arrays into one.
 The merge sort algorithm is given below,

MergeSort(arr[ ], l, r)
If r > l
1. Find the middle point to divide the array into two halves:
middle m = (l+r)/2
2. Call mergeSort for first half:
Call mergeSort(arr, l, m)
3. Call mergeSort for second half:
Call mergeSort(arr, m+1, r)
4. Merge the two halves sorted in step 2 and 3:
Call merge(arr, l, m, r)

 Consider an array which is unsorted, after applying merge sort,

Time Complexity of Merge Sort


 Assume that time taken to merge proportional to n, the time taken by merge sort is given by the
recursive formula,
T(n) = a when n=1, a is any constant
= 2 T(n/2) + cn when n>1, c is a constant
It implies an + cnlog2n -> O (n log2 n)
Design & Analysis of
Algorithms
(Unit 2)

Closest Pair Algorithm


 We are given an array of n points in the plane, and the problem is to find out the closest pair of
points in the array.
 This problem arises in a number of applications.
 For example, in air-traffic control, you may want to monitor planes that come too close together,
since this may indicate a possible collision.
 The following formula for distance between two points p and q.

√(x(i) – x(j))2 + (y(i) – y(j))2

 There are three possibilities:


1. The closest pair lie in P-left
2. The closest pair lie in P-right
3. The closest pair contains:
- One Point from P-Left
- One Point from P-Right
Design & Analysis of
Algorithms
(Unit 2)

 Thus with the help of above two algorithms, the distance between two closest point is determined.
Design & Analysis of
Algorithms
(Unit 2)

Strassen Matrix Multiplication


 Strassen’s matrix is a Divide and Conquer method that helps us to multiply two matrices of size n *
n.
 This method divide each of the matrices into four sub matrices each of dimensions n/2 * n/2.
 Strassen’s Matrix Multiplication formula is given below,

Where,

 In the above formula X, Y & C are the matrix of size N * N.


 a,b,c and d are submatrices of X of size N/2 * N/2.
 e,f,g and h are submatrices of Y of size N/2 * N/2.
 p1,p2,p3,p4,p5,p6 & p7 are the submatrices of size N/2 * N/2.

Time Complexity of Strassen’s Method


 Addition and Subtraction of two matrices takes O(N2) time.
 So time complexity of Strassen’s method is,
T(N) = 7T(N/2) + O(N2)
 O(NLog7) which is approximately O(N 2.8074).
Design & Analysis of
Algorithms
(Unit 2)

Binary Search
 Binary search algorithm works on the principle of divide and conquer.
 For this algorithm to work properly, the data collection should be in the sorted form.
 Binary search looks for a particular item by comparing the middle most item of the collection.
 If a match occurs, then the index of item is returned.
 If the middle item is greater than the item, then the item is searched in the sub-array to the left of the
middle item.
 Otherwise, the item is searched for in the sub-array to the right of the middle item.
 This process continues on the sub-array as well until the size of the subarray reduces to zero.
 Binary search is a fast search algorithm with run-time complexity of Ο (log n).

Iterative Version of Binary Search Algorithm


 The iterative binary search algorithm is given below,

 In the above algorithm, the array is divided into two parts with the help of middle.
 The searched element is first compared with the middle element and the searching goes on.
 The best case complexity of the algorithm is O (n) and the worst & average case is O (logn).
Design & Analysis of
Algorithms
(Unit 2)

Recursive Version of Binary Search Algorithm


 The recursive binary search algorithm is given below,

 In the above algorithm, the algorithm is called recursively until the element is searched.

Analysis of Iterative & Recursive Algorithm


 After practical implementation of Iterative & Recursive binary search algorithm, it is found that
recursive binary search algorithm is inefficient due to the extra recursive calls as compared to
iterative binary search algorithm.
 That is why iterative binary search algorithm is used which is divide & conquer strategy.
Design & Analysis of
Algorithms
(Unit 2)

Divide & Conquer


 Divide and Conquer is an algorithmic paradigm.
 In divide and conquer approach, the problem in hand, is divided into smaller sub-problems and then
each problem is solved independently.
 When we keep on dividing the subproblems into even smaller sub-problems, we may eventually
reach a stage where no more division is possible.
 Those "atomic" smallest possible sub-problem (fractions) are solved.
 The solution of all sub-problems is finally merged in order to obtain the solution of an original
problem.
 Divide and conquer algorithm consists of two parts:
1. Divide: Divide the problem into a number of sub problems. The sub problems are solved
recursively.
2. Conquer: The solution to the original problem is then formed from the solutions to the sub
problems (patching together the answers).

Control Abstraction of Divide and Conquer


 A 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.
 The control abstraction for divide and conquer technique is given below,

DANDC (P)
{
if SMALL (P) then return S (p);
else
{
divide p into smaller instances p1, p2, …. Pk, k >= 1;
apply DANDC to each of these sub problems;
return (COMBINE (DANDC (p1) , DANDC (p2),…., DANDC (pk));
}
}

 SMALL (P) is a Boolean valued function which determines whether the input size is small enough
so that the answer can be computed without splitting.
 If this is so function ‘S’ is invoked otherwise, the problem ‘p’ into smaller sub problems.
 These sub problems p1, p2, . . . , pk are solved by recursive application of DANDC.
 If the sizes of the two sub problems are approximately equal then the computing time of DANDC is:
T(n) = g(n) When n is small
= 2 T(n/2) + f (n) otherwise
where,
T (n) is the time for DANDC on ‘n’ inputs.
g (n) is the time to complete the answer directly for small inputs and
f (n) is the time for Divide and Combine
Design & Analysis of
Algorithms
(Unit 2)

Class of problems for which D&C are unstable


 All D&C problems do not exhibit optimal problem division.
 Consider the problem of visiting all the vertices of a graph in a simple cycle, starting from a chosen
vertex.
 The problem is to find the cycle that yields the smallest total edge-cost.
 We divide the problem into two parts-the set of vertices to be visited before the last vertex, and the
last vertex.
 The types of problems for which divide-and-conquer is not suitable are:
1. Where the solution of size n depends upon n sub-solutions, each of size (n-1).
2. Overlapping sub-problems

Timing Analysis of Divide & Conquer Algorithm


 For D&C algorithms the running time is mainly affected by 3 factors:
1. The number of sub-instances (a) into which a problem is split.
2. The ratio of initial problem size to sub-problem size (B)
3. The number of steps required to divide the initial instance and to combine sub solutions,
expressed as a function of the input size, n.

Advantages of Divide & Conquer


 In a perfect world, where the problem is easy to divide, and the sub-problem at some level is easy to
solve, divide and conquer can be optimal for a general case solution, like merge sort.
 Parallel availability, divide and conquer by it’s very nature lends itself well to parallel processing.

Disadvantages of Divide & Conquer


 Problem decomposition may be very complex and thus not really suitable to divide and conquer.
 Recursive nature of the solution may end up duplicating sub-problems, dynamic/memoized solutions
may be better in some of these cases, like Fibonacci.
 Recursion into small/tiny base cases may lead to huge recursive stacks, and efficiency can be lost by
not applying solutions earlier for larger base cases.

You might also like