Minimum external rectangle

Source: Internet
Author: User

Source: http://www.pmit.com.cn/web/InfoDetailNoi.aspx? Id = 619 & classid = 51

Solution report for the smallest rectangle (rec1)


Author: FENG Hao time: 2007.10.11 document type/Source: NoI Journal

Summary:

A plane point set S is given, and a rectangle with the smallest area is obtained to include all the points of S.

Prerequisites:

Before solving this question, we should first learn about the convex hull.

What is a convex bag? To put it simply, for a plane point set S, we call the smallest Convex Polygon that completely contains the point set the convex packet H of the Point Set S.

A very important property of a convex bag is its convex nature. This feature is of great help to our understanding and computation of convex packets.

I) for any point A in Point Set S, if and only if there is a straight line P over a and all points except a in S are on the side of P, A is a vertex on the convex hull.

Ii) any two points A and B in Point Set S. if and only when all points except A and B in S are on the side of the straight line P of A and B, a line segment AB is an edge on the convex hull.

Iii) Any four points A, B, C, D in the vertex set S. When D is in the Triangle ABC (including edges), D is not the point on the convex hull.

The above mentioned convex hull "convex" provides a foundation for us to calculate the convex hull. Here we will introduce two simple and widely used algorithms-gift-wrapping and Graham-scan.

 

Gift-wrapping algorithm:

Through properties (I), we can find a special point, such as a point with the smallest y coordinate and the smallest X coordinate. Use it as the first vertex of the convex hull calculation. After determining the starting point, we can use the gift-wrapping algorithm to calculate the convex hull of a point set. The following steps intuitively describe the algorithm:

1) Think of all vertices as columns fixed on the plane. Imagine that we are tied to a pillar at the starting point.

2) Pull the rope straight to the right along the horizontal direction and rotate it counter-clockwise. When the rope hits a pillar, it corresponds to a point on the convex bag.

3) continue to rotate the rope and determine the vertex on a convex hull each time until the rope returns to the starting point.

Figure 1: gift-wrapping algorithm for convex hull Calculation

Each time you find the next convex hull vertex by rotating the rope, you need to compare all the remaining vertices in the vertex set.

The time complexity of one step is O (n ). The number of vertices on each convex packet must be rotated once. In the worst case, the number of vertices in the convex packet can be equal to the number of point sets, therefore, the time complexity of the entire gift-wrapping algorithm is O (n2.

 

Graham-scan algorithm:

Gift-wrapping algorithms are simple in terms of understanding and implementation. However, because of its complexity, we cannot use it to solve large-scale convex hull problems. Therefore, we will introduce Graham-scan, an efficient algorithm for convex hull calculation.

The Graham-scan algorithm can be divided into two parts:

1) like gift-wrapping, you must first find a starting point. Use this vertex as the origin to sort the angle.

2) First press the starting point into Stack h, then scan each vertex in the sorted order, and maintain stack H. This stack indicates the convex hull of all vertices that have been scanned so far. When scanning a vertex P:

A) if there are two fewer elements in the stack or the two points at the top of the stack have a left turn relationship with P, P is pushed into the stack.

B) Otherwise, the top element of the stack goes out of the stack and continues to Judge.

After all the points are scanned, stack H is the convex packet we require.

Figure 2: scanning process of the Graham-scan algorithm (the point of the stack H storage is the point of the solid line connection)

