


You have been given the ‘N’ process with process id’s from 1 to ‘N’. For each process, you are given the arrival time and burst time. You are also provided with the time quantum. Now you are supposed to execute a round-robin scheduling algorithm and find the completion time for each given process.
Please refer to Round-robin_scheduling for more detailed information .
Note:A time quantum is associated with the algorithm. Each process gets CPU for some units of time, which are decided by time quantum value, and then again enter the ready queue if they have burst time remaining.
Arrival Time - The time when the process enters the system and is ready for execution.
Burst Time - The total time taken by the process for execution.
Completion Time - The time when the process completes its execution and leaves the system.
Note:
If two processes are having the same arrival time, then a process with a lower ID will be executed first.
The first line contains an integer ‘T’ denoting the number of test cases. Then each test case follows.
The first input line of each test case contains two integers ‘N’ and ‘M’, denoting the number of processes and the quantum time, respectively.
Each of the next ‘N’ lines contains two space integers denoting the arrival time and burst time of the ith process.
Output Format :
For each test case, print a single line containing ‘N’ space-separated integers denoting the complete time of each process.
The output of each test case will be printed in a separate line.
Note :
You are not required to print the expected output; it has already been taken care of. Just implement the function.
1 <= T <= 5
1 <= N <= 1000
1 <= M <= 1000
1 <= ARRIVALTIME[i], BURSTTIME[i] <= 1000
Where ‘N’ and ‘M’ denotes the number of processes and the quantum time, respectively.
Time limit: 1 sec
2
4 2
0 5
1 4
2 2
3 1
1 4
3 5
12 11 6 9
8
For the first test case,
Assume {a, (l : r)} denotes a pair of integers where ‘a’ denotes the process index and (l : r) denotes the start time ( l ) and end time ( r ) of a particular instance of execution. Basically it means process ‘a’ got executed from lth to rth unit of time. Following round robin scheduling the execution order will be {0, (0 : 2)} -> {1, (2 : 4)} -> {2, (4 : 6)} -> {0, (6 : 8)} -> {4, (8 : 9)} -> {1, (9: 11)} -> {0, (11: 12)}.
For the second test case, there is only one process. Its completion time will be 8.
2
2 10
0 3
0 5
2 1
1 1
2 1
3 8
2 3
For the first test case, the completion time of processes will be [3, 8].
For the second test case, the completion time of processes will be [2, 3].
Can you think of using a queue?
The basic idea of this approach is to use a queue to solve the problem. We’ll keep the process in the queue which needs to be executed. And when a process is executed for the time quantum time, then we’ll push it to the end of the queue.
Consider the following steps:
O(N * log(N) + W / M), where ‘N’ is the total number of processes, and ‘W’ is the sum of burst times of the given process, and ‘M’ is the time quantum.
Since we are sorting the array/list of processes which will take O(N * log(N)) time and then at each iteration, we are executing a process for at max one quantum time, so the overall time complexity will be O(N * log(N) + W / M).
O(N), where ‘N’ is the total number of processes.
Since we are using an array/list to store the remaining burst time and completion time for each process and also using a queue that stores at max ‘N’ elements so the overall space complexity will be O(N).