Cs 502

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 1

VUID: bc190402021

CS502
Assignment # 1
Solution:

To find the final maximal set of points from the given set of 2-dimensional space points 𝑃P, we can follow the plane sweep
algorithm provided. We'll use a stack to keep track of the maximal points encountered so far while sweeping a vertical line
from left to right on the x-axis.
Here's how we can implement the algorithm using the provided pseudo code and the given set of points:
1. Sort the points 𝑃P in increasing order by their x-coordinate.
2. Initialize an empty stack 𝑠s.
3. Iterate through each point in 𝑃P and perform the following steps: a. While the stack 𝑠s is not empty and the top
element's y-coordinate is less than or equal to the current point's y-coordinate, pop elements from the stack. b. Push the
current point onto the stack.
4. Output the contents of the stack, which represents the final maximal set of points.
5. Sort 𝑃P in increasing order by x-coordinate: 𝑃={(2,11),(3,4),(5,9),(5,3),(6,8),(9,6),(12,4),(13,10),(14,8),
(15,6)}P={(2,11),(3,4),(5,9),(5,3),(6,8),(9,6),(12,4),(13,10),(14,8),(15,6)}
6. Initialize an empty stack 𝑠s.
7. Start sweeping the vertical line from left to right:
1. At (2,11)(2,11): Stack = {(2,11)}{(2,11)}
2. At (3,4)(3,4): Stack = {(2,11),(3,4)}{(2,11),(3,4)}
3. At (5,9)(5,9): Stack = {(2,11),(3,4),(5,9)}{(2,11),(3,4),(5,9)}
4. At (5,3)(5,3): Stack = {(2,11),(5,9)}{(2,11),(5,9)}
5. At (6,8)(6,8): Stack = {(2,11),(5,9),(6,8)}{(2,11),(5,9),(6,8)}
6. At (9,6)(9,6): Stack = {(2,11),(5,9),(6,8),(9,6)}{(2,11),(5,9),(6,8),(9,6)}
7. At (12,4)(12,4): Stack = {(2,11),(5,9),(6,8),(9,6),(12,4)}{(2,11),(5,9),(6,8),(9,6),(12,4)}
8. At (13,10)(13,10): Stack = {(2,11),(5,9),(6,8),(9,6),(12,4),(13,10)}{(2,11),(5,9),(6,8),(9,6),(12,4),(13,10)}
9. At (14,8)(14,8): Stack = {(2,11),(5,9),(6,8),(9,6),(12,4),(13,10),(14,8)}{(2,11),(5,9),(6,8),(9,6),(12,4),(13,10),
(14,8)}
10. At (15,6)(15,6): Stack = {(2,11),(5,9),(6,8),(9,6),(12,4),(13,10),(14,8),(15,6)}{(2,11),(5,9),(6,8),(9,6),(12,4),(13,10),
(14,8),(15,6)}
8. Output the contents of the stack: {(2,11),(5,9),(6,8),(9,6),(12,4),(13,10),(14,8),(15,6)}{(2,11),(5,9),(6,8),(9,6),(12,4),
(13,10),(14,8),(15,6)}
Now, let's visualize the final maximal set of points on a graph, along with the stack from left to right:

Stack: (2,11) (5,9) (6,8) (9,6) (12,4) (13,10) (14,8) (15,6)


|________|______|______|______|________|________|______|
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
|________|______|______|______|________|________|______|
(2,0) (5,0) (6,0) (9,0) (12,0) (13,0) (14,0) (15,0)

Each segment between two points represents the y-coordinate range of the points lying in between. So, for example,
between (2,11) and (5,9), the points (3,4) and (5,3) lie. Similarly, between (12,4) and (13,10), the point (12,4) lies.
This visualization shows the final maximal set of points and the order in which they appear in the stack from left to right.

You might also like