-2

I am having an algorithm and wanted to find it time and space complexity. Algorithm is

 Algorithm for Scheduling
 INPUT:P1,P2, PN // N processes
 do
  Sort processes in ascending according to their burst time 
  TQ = median of burst time of processes 
  For (i=1 to N )
   Assign TQ to Process Pi 
   If Burst time of process i < TQ 
    remove Process i from array
   Else
    BT of process i = BT of process i - TQ
  Compute waiting time for all process in array
 While (array of processes != empty)

How to calculate time and space complexity of this algorithm?

Asif
  • 763
  • 8
  • 18
  • How do you `remove` and `push`? Do you need the WC or AC ? –  Sep 13 '19 at 08:42
  • @YvesDaoust I updated algorithm and part where pushing process back to array in skipped now. For Removing ,simply set its Burst time (BT) to zero – Asif Sep 13 '19 at 09:53
  • What does the last line? "Compute waiting time..." – PF. Castro Sep 13 '19 at 11:47
  • @PF.Castro An array is there that stores Waiting time of each process and in each iteration waiting time of process it updated. I have not shown detail of that part but it takes O(1) to update Waiting time of a process – Asif Sep 13 '19 at 11:59
  • *"simply set its Burst time (BT) to zero"*: that is not the same as removal. This affects the task **Compute waiting time for all process in array**, **Sort** and **median**, which would have to somehow skip zeroes. – trincot Sep 13 '19 at 12:15
  • Your algorithm presentation is too inaccurate. –  Sep 13 '19 at 12:38

1 Answers1

0

In comments you state that it takes O(1) to update the waiting time of a process, so that means the following line takes O(N):

Compute waiting time for all process in array

Concerning the remove operation you also suggest that it is O(1), although you should not set the corresponding waiting time to zero, as that would impact other actions on the array (like sorting and getting the median). Better is to copy the last process into slot i, and then drop the last element from the array. That would be O(1).

The execution of the for loop then takes O(N). Finding the median in a sorted array is also O(N), so in the do body the operation with the greatest time complexity is the sorting: O(N⋅logN)

In each iteration of the do loop, the array size (N) is halved.

So we have the following equation (leaving out the big-O notation):

T(N) = N⋅log(N) + T(N/2)
     = N⋅log(N) + (N/2)⋅log(N/2) + (N/4)⋅log(N/4) + ...
     = N⋅(log(N) + (1/2)⋅log(N/2) + (1/4)⋅log(N/4) + ...)

Now log(N/c) = log(N) - log(c), so we can group terms like follows:

     = N⋅((1 + 1/2 + 1/4 + ...)⋅log(N) - log(2)/2 - log(4)/4 - log(8)/8 - ...)
     = N⋅(2⋅log(N) - log(2)/2 - log(4)/4 - log(8)/8 - ...)
     < 2N⋅log(N)

So we started out that T(N) is at least O(N⋅log(N)), and we derive that it is at most O(N⋅log(N)), so we have a tight bound for the time complexity:

      O(N⋅log(N))

trincot
  • 317,000
  • 35
  • 244
  • 286