VOOZH about

URL: https://www.geeksforgeeks.org/dsa/scheduling-priority-tasks-limited-time-minimizing-loss/

⇱ Scheduling priority tasks in limited time and minimizing loss - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Scheduling priority tasks in limited time and minimizing loss

Last Updated : 14 Mar, 2023

Given n tasks with arrival time, priority and number of time units they need. We need to schedule these tasks on a single resource. The objective is to arrange tasks such that maximum priority tasks are taken. Objective is to minimize sum of product of priority and left time of tasks that are not scheduled due to limited time. This criteria simply means that the scheduling should cause minimum possible loss. Examples:

Input : Total time = 3
 Task1: arrival = 1, units = 2, priority = 300
 Task2: arrival = 2, units = 2, priority = 100
Output : 100
Explanation : Two tasks are given and time to finish 
them is 3 units. First task arrives at time 1 and it
needs 2 units. Since no other task is running at that
time, we take it. 1 unit of task 1 is over. Now task 2 
arrives. The priority of task 1 is more than task 2 
(300 > 100) thus 1 more unit of task 1 is employed. 
Now 1 unit of time is left and we have 2 units of task
2 to be done. So simply 1 unit of task 2 is done and 
the answer is ( units of task 2 left X priority of 
task 2 ) = 1 X 100 = 100

Input : Total Time = 3
 Task1: arrival = 1, units = 1, priority = 100
 Task2: arrival = 2, units = 2, priority = 300
Output : 0

We use a priority queue and schedule one unit of task at a time.

  1. Initialize total loss as sum of each priority and units. The idea is to initialize result as maximum, then one by one subtract priorities of tasks that are scheduled.
  2. Sort all tasks according to arrival time.
  3. Process through each unit of time from 1 to total time. For current time, pick the highest priority task that is available. Schedule 1 unit of this task and subtract its priority from total loss.

Below is C++ implementation of above steps. 

Output:

100

Time Complexity: O(nlogn), used for sorting the array
Auxiliary Space: O(n), as extra space of size n, is used to make a vector of pairs.

Comment
Article Tags:
Article Tags: