Section 1.6: Beverage supply

Source: Internet
Author: User
Tags ticket

Author: Shide

It has been more than half a month since the day I bought the book. This period of time said short, if it is a book of more than 300 pages, I will be able to handle a day (my record is more than 1000 pages a day "Datang Shuanglong biography"), but to the present "beauty of programming" I only read less than 50 pages. Although I do not see every day, but once I look at a problem, I would like to be able to put this problem in the algorithmic level of analysis, this focus is what I used to read the "introduction of Algorithms" or the algorithm class did not realize when. The reason, the main or pure theory flow in the boring, pure application can not help but superficial, and this book is just good positioning, not only to use the driving algorithm to learn, but also to avoid too didactic style.

Although the original intention is good, but I still feel that the scale is open to discussion. Because programming should be both rigorous and flexible, rigorous thinking to ensure the stability of the operation of the program, and flexible thinking for innovation provided a condition. And "The beauty of programming" gives me the feeling is flexible more than rigorous. I think since it's a book about algorithms, then the proof on some key points is indispensable, for example, in the six questions I saw, there were two greedy algorithms that did not prove their greedy selectivity, or the lack of discussion, in which the greedy choice of "buy a book problem" is still problematic. Of course, we should not expect this book to be located in the "face test set" to write the "Introduction to the algorithm" as a strict proof. But at least we should give concrete ideas to prove that ideas are based rather than intuitive (because our intuitive feelings are often inaccurate, for example, my reading notes four kinds of analysis to buy books, if the discount of three books to 15%, then we may be the resulting discount form misled by the original greedy algorithm. , and most frightening of all, given the fact that it is very difficult to build a counter case, we may not be able to see the clue from the examples given. In addition, the number of errors encountered in the reading process is also repeatedly hit new highs. Heard that the second edition is about to come out, really for the content behind the worry, it is also the last page with a errata.

Although there are many problems, but because of afterward, we can still get enough of the wisdom of Microsoft from the book, even if it is prompted us to review the basis of the algorithm, hopefully, fourth edition (if any):-)     Can see a perfect "programming beauty". 1 Problem description and analysis

The problem is that Microsoft offers employees a variety of different beverages every day, such as cola, yogurt, soy milk, coffee, green tea ... (good pay, hehe), beverage I unit capacity of VI, of which each drink a single capacity is 2 of the power, the staff of the beverage I satisfaction is hi, The total capacity of the freezer is V (must be filled every day), the problem is how to combine the various existing beverages, so that total satisfaction is highest.

Or to say my first impression, obviously this is an optimization problem, but it is easy to think that this problem and linear programming description is very consistent, but because the solution of linear programming simple method is more complex, here is no longer discussed. Second, think back to the classic 0-1 knapsack problem, and the problem is somewhat similar, the difference is 0-1 knapsack problem, each item can only be taken once, and here the same drink can get more than a bottle; In addition, the total amount of supply per day in the original problem must be met (otherwise there will be complaints from employees). ), so it is not possible to have a back-packing situation like 0-1 knapsack problems. This condition actually changes our search strategy for the optimal solution, because the freezer with capacity V must be full every morning, so even if there is another combination that makes the highest degree of satisfaction but not the freezer, we can't choose.

In fact, we can slightly change the condition of the subject, ignoring the original problem of each drink single capacity is 2 of the Fang conditions, and allow the freezer dissatisfaction in the case of the maximum satisfaction of the combination of the hope that the original problem can be more general solution. 2 Algorithm Analysis 2.1 dynamic programming algorithm

No suspense, optimization problems with dynamic planning, greedy algorithm, branch clearance to take turns in the battle. Opt (v ', i) indicates the maximum number of CI-I drinks possible from I to n-1 beverage, and the sum of satisfaction in the scheme with a total of V '. Then the recursive type should be:

Opt (v ', i) =max{K * hi+opt (V '-vi * k,i+1)} (k=0,1,2...,ci,i=0,1,2...,n-1)

Here I think I need to show that the beverage combination can eventually be combined with V. 2.2 Greedy algorithm

