Algorithm: Polygon max element in O(log n)

Question:

I want to find the vertice in convex polygon, which has the greatest x axis value.

The polygon is convex and random.
Its vertices are represented in array as tuples: (x,y).
The vertices from cartesian coordinate system to array are ordered in counterclockwise order.
The most left vertice on the x axis is the first element in the array.

I need to figure out the most efficient approach to find the most right vertice on the x axis (has greatest x value).

My idea:
If the vertice is convex, then in the array must be 2 ordered intervals, lets say from index 0 to index “s” and from index “s” to the last index. 1st interval is increasing the 2nd is decreasing – 1st is lower part of polygon, 2nd uppper.

(Im not sure if this assumotion is correct in any case)

Then I know that the “s” indexed element is the one with greatest x value.

Now i can use slightly modified binary search to find element on the “s” index (in each cycle decide if the middle element is in the 1st interval or the 2nd one and then do the binary search stuff..).

Can anyone tell me if my idea is right? Thank you

Asked By: Loosos

||

Answers:

When you iterate the vertices starting from an arbitrary vertex you will get a sequence that has a single internal local extremum (i.e. the sequence will be increasing until the vertex you are looking for, then decreasing and potentially then it will start increasing until the end). The shape will resemble something like this:

  /
 /  /
/

there are few edge cases to consider too:

  • if there is more than one vertex with max x value (in that case they will be right after one another, so you can do a simple check)
  • if you start from the optimal vertex, the shape will be more like /. To account for that compare the x value of the first/last vertex with what you find

Either way the basis of the solution you are looking for could be ternary search (or in geeksforgeeks) or hill climbing. Both will be able to achieve the log(n) target.

Answered By: Ivaylo Strandjev
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.