Lps 9
Lps 9
ASWATH S
22BPS1009
Problem Statement:
Given the coordinates of nn points in a 2-dimensional plane, design an
algorithm to identify and print all sets of points that are collinear. A set of
points is considered collinear if all the points in that set lie on the same straight
line. There may be more than one such set of collinear points.
Pseudocode:
function find_collinear_sets(points):
collinear_sets = []
n = len(points)
for i in range(n):
slope_map = empty dictionary
for j in range(i + 1, n):
slope = calculate_slope(points[i], points[j])
return collinear_sets
if dx == 0:
return 'vertical' // Return a unique value for vertical lines
else:
return dy / dx
Time Complexity:
The time complexity of this approach is O(n2)O(n2):
• For each point, we check slopes with n−1n−1 other points,
yielding O(n2)O(n2) comparisons.
• Inserting into and accessing elements in a dictionary is on
average O(1)O(1), so operations on the slope_map are manageable
within O(n)O(n) per base point.
Space Complexity:
The space complexity is O(n2)O(n2):
• We maintain a dictionary (slope_map) to store the slopes between each
pair, which can contain up to O(n2)O(n2) slope entries in the worst case.
• The output collinear_sets will store groups of points, requiring additional
space depending on the number of collinear sets found.
Proof of Correctness:
1. Completeness: This approach captures all sets of collinear points by
using slopes. If three or more points are collinear, they will have the
same slope with respect to any base point.
2. Uniqueness: Using a set for each slope in slope_map ensures that we
don't duplicate points within the same collinear set.
Conclusion:
This algorithm efficiently identifies all sets of collinear points among a set
of nn points in O(n2)O(n2) time. It leverages the property of slopes between
pairs of points to group collinear points together.
8. Given the coordinates of the end points of n line segments, design an
algorithm to retrun the line segments which intersect. If more than one pair of
line-segments intersect, your algorithm should return all such pairs. Analyse
your algorithm with the time complexity.
Problem Statement:
Given the coordinates of the endpoints of nn line segments in a 2-dimensional
plane, design an algorithm to identify and return all pairs of line segments that
intersect. Each line segment is defined by its two endpoints. If more than one
pair of line segments intersects, the algorithm should return all such pairs.
Pseudocode:
function find_intersections(segments):
events = []
for i in range(len(segments)):
segment = segments[i]
# Add enter and exit events for each segment
events.append((segment[0], 'enter', i))
events.append((segment[1], 'exit', i))
# Sort events by x-coordinates, with ties broken by y-coordinates
events.sort()
if event_type == 'enter':
# Add segment to active list and check for intersections
active_segments.insert(segment)
above, below = active_segments.find_neighbors(segment)
return intersecting_pairs
# General case
if o1 != o2 and o3 != o4:
return True
# Special cases for collinearity
if o1 == 0 and on_segment(p1, p2, q1): return True
if o2 == 0 and on_segment(p1, q2, q1): return True
if o3 == 0 and on_segment(p2, p1, q2): return True
if o4 == 0 and on_segment(p2, q1, q2): return True
return False
Explanation of Functions:
1. orientation(p, q, r): Determines the orientation of the
triplet (p,q,r)(p,q,r) using the cross product:
• 0 if the points are collinear
• 1 for clockwise
• 2 for counterclockwise
2. on_segment(p, q, r): Checks if the point qq lies on the
segment prpr when the points are collinear.
Time Complexity:
• Sorting the events takes O(nlogn)O(nlogn).
• Each event is processed in O(logn)O(logn) time using the
balanced BST, so processing all events takes O(nlogn)O(nlogn).
• Checking for intersections with neighbors is an O(1)O(1) operation
in the average case, given that we only check the immediate
neighbors.
Overall, the time complexity is O(nlogn)O(nlogn).
Space Complexity:
• The space complexity is O(n)O(n) to store the events and the
active segments in the BST.
Proof of Correctness:
1. Completeness: By processing each segment entry and exit, we ensure
that all potential intersections are considered.
2. Correctness: The orientation tests correctly capture the intersection
conditions for any two line segments, including edge cases like
collinearity.
Conclusion:
This sweep-line algorithm efficiently detects intersecting pairs among nn line
segments in O(nlogn)O(nlogn) time, leveraging event processing and
orientation checks to limit unnecessary comparisons. This approach is optimal
for this type of geometric intersection problem.
Problem Statement:
Given the coordinates of nn points in a 2-dimensional plane, design an
algorithm to determine whether these points can form a simple polygon. A
polygon is simple if its edges do not intersect or touch each other, except at
their endpoints.
Pseudocode:
function is_simple_polygon(points):
n = len(points)
edges = []
# General case
if o1 != o2 and o3 != o4:
return True
# Special cases
if o1 == 0 and on_segment(p1, p2, q1): return True
if o2 == 0 and on_segment(p1, q2, q1): return True
if o3 == 0 and on_segment(p2, p1, q2): return True
if o4 == 0 and on_segment(p2, q1, q2): return True
return False
Time Complexity:
• Constructing the edges: O(n)O(n)
• Checking for intersections involves checking O(n2)O(n2) pairs of
edges, so the overall time complexity is O(n2)O(n2).
Space Complexity:
• The space required is O(n)O(n) to store the edges, so the space
complexity is O(n)O(n).
Proof of Correctness:
• Completeness: The algorithm checks all pairs of non-adjacent edges for
intersection. If any intersection is detected, it correctly returns False.
• Correctness: The orientation method correctly identifies whether two
line segments intersect, including special cases such as collinear points.
Conclusion:
The algorithm efficiently determines if nn points form a simple polygon by
checking all non-adjacent edges for intersections. The O(n2)O(n2) time
complexity is feasible for moderate values of nn, and the algorithm ensures
correctness through comprehensive geometric checks.