440 likes | 548 Views
Data Structure. Dr. Mohamed Khafagy. Queues. Queue. Stores a set of elements in a particular order Stack principle: FIRST IN FIRST OUT = FIFO It means: the first element inserted is the first one to be removed Example
E N D
Data Structure Dr. Mohamed Khafagy
Queue • Stores a set of elements in a particular order • Stack principle: FIRST IN FIRST OUT • = FIFO • It means: the first element inserted is the first one to be removed • Example • The first one in line is the first one to be served cinemark
First In First Out D C B rear front A B A C B A D C B A rear front rear front rear front rear front
Queue • A queue is an ordered list in which all insertions take place at one end and all deletions take place at the opposite end. It is also known as First-In-First-Out (FIFO) lists. an-1 a0 a1 a2 rear front
Basic operations of a queue Enqueue New ? Empty? Dequeue Full?
ADT 3.2 Abstract Data Type Queue Template <class KeyType> class Queue { // objects: A finite ordered list with zero or more elements public: Queue(intMaxQueueSize = DefaultSize); // Create an empty queue whose maximum size is MaxQueueSize Boolean IsFull(); // if number of elements in the queue is equal to the maximum size of // the queue, return TRUE(1); otherwise, return FALSE(0) void Add(constKeyType& item); // if IsFull(), then QueueFull(); else insert item at rear of the queue Boolean IsEmpty(); // if number of elements in the queue is equal to 0, return TRUE(1) // else return FALSE(0) KeyType* Delete(KeyType&); // if IsEmpty(), then QueueEmpty() and return 0; // else remove the item at the front of the queue and return a pointer to it };
Shifting Elements in Queue rear front front rear rear front
Exercise: Queues • Describe the output of the following series of queue operations • enqueue(8) • enqueue(3) • dequeue() • enqueue(2) • enqueue(5) • dequeue() • dequeue() • enqueue(9) • enqueue(1)
Boolean isEmptyQ(queue) ::= if (queue ==CreateQ(max_queue_size))return TRUEelse returnFALSE Element dequeue(queue) ::=if (IsEmptyQ(queue)) return else remove and return the item at front of queue. Queue ADT (cont’d)
Array-based Queue Implementation • As with the array-based stack implementation, the array is of fixed size • A queue of maximum N elements • Slightly more complicated • Need to maintain track of both front and rear Implementation 1 Implementation 2
Array Implementation • A queue can be implemented with an array, as shown here. For example, this queue contains the integers 4 (at the front), 8 and 6 (at the rear). [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . . 4 8 6 An array of integers to implement a queue of integers We don't care what's in this part of the array.
first size last Array Implementation • The easiest implementation also keeps track of the number of items in the queue and the index of the first element (at the front of the queue), the last element (at the rear). 3 0 2 [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . . 4 8 6
first size last A Dequeue Operation • When an element leaves the queue, size is decremented, and first changes, too. 2 1 2 [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . . 4 8 6
first size last An Enqueue Operation • When an element enters the queue, size is incremented, and last changes, too. 3 1 3 [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . . 2 8 6
first size last At the End of the Array • There is special behavior at the end of the array. For example, suppose we want to add a new element to this queue, where the last index is [5]: 3 3 5 [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] 2 6 1
last size first At the End of the Array • The new element goes at the front of the array (if that spot isn’t already used): 4 3 0 [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] 4 2 6 1
first size last Array Implementation • Easy to implement • But it has a limited capacity with a fixed array • Or you must use a dynamic array for an unbounded capacity • Special behavior is needed when the rear reaches the end of the array. 3 0 2 [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . . 4 8 6
Circular Queue • To resolve the issue of moving elements in the queue, circular queue assigns next element to q[0] when rear == MaxSize – 1. • Pointer front will always point one position counterclockwise from the first element in the queue. • Queue is empty when front == rear. But it is also true when queue is full. This will be a problem.
Circular Queue (Cont.) 4 4 J4 n-4 n-4 J3 3 3 J1 J2 n-3 n-3 J2 J1 2 2 J3 J4 n-2 n-2 1 1 n-1 n-1 0 0 front = 0; rear = 4 front = n-4; rear = 0
Circular Queue (Cont.) • To resolve the issue when front == rear on whether the queue is full or empty, one way is to use only MaxSize – 1 elements in the queue at any time. • Each time when adding an item to the queue, newrear is calculated before adding the item. If newrear == front, then the queue is full. • Another way to resolve the issue is using a flag to keep track of last operation. The drawback of the method is it tends to slow down Add and Delete function.
void enqueue(int front, int *rear, element item){/* add an item to the queue */ *rear = (*rear +1) % MAX_QUEUE_SIZE; if (front == *rear) /* reset rear and print error */ return; } queue[*rear] = item; } Enqueue in a Circular Queue
element dequeue(int* front, int rear){ element item; /* remove front element from the queue and put it in item */ if (*front == rear) return queue_empty( ); /* queue_empty returns an error key */ *front = (*front+1) % MAX_QUEUE_SIZE; return queue[*front];} Dequeue from Circular Queue
Applications of Queues • Direct applications • Waiting lines • Access to shared resources (e.g., printer) • Multiprogramming • Indirect applications • Auxiliary data structure for algorithms • Component of other data structures
Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue.
Min Priority Queue Collection of elements. Each element has a priority or key. Supports following operations: empty size insert an element into the priority queue (push) get element with min priority (top) remove element with min priority (pop)
Max Priority Queue Collection of elements. Each element has a priority or key. Supports following operations: empty size insert an element into the priority queue (push) get element with max priority (top) remove element with max priority (pop)
Operations supported by priority queue • The functions that have been supported: • SP1: Return an element with minmum priority • SP2: Insert an element with an arbitrary priority • SP2: Delete an element with minimum priority • Extended functions: • Meld two priority queues • delete an arbitrary element • decrease the key/priority
Double-ended priority queue(DEPQ) • Support the following operations • DP1: Return an element with minimum priority • DP2: Return an element with maximum priority • DP3: Insert an element with an arbitrary • DP4: Delete an element with minimum priority • DP5: Delete an element with maximum priority
Applications Sorting use element key as priority insert elements to be sorted into a priority queue remove/pop elements in priority order if a min priority queue is used, elements are extracted in ascending order of priority (or key) if a max priority queue is used, elements are extracted in descending order of priority (or key)
Sorting Example Sort five elements whose keys are 6, 8, 2, 4, 1 using a max priority queue. Insert the five elements into a max priority queue. Do fiveremove max operations placing removed elements into the sorted array from right to left.
After Inserting Into Max Priority Queue Sorted Array 8 4 6 Max Priority Queue 1 2
After First Remove Max Operation Sorted Array 4 6 Max Priority Queue 1 2 8
After Second Remove Max Operation Sorted Array 4 Max Priority Queue 1 2 6 8
After Third Remove Max Operation Sorted Array Max Priority Queue 1 2 4 6 8
After Fourth Remove Max Operation Sorted Array Max Priority Queue 1 2 4 6 8
After Fifth Remove Max Operation Sorted Array Max Priority Queue 1 2 4 6 8
Machine Scheduling m identical machines (drill press, cutter, sander, etc.) n jobs/tasks to be performed assign jobs to machines so that the time at which the last job completes is minimum
Machine Scheduling Example 3 machines and 7 jobs job times are [6, 2, 3, 5, 10, 7, 14] possible schedule 6 13 A 2 7 21 B 3 13 C time ----------->
Machine Scheduling Example 6 13 Finish time = 21 Objective: Find schedules with minimum finish time. A 2 7 21 B 3 13 C time ----------->
LPT Schedules Longest Processing Time first. Jobs are scheduled in the order 14, 10, 7, 6, 5, 3, 2 Each job is scheduled on the machine on which it finishes earliest.
LPT Schedule [14, 10, 7, 6, 5, 3, 2] 14 16 A 10 15 B 7 13 16 C Finish time is 16!
Using A Min Priority Queue Min priority queue has the finish times of the mmachines. Initial finish times are all 0. To schedule a job remove machine with minimum finish time from the priority queue. Update the finish time of the selected machine and insert the machine back into the priority queue.