Neha Patil (Editor)

Calendar queue in simulation

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit

A calendar queue (CQ) is a priority queue (queue in which every element has associated priority). It is analogous to desk calendar, which is used by humans for ordering future events by date. And time is a priority when two or more events on the same date. As Discrete event simulations require a future event list (FEL) structure to manage events according to their time. So, choice of good and efficient data structure is important as much time spent on its management. Calendar queue is designed for it with O(1) average performance.

Contents

Implementation

Theoretically, a calendar queue consists of an array of linked lists. Sometimes each index in the array also referred as a bucket. The bucket has specified width and stores events in that timestamp. As desk calendar has 365 buckets for each day with a width of one day. Each array element contains one pointer for corresponding linked list. If the array name is “month” then month[11] is a pointer to the list of events scheduled for the 11th month of the year. The complete calendar thus consists of an array of 12 pointers and a collection of up to 12 linked lists. In calendar queue, enqueue (addition in a queue) and dequeue (deleting from a queue) of events in FEL is based on event time.

Let the calendar queue with n buckets with w width. Then enqueue of an event with time t is in ( t / w ) % n . And more than two events scheduled in the bucket according to the increased timestamp. To dequeue events from the calendar queue, it keeps track of current year and day. Then it searches for the earliest event and dequeue it. It also used as circular as next year events can be saved in the bucket with the year that does not allow it to remove from the bucket before its time.

Calendar Queue Resize Operation

If the number of events in the queue is much smaller or much larger than the number of buckets, it will not function efficiently. The solution is to allow the number of buckets to correspondingly grow and shrink as the queue grows and shrinks. To simplify the resize operation, the Nb(number of buckets) in a CQ is often chosen to be the power of two, i.e. N b = 2 n ;↵

The number of buckets is doubled or halved each time the Ne(number of events) exceeds 2Nb or decreases below Nb/2 respectively. When Nb is resized, the new width w has to be calculated as well. The new w that is adopted will be estimated by sampling the average inter-event time gap from the first few hundred events starting at the current bucket position. Thereafter, a new Calendar queue is created and all the events in the old calendar will be recopied over.

References

Calendar queue in simulation Wikipedia