0% found this document useful (0 votes)
9 views

Design and Analysis of Algorithms Lab - 5

Uploaded by

paul.r212003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Design and Analysis of Algorithms Lab - 5

Uploaded by

paul.r212003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Design and Analysis of Algorithms Lab

(BCSE204P)
Faculty: - Dr Malini S

Digital Assignment V:
Graham Scan, Jarvis March & Lines Int.

Date: 16th April, 2024

Rishabh Paul (21BCE2019)


(School of Computer Science and Engineering)
[email protected]
Q1.) CONVEX HULL USING GRAHAM SCAN

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:

The Graham Scan algorithm proceeds as follows:

• 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.

TIME COMPLEXITY ANALYSIS:

• Sorting the points by polar angle takes O(n log n) time.


• Removing collinear points takes O(n) time.
• The overall time complexity of the Graham Scan algorithm is O(n
log n)
PSEUDOCODE:

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])

// Process remaining points


for i from 3 to size(points) - 1
// Keep removing top while the angle formed by points next-to-top,
top, and points[i] makes a non-left turn
while orientation(next_to_top(stack), stack.top(), points[i]) !=
left_turn
stack.pop()
stack.push(points[i])

return stack // Contains points forming the convex hull


C/C++ CODE:
It asks to enter the no. of points, let’s say it equals 6. Then co-ordinates:
Point 1 (x y): 1 1
Point 2 (x y): 2 3
Point 3 (x y): 3 4
Point 4 (x y): 4 2
Point 5 (x y): 2 1
Point 6 (x y): 2 3
OUTPUT/RESULT:
Example2:
VERIFICATION STATUS
Q2.) CONVEX HULL USING JARVIS MARCH

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

The time complexity of the Jarvis March algorithm depends on the


number of points in the input set.

• 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:

• Sweep a vertical line from left to right across the plane.


• Maintain a status structure, often implemented as a balanced binary
search tree, to keep track of the line segments that intersect the sweep
line.
• When encountering an endpoint of a line segment:
• If it's the left endpoint, insert the segment into the status structure.
• If it's the right endpoint, remove the segment from the status structure.
• Check for intersections between the segment and its neighboring
segments in the status structure.
• Maintain a priority queue of events, such as segment endpoints and
intersection points, to process them in the order they occur.

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

CASE1 At least 2 lines with intersection

CASE2 At least 2 lines without intersection

CASE3 At least 2 lines with collinearity

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.

• Performing operations on the status structure, such as insertion,


deletion, and intersection checks, takes O(log n) time per event.

• Therefore, the overall time complexity of the Bentley-Ottmann


algorithm is O(n log n), where n is the number of segments.

You might also like