[Algorithm analysis] the algorithm that selects "Best Girl" as the maximum probability

Source: Internet
Author: User

 

 

Problem:

 

Suppose you are a boy, and God has set up 20 suitable girls between the ages of 20 and 30. These girls are willing to be your partners, but you can only choose one of them. The selection conditions are as follows:

  1. For you, the 20 girls can be sorted, that is to say, you can rank their quality after the event. The top girl is the best for you, the 20th ranking is the worst for you.
  2. These 20 girls don't appear in your life at the same time, but in chronological order. Every time they appear, you have to decide whether to stay or refuse. If you leave her behind, she will become your lifelong companion and you will not have the right to select the girl next to her. If you refuse, you can also choose the girl next to her, however, for girls who have already refused, there is no chance to start all over again.

Assume that the appearance of a girl appears at random in various time periods, that is, the appearance time has nothing to do with the quality of the girl. So when should you decide to accept a girl and make the accepted girl the most likely to be the best girl?

 

Algorithm analysis:

An interesting question: First of all, I subconsciously think that I should first take a look at the comparison, but I don't want the best. Later, I saw on the Internet that the probability of such a strategy is quite high, first, review the complete online answer:

Policy 1: Draw lots in advance and select the number one. For example, if the number of girls reaches 10th, the 10th girls who appear in your life will be identified as your partner beforehand. What is the probability that she is the best girl? The answer is 1/20 = 0.05. This strategy gives you 5% of the chance to get the best girl. This probability is obviously too small to happen.

Policy 2: divide all the girls into the first and second paragraphs. The first 10 girls are not accepted, but they know the quality of these 10 girls. Then, among the 10 girls that appeared later, the first time I met a cute girl, I immediately accepted it. This is a method of waiting and taking a look. In this strategy, the probability of getting the best girl is (10/20) * (10/19) = 0.263. This probability is not too small.

The probability algorithm in policy 2 is described as follows, make sure that the best girl is required to appear among the last 10 girls. Otherwise, you cannot get the best girl. The probability is (10/20). At the same time, I also asked the second-best girl to appear in the top 10. The probability is (10/19). Why is it (10/19 )? In addition to the best, there are 19 remaining girls, and the probability that the second best girl appears in the top 10 is (10/19)-This ensures that you will get the best girl.

However, is the probability of getting the best girl in strategy 2 0.263? Maybe not, because this is just the first 10 of the second-year-old girls. In fact, even if the second-year-old girl did not appear in the first 10, however, as long as the top 10 most quality girls appear before the best girls appear, Strategy 2 can also ensure the best girls (this should be figured out, otherwise, it is hard to understand the following content ). That is to say, the probability that policy 2 gets the best girl is actually more than 0.263 (in fact, we will find that this probability should be 0.3594 later. Wow! This is indeed a small probability ).

But is there a better way? Or we can ask, is it the best to give up the first 10 girls? If not, how many girls should they give up first?

In fact, we do have a better strategy (you should first understand the previous content. If you do not understand the previous content, you may not understand the content below ). Since the quality of 20 girls is random in your life and there are no rules, the probability of K girls being the best girl is 1/20, what is the probability of choosing the best girl? This should be taken into consideration: given that the K Girls are of the best quality, we decided to give n-1 girls and execute rule 2 from the N girls, so it is required that the highest quality among girls before K must appear in the first n-1 girls, in order to ensure that K is selected, the probability is n-1)/(k-1 ). So the K girl is just the best girl and the probability of being selected is (1/20) [(n-1)/(k-1)]. Here, the value range of K is obviously an integer in [N, 20. So the probability of giving up n-1 girls will surely get the most lovely girl is actually (1/20) [(n-1)/(n-1)] + (1/20) [(n-1) /(n)] + (1/20) [(n-1)/(n + 1)] +... + (1/20) [(n-1)/(20-1)]. This probability can be calculated using Mathematica or Excel. You will find that when n = 8, this probability has a maximum value of 0.3842. That is to say, if we give up the first seven girls, first take a look and have a picture in our hearts, and then as long as we see the girls better than the first seven girls, then we will accept them immediately. The probability that the accepted girl belongs to the best girl is 0.3842. This is better than policy 2, which gives up 10 girls (N * = 11). Based on the formula above, the probability of getting the best girl is 0.3594.

We use Mathematica to plot the probability graph for the best girl (the vertical axis is the probability, and the horizontal axis indicates that the probability is carefully considered from the very beginning. The maximum probability is N * = 8, that is, the first seven are abandoned, and the acceptance is carefully considered from the first 8th ).

 

Based on the results, we can conclude that if a person is determined to marry between the ages of 20 and 30, and the 20 girls appear on average every year, so you should begin to consider things for life at the age of 24.

In this example, you can modify the data and use the same method to solve the problem. For example, if you are 30 girls, you should start from 11th girls and seriously consider lifelong things.

This example can also be changed to another version. For example, a gem is placed on each floor on the 20 th floor, and each gem has different sizes. Now you are going upstairs from the first floor, and you can decide whether or not to use the gems on the first floor. If not, you cannot go back. If yes, it cannot be obtained later. Q: How can you obtain the biggest gem with the greatest chance? This question is said to be an interview question from Microsoft. However, the principle is the same as that for girls.

Proof Method from shell net: http://www.guokr.com/article/6768/

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.