Greedy algorithms: cost minimisation
I'm struggling with the following greedy algorithm I wrote; I know my algorithm is not complete, but I really don't know to improve it.:
Algorithme minCost()
while j<n (while all the licences have not been yet bought)
j=next licence bought
If the licence of the current month is available then
buy
EndIf
EndWhile
This the formulation of the problem: To market its various products, a company needs "n" licenses. Because of certain laws, it can not get more than one permit per month. In addition, the cost of permits increase each month. Indeed, although the cost of each permit is $100.00 currently, the cost of the permit j, (1 ≤ j ≤ n) increases by a factor rj> 1 each month (the rj are parameters). In other words, buy the license in the first four months costs 100r4 while its acquisition during the fith month, for example, would cost $ 100(r3)^5. Finally, we assume that ri is diff开发者_如何学JAVAerent of rj for i different of j. The question then is, for a given set of rj (1 ≤ j ≤ n), in what order to buy the "n" permits to minimize the total cost of ownership. 1. Develop a polynomial algorithm using greedy approach, for solving this problem. Analyze your algorithm in worst case. 2. Prove that your algorithm returns the optimal solution well. 3. Illustrate your algorithm on the following instance: n = 3, r1 = 3, r2 = 4, r3 = 2.
Thanks
Algorithme minCout(A[1, ..., n], B[])
//A[1, ..., n]: table storing the rj values of the licenses cost
//B[]: empty table to store selected licences cost for buy
QuickSort(A[1, ..., n])
//A[1, ..., n] is now sorted in decreasing order
while j < n (while all the licences have not been yet bought) do
for i ← 1 to n (Check all the available licences cost) do
if ri < ri+1 (choose the highest license cost) then
A[i ] = i + 1 (buy now the highest cost licence)
end
j = j + 1 (check the next licence to buy)
end
end
Return A[i]
Normally the number of licences must decrease as longer as I'm selecting the licences of the highest cost and store them into table B. In addition, as I'm reviewing the cost of the licences, I must not review again the full part of table A. Then, how can I write then a recursive version of this algorithm that can allow me to consider what I'm just mentionned? Thank you.
精彩评论