The greedy solution of the book is familiar, and the information is sorted according to the volume of the beverage (where we have a beverage with a n0 capacity of 20):

Volume

TotalCount

Happiness

20

Tc0_0

H0_0

...

...

...

20

Tc0_1

H0_n0

...

...

...

21st

Tc1_0

H0_0

...

...

2M

Tcm_0

Hm_0

...

Then do the greedy choices in the following order:

(1) The total number of beverages is 1, and the maximum Happiness index is selected from beverages with a capacity of 20.

(2) The total beverage is 2, from the volume of beverages 21 to choose the most Happy index (set to H1), and the beverage volume 20 of the highest happiness index (set to H0), compare H1 and 2* H0, take out the biggest is the current best choice

(3) Continue until the OPT (v,0) is obtained

A cursory look, some déjà vu. We also faced the problem of buying books when we had to split the book, and then look up the table for greedy choice. However, every time you choose to buy a book of M book, and each choice only affect the next selection, so only need to 2M a limited split.

The subject is not the same, for a certain capacity V ' =11 as an example, there are two problems:

1. First, we need to look at all the possibilities of splitting, and find out the biggest of them as the result of this greedy choice. Among them, because "each drink's unit capacity is 2 of the power", so the split results are only considered with less than V ' 2 of the Fang Lai, that is (1) 11=8+2+1,4+4+2=1,4+2+2+2+1,....,1+1+...+1. As you can see, for V ' we can at least ' remove ' the V ' combination (perhaps more), even if we save the results of each calculation in a table, our lookup frequency is at least Ω (1+2+...+v ' =v ' 2), and the space complexity is high, and there is no "much simpler". Furthermore, if the limit of "power per unit capacity of each drink is 2" is removed, the result of the split will become (calculated 2) 11=10+1,9+2,...,5+5,5+5+1,5+4+2, ..., resulting in a further increase in the number of lookups.

2. Second, let's go back to the definition of greedy choice. Because of the greedy selectivity, the process of the greedy algorithm chooses the best results at the present time, so that when the final state is reached, the result is just the optimal solution.     And we look at the V ' =11 example, assuming the optimal solution is 4+4+2+1, then we have calculated 8 is not a part of the optimal solution, which is not the spirit of the greedy algorithm, so this method is actually dynamic planning. 3 Summary

The method of dynamic programming is very good and the greedy algorithm is open to discussion. In fact, this is normal, because the difficulty of using the greedy algorithm is to prove the existence of greedy selectivity, given the positioning of the book, we can not criticize more. But the problem and the greedy algorithm for the last question (Buy book discount) I think are too hasty.     If you want to make this book a classic, "Microsoft Interview" gimmick is far from enough, the attitude of excellence is critical, sometimes it is not necessary to set the deadline so urgent, the occasional jump to the quality of the ticket is also taken for granted, to see people Blizzard's "StarCraft 2" Jump ticket so many years to know ... 4 "The beauty of programming" this question Errata

[1] P41, seventh paragraph, "Opt (V ', i) expression from the first i,i+1,...,n-1,n drink ...", you should remove the N, because the value range of i is [0,n-1].

[2] P41, eighth paragraph, "opt (v,n)" should be replaced by opt (v,0).

[3] P41, derivation formula, should be changed to max{... +opt (V '-vi * k, i+1)} (..., i=n-2,n-1,......,0)

[4] P42, the Code listing 1-9,for (int i=0 i <= v i++) should be changed to for (int i=1; I <= v i++), because the first line of Opt[v][n is 0, so it's not calculated.

[5] P42, the code listing 1-9, "if (I<=k*v[j])" should be changed to if (I < k*v[j)), because obviously the equal sign situation should be retained

[6] P42, the last paragraph, which should be computed opt[i][j], requires only opt[k][j+1] (0<=k<=v), so you do not have to list the entire matrix, but only two columns.

[7] P44, the third row of three kinds of table, tc0_1, should be changed to Tc0_n0

(Original address: http://blog.csdn.net/kabini/archive/2008/04/21/2311946.aspx)

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.