Analyze the complexity of Graham-Scan: the complexity of finding the starting point and sorting the Polar Angle in step 1 is O (n log n ). In step 2, each vertex is scanned only once and the stack H is maintained accordingly. During the stack maintenance process, each time the vertex in stack H is accessed, either this vertex is deleted or the stack maintenance is stopped. Therefore, all stack maintenance tasks can only be accessed for a maximum of 2n times. Therefore, the complexity of this part is O (n ). In combination, the time complexity of the Graham-scan algorithm is O (n log n.

Algorithm analysis:

Considering this question, we need to find a rectangle with the smallest area to cover all the given points. The yi zhi rectangle overwrites all vertices only when it overwrites the convex packets of These vertices. Therefore, the problem can be converted to a convex hull, and a rectangle with the smallest area is obtained to cover it.

So what are the properties of the smallest rectangle covering the Convex Hull?

First, the four sides of the rectangle must have the vertex of the convex hull. It is easy to figure out that if an edge of a rectangle does not touch the vertex of a convex hull, we will be able to compress this edge to a smaller rectangle that meets the conditions.

Secondly, this rectangle must have at least one side that overlaps with the side on the convex hull. This nature is not easy to intuitively think about, and you need to prove it in writing. Since the complete proof needs to be discussed in many cases, it is complicated, so here we only select one of the cases to prove it. Other cases can be proved similarly.

Using the inverse proof method, we assume that all the edges of the smallest rectangle that covers the convex hull are not overlapped with the edges of the convex hull. That is to say, each edge of the smallest rectangle has only one vertex of the convex hull. As shown in 3, the rectangle ABCD is the smallest rectangle covering the convex hull. M, N, P, and q are the vertices of the convex hull on the four sides of the rectangle. Let's create mm 'CD, NN 'ad respectively. The area S of the rectangle ABCD is mp x cos (∠ pmm') x nq x cos (∠ qnn '). We rotate the rectangle X degrees (Clockwise is positive, clockwise is negative), and still make the rectangle overwrite the convex hull and M, N, P, Q are on its four sides. Then, the area S of the new rectangle is MP × cos (∠ pmm' + x) × NQ × cos (∠ qnn '-x ). We only need to consider the monotonicity of COS (∠ pmm' + x) × cos (∠ qnn '-x.

Cos (∠ pmm' + x) × cos (∠ qnn '-x)

= 1/2 [cos (∠ pmm' + x + ∠ qnn '-x) + cos (∠ pmm' + X-∠ qnn' + x)]

= 1/2 [cos (∠ pmm' + ∠ qnn ') + cos (∠ pmm'-∠ qnn' + 2x)]

Limit 0 ≤ limit pmm' <π/2, 0 ≤ limit qnn '<π/2

Starting-π/2 <using pmm'-using qnn '<π/2

When cos (then pmm'-then qnn ') cannot obtain the minimum value

F (x) = cos (∠ pmm'-∠ qnn '+ 2x) in the left interval of 0, or F (x) In the right interval of 0) = cos (∠ pmm'-∠ qnn '+ 2x) decrease.

Therefore, for such a rectangle, we can always rotate a small angle clockwise or counterclockwise to obtain a smaller rectangle, which is in conflict with assumptions. Therefore, the smallest rectangle must have at least one side that overlaps with the convex hull.

After learning about the two properties of the smallest rectangle, we can easily think of an algorithm to enumerate which side on the convex hull overlaps with the side of the rectangle, find the farthest point in the positive and negative directions of the linear projection and the three points in the farthest distance to the straight line, so as to determine and calculate the area of the rectangle, and finally select the minimum value, that is, the area of the smallest rectangle that covers the convex hull.

We use the simplest method to implement it. After each edge is enumerated, the remaining points are scanned to find the other three points and calculate the area of the rectangle. The time complexity is calculated by O (n2. This question is acceptable. But what if the scale is bigger? Can we do better?

The answer is yes! We still have a very important piece of information that is not used. We calculate the distance or projection distance from the convex hull vertex to any side of the convex hull, the sequence is always one that increases first and then drops. At the same time, it is noted that if we enumerate overlapping edges in a counter-clockwise order, the other three points are always moving in the counter-clockwise direction.

As a result, we have obtained a more efficient algorithm. During the enumeration process, you do not need to scan all vertices after rotating to the next edge counterclockwise. You only need to compare them from the three points determined by the last edge and find the maximum value, to update these three points.

During the enumeration process, the pointers of the three vertices only access each vertex once, so the complexity of this process is O (n. In combination with the previous convex packet calculation process, we can solve this problem successfully in O (n log n) time.

After learning about the two properties of the smallest rectangle, we can easily think of an algorithm to enumerate which side on the convex hull overlaps with the side of the rectangle, find the farthest point in the positive and negative directions of the linear projection and the three points in the farthest distance to the straight line, so as to determine and calculate the area of the rectangle, and finally select the minimum value, that is, the area of the smallest rectangle that covers the convex hull.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.