Design and Analysis of Algorithms Lab - 5
Design and Analysis of Algorithms Lab - 5
(BCSE204P)
Faculty: - Dr Malini S
Digital Assignment V:
Graham Scan, Jarvis March & Lines Int.
AIM:
The aim of the Convex Hull algorithm using Graham Scan is to
find the smallest convex polygon that encompasses all given
points in a plane.
ALGORITHM:
• Choose the point with the lowest y-coordinate (if tied, choose the
leftmost).
• Sort the points by the angle they make with the chosen point, in
counterclockwise order.
• Initialize an empty stack and push the first three points onto it.
• For each remaining point:
• Pop the top point from the stack until the angle formed by the last
two popped points and the current point makes a left turn.
• Push the current point onto the stack.
• The stack now contains the points forming the convex hull.
function GrahamScan(points[])
if size(points) < 3 then
return empty set // Convex Hull is not possible
// Find the point with the lowest y-coordinate (if tied, choose the
leftmost)
lowest_point = point with the lowest y-coordinate
if multiple points have the same lowest y-coordinate
lowest_point = point with the lowest x-coordinate among them
// Sort the points by the polar angle with respect to the lowest point
sort points by polar angle with respect to lowest_point
// Initialize an empty stack and push the first three points onto it
stack.push(points[0])
stack.push(points[1])
stack.push(points[2])
AIM
The aim of the Convex Hull algorithm using Jarvis March, also known as the
Gift Wrapping algorithm, is to find the smallest convex polygon that
encompasses all given points in a plane.
ALGORITHM
The Jarvis March algorithm proceeds as follows:
• Choose the point with the lowest y-coordinate (if tied, choose the
leftmost) as the starting point of the convex hull.
• Initialize an empty list to store the convex hull points.
• Repeat until we reach the starting point again:
• For each point in the set, find the point that makes the smallest
counterclockwise angle with the current point.
• Add this point to the convex hull list.
• Update the current point to the point just added.
• Return the list of convex hull points.
PSEUDOCODE:
function JarvisMarch(points[])
if size(points) < 3 then
return empty set // Convex Hull is not possible
start_point = point with the lowest y-coordinate (if tied, choose the leftmost)
current_point = start_point
convex_hull = empty list
repeat
next_point = any point in points
for each point in points
if point is not current_point and forms a smaller counterclockwise
angle with current_point than next_point
next_point = point
add next_point to convex_hull
current_point = next_point
until current_point equals start_point
return convex_hull
C/C++ CODE:
OUTPUT:
VERIFICATION STATUS
TIME COMPLEXITY ANALYSIS
• In the worst case, where all points are on the convex hull, and each
point is visited once, the time complexity is O(nh), where n is the
number of input points and h is the number of points on the convex
hull.
• In the average case, the time complexity is O(nh), where n is the
number of input points and h is the number of points on the convex
hull.
Q3.) LINE INTERSECTION CODE
AIM
The aim of the Line Segment Intersection algorithm is to find all
intersections between a set of given line segments.
ALGORITHM
→The Line Segment Intersection algorithm typically involves sorting the line
segments and then iterating over them to identify intersections. One common
approach is the Bentley-Ottmann algorithm:
PSEUDOCODE:
function LineSegmentIntersection(segments[])
events = create a list of segment endpoints and intersection points
sort events by their x-coordinate
status_structure = empty balanced binary search tree
for each event in events
if event is the left endpoint of a segment
insert segment into status_structure
check for intersections between segment and its neighbors in
status_structure
else if event is the right endpoint of a segment
remove segment from status_structure
check for intersections between its neighbors in status_structure
else if event is an intersection point
output intersection point
swap the positions of the intersecting segments in status_structure
if needed
C/C++ CODE:
The points are given in main function, based on the Q asked, it’ll confirm
whether or not:
CASE OUTCOME
For the given point it asks what we want to check case1, case2 or case3
OUTPUT
VERIFICATION STATUS
TIME COMPLEXITY ANALYSIS
→ Sorting the events by their x-coordinate takes O(n log n) time, where n
is the number of endpoints.