Knowledge in quick sorting: Let's look at the question of ball.

Source: Internet
Author: User
One of the 12 small balls is a bad ball. There is a balance. You need to use the least number of calls to determine which ball is bad and whether it is light or heavy. This is a long-standing intellectual question. There are also a lot of explanations on the network, and there is a strict proof of generalization to N balls. There are also some sporadic points that refer to the optimal solution method from the perspective of information theory. I have always thought that this question has no good ideas except trial and error. I can only try it one by one.

I used to guess the digital game to illustrate the idea of binary. here I will look at a common thinking question:

One of the 12 small balls is a bad ball. There is a balance. You need to use the least number of calls to determine which ball is bad and whether it is light or heavy.

This is a long-standing intellectual question. There are also a lot of explanations on the network, and there is a strict proof of generalization to N balls. There are also some sporadic points that refer to the optimal solution method from the perspective of information theory. Originally, I always thought that this question had no good ideas except trial and error. I had to try it one by one and try to find information from the results to see which solution was the least.

However, in fact, it does have other ideas, a more essential idea, and it does not need the knowledge of information theory.

Let's take a look at digital games. To make sure that we guess at least a number of times in any situation, our strategy is to eliminate exactly half of the possibility every time. Similar to the ball problem:

  • A bad ball may be any of the 12 balls, which is 12 possibilities.
  • In each of these possibilities, the ball may be light or heavy. The answer to the question "which ball is a bad ball, light or heavy" is 12 × 2 = 24.

Now we use the balance to weigh the ball, which is equivalent to asking questions about the 24 possibilities. the balance has three output results: Balance, left tilt, and right tilt ", this is equivalent to three answers to our question: All possibilities can be divided into three parts. based on the imagination of the digital game, we should try to make the probability of the three branches equal, that is to say, the probability of splitting all data is triplicate. In this way, one weighing can reduce the possibility of the answer to 1/3, and three times can be reduced to 1/27. There are only 24 possibilities in total, so theoretically it can be called three times.

With the guiding principles of how to name it, it is not too difficult to construct a so-called strategy.

First, explain why the most intuitive naming method is not optimal-6 and 6: When 6 and 6 are called, the possibility of balance is 0. As I said just now, the optimal strategy should make the probability of the three states of the balance equal so that all the possibilities of the three states can be answered.

To get a clearer picture of this problem, let's assume there are six balls to consider the differences between 3, 3 and 2:

Before being called, there are 12 possibilities: 1 light, 1 heavy, 2 light, 2 heavy ,... , 6 light, 6 heavy.

3, 3 said: Now put 1, 2, 3 on the left, 4, 5, 6 on the right after 3, 3 said, without losing the general assumption that the balance left, then the possibility of a ball becomes half of the original (6 types): 1, 2, 3, 4, 5, 6. That is to say, this method can eliminate half the possibility.

2, 2 said: Now let's look at the 2, 2, that is, 1, 2 put on the left, 3, 4 put on the right, the remaining 5, 6 not called, put one side. If the result is a balance, there are four possibilities left: 5, 5, 6, and 6. If the balance is left tilted, there are four possibilities left: 1, 2, 3, and 4. The right and left are similar. In short, no matter what the balance results, the situation is reduced to 1/3! We make full use of the condition that "three possible results of the balance exist" to trigger all possibilities, rather than second-class scores.

Speaking of this, the rest is really simple: Step 2, just remember this guiding ideology-the name you choose must make the remaining answer possibilities when the balance is balanced, as much as the remaining answer possibilities when the balance is left (right. In fact, this is equivalent to choosing a naming method, so that the probability of the three results output by the balance is equal, because the probability that the balance outputs a result is equivalent to the sum of all answers that support this result (left-leaning, right-leaning, and balanced), and each possibility of the answer is equal probability.

In his book "Information Theory: Inference and Learning Algorithms" (The author opens a free e-book) Section 4.1 specifically addresses the ball problem and draws a good picture, I copied it:

In the figure, "1 +" indicates the possibility that "1 ball is the most important. There are 24 possibilities at the beginning.

4 and 4. no matter what the situation (branch) is, there are always four possibilities. This is a perfect 3-point.

Then construct the second name for each branch. here you only need to perform a slight calculation to find the second name on branch 1, in this way, the three results output by the balance are equal (strictly speaking, they are almost equal ). This is the reason why this naming method can do the best in the worst case. no branch is its weakness, and it will inevitably reduce the situation to 1/3.

Additional reading

The topic list of this article is as follows:

  1. Knowledge in quick sorting: start with guesses
  2. Knowledge in quick sorting: Let's look at the question of ball.
  3. Knowledge in quick sorting: Information entropy
  4. Knowledge in quick sorting: the process of quick sorting
  5. Knowledge in quick sorting: Hall and quick sorting
  6. Knowledge in quick sorting: Implementation of Hall's fast sorting
  7. Knowledge in quick sorting: Key element selection and algorithm efficiency
  8. Knowledge in quick sorting: randomization fast sorting

This article is available at http://www.nowamagic.net/librarys/veda/detail/2388.

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.