Answered step by step
Verified Expert Solution
Question
1 Approved Answer
You are given a set of n points x 1 , . . . , xn and a set of n segments [ a 1
You are given a set of n points x xn and a set of n segments a ban bn on a line, where xi ai bi are all integers. We want to output k k kn where ki is the number of segments that contain xi More formally, let X n be the array storing the points. Moreover, let I n denote an array storing the segments such that each entry Ii stores two values Iileft and Iiright. We want to write an algorithm that computes K n with Ki being the number of segments containing the point Xi In the following, assume that all the points, starting points, and endpoints are distinct.
a Assume you solve the problem using a naive algorithm which iterates through all pointsegment pairs, what is the running time?
In the following, we develop a more clever algorithm. To this end, we first create an auxiliary array Cn that stores the points as well as start and end points of the intervals. Each entry Cj stores three values: Cjvalue that is an integer, Cjtype that is either of the literals leftright or point and a value Cjpointer that stores an index between and n into one of the original arrays.
b Write the pseudocode of a procedure GenerateCXI,n that fills the above described array C based on the arrays X and I.
c Assume now that the above described array C has been generated and then sorted according to the value field, ie such that Civalue Ci value for all i from to n Write a procedure ComputeKC n which computes the array K n in time Theta n by iterating over the array C once.
d Assume you use the best sorting algorithm you know from the lecture so far, what is the overall running time when combining the solutions from parts b and c to solve the overall problem?
e Argue the correctness of part c using a suitable loop invariant.
f Now assume that collisions between points and start or endpoints of the intervals can happen, as well as collisions between multiple intervals. For instance, we could have that Xi Ijleft, that Iileft Ijright, or that Iileft Ijleft for some i and j Show that if we do not make any assumptions on how the sorting algorithm breaks ties, that then our algorithm is incorrect. To this end, provide an example input X and I and a sorted corresponding C such that your algorithm from part c produces the wrong result
g Generalize your algorithm from part b in such a way that the combined algorithm, with unmodified part c correctly works even in the presence of collisions. The format of the array C should remain unchanged and we still do not make any assumption on how the sorting algorithm breaks ties. Hint: You may consider tweaking the values stored in Civalue.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started