Select M items from N items such that completing these M item's tasks take the minimum time

I'm trying to solve the following problem: You are given N items. Each item contains three tasks A, B and C. The time required to complete task A is TA, task B is TB, task C is TC. Now, we must select M items such that completing these M item's tasks take the minimum time. And here are the rules:

  1. All selected M items are operated simultaneously, i.e. tasks of all the M items are operated at the same moment
  2. The task B of any selected items cannot be started unless task A of all M items is complete
  3. The task C of any selected items cannot be started unless task B of all M items is complete

For example:

if say N = 3 and M = 2 (it means we must select M items out of 3 items in total) Tasks: A B C item 1 : 1 2 2 item 2 : 3 4 1 item 3 : 3 1 2 

If we select item 1 and item 3, task A of both items gets completed after 3 units(item 1 waits for item 3 to finish), then task B of both the items gets completed after the next 2 units time. Similarly, task C gets completed after 2 units time. Hence the total time is 7(which is the minimum possible combination we can find)

I have tried thinking of a Dynammic programming solution to the problem. But am unable to get a concrete solution to the problem. Could anyone help me out by giving a valid solution to the problem.

PS: Please don't write the code. I am just looking for the logic here.

Thank you in advance.

3

2 Answers

Solution via Greedy Method (Weight Calculation + Deadline Sequencing)

Here is a greedy approach for the solution of this problem, I hope it helps. Good luck!

Since each task within an item takes time T to complete, we can think of these as "Deadlines" for these tasks (A, B and C) . And we can visualise these deadlines as if they were "slots" within an array/train of slots.

In order to visualize these deadlines consider these examples;

Task A of item 2;

0__A__1__A__2__A__3

Task C of item 1;

0__C__1__C__2

Let's consider this now; We have an K amount of "slots" within our hand 0__1__2__ ... __K and the problem asks us to spend minimum amount of slots as possible.

Another example from your explanation for better visualization of the problem, when you chose the item1 and item3 our slot took this form;

item1 + item3 "deadline slot occupation"

0_A_1_A_2_A_3_B_4_B_5_C_6_C_7

First three slots are occupied because item3's task A takes 3 units longer than item1. Task B can only start when this "longer" task A is done therefore starts from the slot number 3.

Therefore the problem becomes this; Fill our slot with the MINIMUM amount of slots spent. Therefore we will take a greedy approach into this problem.

  • Find individual "deadline slots" for M number of items we want to choose from N items

In the example you have provided;

For item1;

0_A_1_B_2_B_3_C_4_C_5

5 slots occupied

For item2; 8 slots occupied

For item3; 6 slots occupied

For itemX; P slots occupied and so on....

After knowing the number of slots each item demands on task times we will check M number of Subtractions as combinations of items within N number of item task times to get the SMALLEST number possible.

Example; For M items to choose when M=2;

Item1-Item2 = 5;

Item1-Item3 = 3;

Item2-Item3 = 4;

**edit; Item1 - Item2 corresponds to absolute value of subtractions within the combinations of chosen number of items; such as if M=2; |a1-a2| + |b1-b2| + |c1-c2| ...

Therefore for M=2 choices we take the minimum result of 3 which leads us to choosing Item1 and Item3 as solution.

This number will give us the minimum number of slots used. Hence, this leads us to the solution.

4

Solution using priority queue or sorting

let's say we choose some M elements and their maximum values of A, B, C are Amax, Bmax and Cmax respectively, then we are sure that there must be some element in array with A = Amax similarly for B and C, so we can say that there are at maximum N different values for A, B, C. Hence possible combination for (Amax, Bmax, Cmax) are at most N^3. Then we can check if there are at least M elements in the array satisfying this constraint for each combination and update the answer value using ans = max(ans, Amax + Bmax + Cmax). This approach takes O(N^4) time.

But we can reduce the time complexity to O(N^3logN) using sorting or priority queue for Cmax level. Basically, find all elements satisfying Amax and Bmax constraint and store C values for all such elements then find Mth smallest value in array for Cmax.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like