We have discussed one loss minimization strategy before: Job Sequencing Problem – Loss Minimization. In this article, we will look at another strategy that applies to a slightly different problem.

We are given a sequence of N goods of production numbered from 1 to N. Each good has a volume denoted by (Vi). The constraint is that once a good has been completed its volume starts decaying at a fixed percentage (P) per day. All goods decay at the same rate and further each good take one day to complete.

We are required to find the order in which the goods should be produced so that overall volume of goods is maximized.

**Example-1:**

Input: 4, 2, 151, 15, 1, 52, 12 and P = 10% Output: 222.503

Solution: In the optimum sequence of jobs, the total volume of goods left at the end of all jobs is 222.503

**Example-2:**

Input: 3, 1, 41, 52, 15, 4, 1, 63, 12 and P = 20% Output: 145.742

Solution: In the optimum sequence of jobs the total volume of goods left at the end of all jobs is 145.72

** Explanation – **

Since this is an optimization problem, we can try to solve this problem by using a greedy algorithm. On each day we make a selection from among the goods that are yet to be produced. Thus all we need is a local selection criteria or heuristic, which when applied to select the jobs will give us the optimum result.

Instead of trying to maximize the volume, we can also try to minimize the losses. Since the total volume that can be obtained from all goods is also constant, if we minimize the losses we are guaranteed to get the optimum answer.

Now consider any good having volume V

Loss after Day 1: PV

Loss after Day 2: PV + P(1-P)V or V(2P-P^2)

Loss after Day 3: V(2P-P^2) + P(1 – 2P + P^2)V or V(3P – 3P^2 + P^3)

As the day increases the losses too increase. So the trick would be to ensure that the goods are not kept idle after production. Further, since we are required to produce at least one job per day, we should perform low volume jobs, and then perform the high volume jobs. This strategy works due to two factors.

- High Volume goods are not kept idle after production.
- As the volume decreases the loss per day too decreases, so for low volume goods the losses become negligible after a few days.

So in order to obtain the optimum solution we produce the larger volume goods later on. For the first day select the good with least volume and produce it. Remove the produced good from the list of goods. For the next day repeat the same. Keep repeating while there are goods left to be produced.

When calculating the total volume at the end of production, keep in mind the the good produced on day i, will have times its volume left. Evidently, the good produced on day N (last day) will have its volume intact since .

**Algorithm –**

Step 1: Add all the goods to a min-heap Step 2: Repeat following steps while Queue is not empty Extract the good at the head of the heap Print the good Remove the good from the heap [END OF LOOP] Step 4: End

**Complexity –**

We perform exactly N push() and pop() operations each of which takes log (N) time. Hence time complexity is O( N * log(N) ).

Below is the Cpp implementation of the solution.

`#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `void` `optimum_sequence_jobs(vector<` `int` `>& V, ` `double` `P) ` `{ ` ` ` `int` `j = 1, N = V.size() - 1; ` ` ` `double` `result = 0; ` ` ` ` ` `// Create a min-heap (priority queue) ` ` ` `priority_queue<` `int` `, vector<` `int` `>, greater<` `int` `> > Queue; ` ` ` ` ` `// Add all goods to the the Queue ` ` ` `for` `(` `int` `i = 1; i <= N; i++) ` ` ` `Queue.push(V[i]); ` ` ` ` ` `// Pop Goods from Queue as long as it is not empty ` ` ` `while` `(!Queue.empty()) { ` ` ` ` ` `// Print the good ` ` ` `cout << Queue.top() << ` `" "` `; ` ` ` ` ` `// Add the Queue to the vector ` ` ` `// so that total voulme can be calculated ` ` ` `V[j++] = Queue.top(); ` ` ` `Queue.pop(); ` ` ` `} ` ` ` ` ` `// Calclulating volume of goods left when all ` ` ` `// are produced. Move from right to left of ` ` ` `// sequence multiplying each volume by ` ` ` `// increasing powers of 1 - P starting from 0 ` ` ` `for` `(` `int` `i = N; i >= 1; i--) ` ` ` `result += ` `pow` `((1 - P), N - i) * V[i]; ` ` ` ` ` `// Print result ` ` ` `cout << endl << result << endl; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// For implementation simplicity days are numbered ` ` ` `// from 1 to N. Hence 1 based indexing is used ` ` ` `vector<` `int` `> V{ -1, 3, 5, 4, 1, 2, 7, 6, 8, 9, 10 }; ` ` ` ` ` `// 10% loss per day ` ` ` `double` `P = 0.10; ` ` ` ` ` `optimum_sequence_jobs(V, P); ` ` ` ` ` `return` `0; ` `} ` |

**Output –**

1 2 3 4 5 6 7 8 9 10 41.3811

## Recommended Posts:

- Minimum product subset of an array
- Water Connection Problem
- Smallest subset with sum greater than all other elements
- Number of ways to form a heap with n distinct integers
- Job Sequencing Problem - Loss Minimization
- Policemen catch thieves
- Job Sequencing Problem | Set 2 (Using Disjoint Set)
- Greedy Algorithm to find Minimum number of Coins
- Greedy Algorithm for Egyptian Fraction
- Job Sequencing Problem | Set 1 (Greedy Algorithm)
- Binary Heap
- Efficient Huffman Coding for Sorted Input | Greedy Algo-4
- Huffman Coding | Greedy Algo-3
- Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
- Activity Selection Problem | Greedy Algo-1

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.