Question:
Fuzzy sorting of 7-6 pairs of intervals
Consider this sort problem, that is, you cannot accurately know the number of each number to be sorted. For each number, we only know that it falls into a certain range on the real axis. That is, given is a closed interval of N shapes such as [ai, BI], where Ai is less than or equal to bi. The goal of the algorithm is to perform fuzzy-sort for these intervals, that is, to sort the intervals generated <I1, I2 ,..., in>, so that there is a CJ in [ai, BI] That satisfies C1 ≤ C2 ≤... ≤ CN.
A) design an algorithm for Fuzzy sorting of N intervals. Your algorithm should have a general structure of the algorithm, which can quickly sort the left endpoint (that is, each AI), and make full use of overlapping intervals to improve the running time. (As the intervals overlap more and more, the problem of fuzzy sorting of each interval will become more and more easy. Your algorithm should be able to make full use of this overlap .)
B) Proof: In general, the expected running time of your algorithm is lift (nlgn), but when all the intervals overlap, the expected running time is lift (n) (that is, when there is a value of X, so that all I have X in [ai, BI]). Your algorithms should not check this situation explicitly, but should naturally improve performance as the number of overlaps increases.
Thought: At first I saw this question, but I didn't understand what it was for a long time. Later I finally thought it was a problem through examples. We need to understand the Interval Fuzzy sorting mentioned above. The first point is that this is the interval sorting, which is different from the quick sorting. The quick sorting means that every element is determined, and each element is a certain interval, and the sorting result is not necessarily correct, fuzzy. First, we need to understand the following partial order relationship. First, let's take a look at the question: why does interval overlap improve the expected running time of the sorting algorithm? It is actually very easy to understand. This is equivalent to the fact that when we sort an array quickly, there are many repeated elements in the array. If we make an improvement, we will change the Division from 2 to 3, the front side is smaller than, the middle side is equal to, and the back side is greater. In the future, the intervals to be divided (only smaller than or greater than each other) may be greatly reduced, so that the partition may be very efficient.
A)First, define the partial order relationship (not unique) between two line segments, as shown in
The AI on the left endpoint of line segment I is used as a reference in three cases:
1) Line Segment j <line segment I, which meets B. End <A. Begin
2) line J = line I. At this time, B. Begin ≤ A. Begin ≤ B. End. The reason for this definition is that, in the end, we can obtain a. Begin (overlapping part) for the Ci values of line J and line I)
3) Line Segment j> line segment I. B. Begin> A. Begin
Through this, we can clearly perceive the situations in which the partial order is smaller than, the partial order is greater than, and the partial order is equal to each other. The figure is really a good thing.
After thinking for a long time, I finally found that Interval Fuzzy sorting is essentially a variant of fast sorting. In fact, interval sorting is performed in units. Therefore, you need to define a struct to store the first and last elements of the interval. Fast sorting is based on elements. By defining the relationship between a partial order greater than, less than or equal to, and then improving the partition of the fast rank, it can return a range struct every time, so that the value on the left is less than, and the value on the right is greater, in the middle. Code:
# Include <iostream> # include <ctime> using namespace STD; // the input of fuzzy sorting is an interval value pair, so it defines a struct node {int; int B ;}; // exchange two struct void swap (node & A, node & B) {node temp = A; A = B; B = temp ;} // The partition function is divided into three sections. The leftmost part is smaller than the principal component, the rightmost part is greater than the principal component, and the center part is equal to the principal component. // set the interval to [L, r] based on a certain range, divided into three intervals: [L, I] <[I + 1, J-1] <[J, R] node partition (node A [], int L, int R) {// select the last element as the primary element. RET is the structure for saving the vertex division node x = A [R], RET; int I = L-1; // trace the inter-cell Int J = R + 1; // trace the large interval int K = L; // traverse the entire array while (k <J & K <= R) {if (a [K]. B <X. a) // if it is less than the principal component, switch to the front {++ I; swap (A [I], a [k]); ++ K ;} else if (a [K]. a> X. a) /// if the value is greater than {J --; swap (A [J], a [k]); // The value cannot be K ++, because the exchanged elements may also be greater than the principal element} else {// The two intervals overlap and are considered equal. Take the overlapping parts as the reference intervals for further division. The reference intervals are relatively large at the beginning, after division, the reference interval gradually decreases. // The final result of division is [I, j]. Then, the left side of I is the inter-zone, and the back of J is the large interval, [I, j] is the overlapping part of the remaining interval, that is, the equal part // essentially a variant of the fast sorting. The fast sorting is the result of every call to partition. You can determine a pivot point, so that the number on the left is smaller than the pivot point, and the number on the right is greater than the pivot point // each time a partition is called, a "equal" interval can be determined, make all the left of the node smaller than (in a partial order) the pivot interval, and the right side greater than the pivot interval // a quasi can be determined While fuzzy sorting can determine a possible range sorting X. A = max (A [K]. a, X. a); X. B = min (A [K]. b, X. b); // if they are equal, they are not exchanged, but the public factor ++ K ;}} RET must be extracted. A = I; ret. B = J; return ret; // RET temporary storage} void fuzzy_sort (node A [], int L, int R) {If (L <R) {node mid = partition (A, L, R); // The mid stores the split point [I, j] // The middle overlapping part is not processed. Only the two parts fuzzy_sort (, l, mid. a); fuzzy_sort (A, mid. b, R) ;}} bool print_possible_sort (node A [], int N) {int * c = new int [N]; C [0] = A [0]. a; cout <C [0] <''; For (INT I = 1; I <n; I ++) {c [I] = max (A [I]. A, C [I-1]); If (C [I]> A [I]. b) // when an error occurs, the I-th interval is greater than the I + 1 interval, that is, I. a> (I + 1 ). B {cout <"error! "<Endl; return false;} cout <C [I] <'';} Delete [] C; return true;} int main () {srand (unsigned) time (0); node A [10000]; int n = 0; cout <"Number of input intervals N:"; CIN> N; cout <"random input below" <n <"start and end point A, B:" <Endl; For (INT I = 0; I <N; I ++) // generate a valid range pair {A [I]. A = rand () % 1000; A [I]. B = rand () % 1000; if (a [I]. a> A [I]. b) {swap (A [I]. b, A [I]. a);} cout <A [I]. A <"" <A [I]. B <Endl ;}fuzzy_sort (A, 0, n-1); cout <"the result of fuzzy sorting is as follows:" <Endl; For (INT I = 0; I <n; I ++) cout <A [I]. A <"" <A [I]. B <Endl; cout <"may be arranged as follows:" <Endl; If (print_possible_sort (A, n )) {cout <Endl <"successfully finds an order that meets the conditions. "<Endl;} else {cout <" algorithm failed! "<Endl;} return 0 ;}