Unit 2 DAA
Unit 2 DAA
Algorithms
(Unit 2: Question Bank)
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,
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.
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.
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.
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,
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.
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)
Thus with the help of above two algorithms, the distance between two closest point is determined.
Design & Analysis of
Algorithms
(Unit 2)
Where,
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).
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)
In the above algorithm, the algorithm is called recursively until the element is searched.
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)