Minimum K count

Source: Internet
Author: User

Today, csdn accidentally saw July's blog titled "the most classic top 10 algorithms in the world today". I felt that the name of this article was quite domineering, so I took a look at it. One of them is calledBfprtIt is said that the k-th element in the array can be found with O (n) complexity in the worst case. The blog post links to another blog that explains this algorithm in detail, so I clicked in again to prepare to see how amazing this algorithm is, so it can be so efficient!

This article begins with a question: how to find the minimum K number in a pile of data.

I thought about the following methods:

1. Sort the data first, and then retrieve the first K number. There are many sorting algorithms, such as insert sorting and fast sorting, but it is impossible to reach O (n) in the worst case );

2. open an array of | K | sizes. First, load the first K number from the data to find the maximum number of K numbers max (K ), then, start from the number k + 1 and look backward. If there is a number smaller than this Max (K), replace this number, then find the maximum max (k) from the k Number ). In this way, scan backward to obtain the result. The worst complexity of this algorithm is O (kN;

3. heap, such an idea flashed in his mind, can only be regarded as inspiration and other things, but did not think about it.

I was eager to see what happened to this so-called bfprt algorithm, so I just thought a little bit and didn't think about it.

The blog also lists several possible solutions to this problem:

1. Like what I think, sorting, number taking, and the most stupid method are also the easiest to think;

2. The same as I thought, above 2 (it seems that I am not the most stupid );

3. Stupid method: scan the data heap K times and find the smallest number each time. The complexity is O (kN );

4. I used the heap (unfortunately I did not think about it )! The idea is similar to 2. First create one with the first K number in the dataMax heapThe heap building complexity is O (K), and then scanning backward from k + 1. If the heap top element is smaller than the heap top element, replace the heap top element and update the heap, the complexity of this operation is O (logk ). Therefore, the total time is O (K + (n-k) * logk) = O (N * logk), which is slightly better than the O (NK) of method 2.

The advantage of this method is that when the data size is large, if the memory cannot store all the data, this method can solve this problem. Read a part of the data, create a heap, process the data, and then read a part of the data. The loop goes on until the data processing is complete.

5. A heap is also used, but a heap is created for the entire data.Minimum heap(O (n), and then extract the top elements of the heap. Each time the heap is updated, the heap (O (logn) is retrieved. K times, so the total complexity is O (n + K * logn );

It can be proved that O (N + K * logn) <O (N * logk ), that is, the method of creating n elements is the minimum heap, and then taking the first K heap top elements is more time-complex than creating the maximum heap of k elements and then comparing all data to get the minimum K number. A little better, but the two are actually an order of magnitude. In that blog post, the author specifically wrote two methods to process a group of big data, the result shows that the time of the two methods is almost the same.

However, in space, the maximum heap only requires the space complexity of O (K), while the minimum heap requires O (N). Therefore, in general, the max heap solution has an advantage over the min heap.

Algorithm Improvement: Each time the top element of the heap is taken away to update the heap, the last element in the heap is normally put on the top of the heap! Top), and then adjust the heap! Top down to where he should be. After improvement ,! Top does not need to be lowered to the position where he was supposed to be, but can be lowered at most K times. The details are as follows:

After creating the minimum heap of N, remove the top element of the heap (the first number), and then set the last number! Top to heap top! Top down at most the K-1 layer to form a new heap; then take the heap top elements (the second number), the same, when updating the heap! Top down at most K-2 layer... the heap is no longer updated until the k Number is removed (the heap is no longer the minimum heap). The algorithm ends and the minimum K number is obtained, it doesn't matter if the final "heap" is a heap.

Complexity after improvement: Build the heap O (N), update the heap O (K), and update it K to O (K * K) = O (K ^ 2 ), therefore, the total complexity is O (n + k ^ 2), which is better than O (N + K * logn) before the improvement.

6. With the idea of fast sorting, we first select a number as the benchmark comparison (The author is called the "pivot element", that is, the benchmark) and divide the data into two parts: SA and Sb.

If K <| sa | (| sa | indicates the SA size), perform the operation on the SA part in the same way;

If K = | sa |, SA is the desired number;

If K = | sa | + 1, SA and this operator constitute the solution;

If K> | sa | + 1, use the same method to find the smallest part of Sb (k-| sa |-1) number (SA and region are already part of the solution ).

Different from the Quick Sort, each time the Quick Sort operation is performed on the two parts of the data after the division, the quick selection (this algorithm is called for the time being) is different, perform operations on only some of them.

BfprtThe algorithm is improved based on this method. The main improvement of the bfprt algorithm is to select the greatest number. Generally, the fastest sorting is to obtain the first or last number in the Data heap, the bfprt algorithm uses the "median of the Five-differentiation median" method to obtain the Gini coefficient, reducing the complexity of the algorithm to O (n). The specific method is as follows:

Divide the data into five groups. The number of the last group of data is n % 5. Then, the median is selected using the insert sorting method for each group of data, continue the selection of the selected medians in the same way, and finally select the medians of these numbers as values to achieve O (n) efficiency.

I did not take a closer look at the specific proof of the algorithm. That blog post is too long. It is estimated that it has been continued for many times. After modifying it n times, it seems that the organization is a bit messy. When I see my head dizzy, I will look at the proof of this algorithm again. For details, refer:

Mark Allen Weiss's data structure and algorithm analysis-C language description, Chapter 10th, section 10.2.3

Introduction to algorithms, sections 9.2 and 9.3

The first version of the beauty of programming, 141st pages, section 2.5 looking for the maximum K count

M. Blum, R. W. Floyd, V. Pratt, R. Rivest and R. Tarjan, "Time bounds for selection"

Programming Pearl II Chapter 15th procedures


Summary: if you think about it at the time, you may think about several methods. After you have an understanding of the method, think about whether the algorithm is optimal and whether it can be optimized. For example, sorting at the beginning may take a long time, is it possible to think of a heap without the sort method or the heap must be adjusted to the "correct Heap" every time, or you may use the fast sort method, fast sorting is not the data divided by two parts each time, and so on; think more about the flexible application of the learned data structure, such as the heap and fast sorting used in the past for data sorting, for data selection now, blahblah... the summary is complete.


This article reference: "Programmer programming Art: Chapter 3, find the minimum k Number" http://blog.csdn.net/v_JULY_v/article/details/6370650

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.