How to calculate departure times in a LIFO queue?

Viewed 41

Assume that we have the arrival times and the service times that each customer will require in a single server queue. If we use a FIFO strategy, we can calculate the departure times using the following code:

departures[0] = arrivals[0] + services[0]
for i in range(1, n):
    if arrivals[i] < departures[i-1]:
        departures[i] = departures[i-1] + services[i]
    else:
        departures[i] = arrivals[i] + services[i]

What if there is a LIFO strategy, i.e, when the server completes a service, the next customer to enter service is the last to join the line. I have trouble coding this one in python and I appreciate your help.

1 Answers

I wouldn't consider the below solution elegant but it should get the job done. Basically, the idea is to add a Lifo Queue with the people who have already arrived and are waiting for the service.

from queue import LifoQueue 
waiting = LifoQueue()

departures = [arrivals[0] + services[0]]
i = 1
while(i < n):
    if arrivals[i] <= departures[-1]:
        waiting.put(i)
        i += 1
        continue
    if not waiting.empty():
        index = waiting.get()
        departures.append(departures[-1] + services[index])
    else:
        departures.append(arrivals[i] + services[i])
        i += 1

while(not waiting.empty()):
    index = waiting.get()
    departures.append(departures[-1] + services[index])
Related