CG Midsem PDF
CG Midsem PDF
CG Midsem PDF
Assignment
Ritajit Majumdar
Roll: 91/CSE/111006
June 18, 2014
Contents
1 Problem 2
2 Problem 1
2.1 Part a . . . . . . . . . . . . . . . . . . . . . . . .
2.2 Part b . . . . . . . . . . . . . . . . . . . . . . . .
2.3 Part c . . . . . . . . . . . . . . . . . . . . . . . .
4
4
5
6
3 Problem 3
4 Problem 4
5 Problem 5
10
Problem 2
Let S be a set of n disjoint line segments in the plane, and let p be a point
not on any of the line segments of S. Determine all line segments of S that
p can see, that is, all line segments of S that contain some point q so that the
open segment pq doesnt intersect any line segment of S. Give an O(nlogn)
time algorithm for this problem that uses a rotating half-line with its endpoint
at p.
Solution
Input: Set S = {S1 , S2 , . . . , Sn } of n disjoint straight lines, a point P .
Output: All line segments Si seen from P .
Consider a set N = {Si1 ,Si2 } 0 < i n where Si1 is the start point and
Si2 is the end point of the line segment Si . Let us define event point as the
start and end points of the straight lines. So for n straight lines, there will
be 2n event points. I shall explain the upper half in the algorithm, the lower
half will follow similarly.
Algorithm:
1. Draw a horizontal line passing through P . Let the y-value of this line
be yp .
2. Rotate the half-line counter clock wise in upward direction, stopping
only at the event points.
3. Store the event points as they arrive in a queue. Any data structure will
be suitable where the first event point will be processed first. Queue
seemed most appropriate.
1
4. Now, arrive at each event point popping them from the queue.
5. Stop the half line at the first event point. Include the straight line as
the one seen.
6. Stop at the next point as they are popped. If it is the point of the same
straight line already entered as seen, then proceed without any action.
Otherwise, check the y-value with the y-value of the previous point. If
the y-value is less than the previous one, mark the straight line as seen.
Otherwise, store the straight line.
N.B. This will work because no lines intersect. y-value more means the
straight line is above the current one and cant be seen, at least till
now.
7. Check for the stored stright line. Suppose we reach the end point of
one straight line without entering any other line. Now if the next event
point belongs to the stored straight line, mark it as seen. Otherwise, if
the x-value of the next starting point is less than the end point of the
stored line, then also mark it as seen.
N.B. This is because, since there is no overlapping of lines, there is a
gap between two lines. So the stored line will be visible from that gap.
A condition has been shown in the diagram.
8. Continue this process for all points above the half-line.
9. For the lines below the half-line, start from right and continue clock
wise downward.
The time complexity of this algorithm The inital sorting takes O(nlogn) time.
Now for each line, there are 2 event points. So for n straight lines, there will
be 2n event points, leading to a complexity of O(n).
So the final time complexity is - O(nlogn) + O(n) = O(nlogn).
2
2.1
Problem 1
Part a
You are given a convex polygon P in the plane having nP sides and an xmonotone polygonal chain Q having nQ sides. Give a bound on the maximum
number of intersections that might occur between the edges of P and Q?
Solution
In the following figure, I have taken nP = 7 and nQ = 8.
2.2
Part b
Solution
If P and Q has np and nq vertices respectively, then their respective number of edges will be np + 1 and nq + 1.
Now, between two intersection points, there will be atleast one vertex of
either P and Q. But this vertex is not the first or last vertex of the chain
since on both sides of it, there are intersections. So maximum number of
such points is
5
(np + 1) 2 + (nq + 1) 2 = np + nq 2
Thus after the first intersecting point, there may be atmost np + nq 2
intersecting points. So maximum number of intersections is (np +nq 2)+1 =
np + nq 1
2.3
Part c
Given P and Q, two monotone polygonal chains with different directons, give
a bound on the maximum number of intersections that might occur between
the edges of P and Q.
Solution
Consider the following situation
Problem 3
You are given a set of n vertical line segments in the plane. Present an
efficient algorithm to determine whether there exists a line that intersects all
of these segments. (Hint: O(n) time is possible.) Justify your algorithms
correctness and derive its running time.
Solution
Input: A set S of n vertical lines. S ={S1 , S2 , , Sn }
Output: A straight line l that intersects all the n lines, if one exists.
It should be noted that the straight line cannot be vertical, then it will
not intersect vertical lines. So we can take the equation of the straight line
to be y = mx + c. If the line is horizontal, then we will have c = 0 and a
definite value of mx, giving the equation y = const.
Moreover, each straight line in set S is denoted by two pairs of points,
one start point and one end point, i.e. the ith straight line will be trated as
(xi , yi1 ) and (xi , yi2 ).
Algorithm:
7
Find the x-value of the equation of the straight line from set S.
4.
Put the x-value in the equation of line l and find the corresponding
y-value.
5.
The y-value may be within the line segment from set s. In that
case there is an equality, otherwise inequality (y-value is greater or less
than the maximum and minimum y-values of straight line respectively)
accordingly.
Problem 4
Solution
For every edge ei , draw the corresponding half plane hi whose boundary
lies on the line ei . So for n edges, there will be n half planes h1 , h2 , . . . , hn .
Define H = h1 h2 . . . hn .
If H is not null, then it can be claimed that the polygon is start shaped.
This is because if H is not null, then we can choose a point q H such that
it can see every edge. Thus the polygon is start-shaped, otherwise it is not.
Equation of the lines representing the edges can be found in linear time.
Moreover, once the half-planes are constructed, the problem of finding whether
H is null is a problem of linear programming which may be solved in linear
time.
So total time complexity is O(n).
10
Problem 5
The code for the problem has been attached with the mail. A test case and
the corresponding output is also given along with it.
11