# Data Structure Dr. Mohamed Khafagy.

## Presentation on theme: "Data Structure Dr. Mohamed Khafagy."— Presentation transcript:

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 The first one in line is the first one to be served cinemark

First In First Out rear front D C B A B A C B A D C B A 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. a0 a1 a2 an-1 front rear

Basic operations of a queue
Enqueue New ? Dequeue Empty? 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(int MaxQueueSize = 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(const KeyType& 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
front rear front rear front rear

Exercise: Queues Describe the output of the following series of queue operations enqueue(8) enqueue(3) dequeue() enqueue(2) enqueue(5) enqueue(9) enqueue(1)

Queue ADT (cont’d) Boolean isEmptyQ(queue) ::= if (queue ==CreateQ(max_queue_size)) return TRUE else return FALSE Element dequeue(queue) ::= if (IsEmptyQ(queue)) return else remove and return the item at front of queue.

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 ] . . . Just like our stack implementation in the previous chapter, one way to implement a queue is to store the elements in an array. 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.

Array Implementation size 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 first last 2 The easiest implementation also keeps track of three numbers. The size could be as small as zero or as large as the number of items in the array. The index of the front element is stored in the first member variable. The front item in the queue is at that index of the array. The next item is after the first one and so on until the rear of the queue that occurs at the index stored in a member variable called last. [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . . 4 8 6

A Dequeue Operation size When an element leaves the queue, size is decremented, and first changes, too. 2 first 1 last 2 This shows how the member variables change when an item leaves the queue. [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . . 4 8 6

An Enqueue Operation size When an element enters the queue, size is incremented, and last changes, too. 3 first 1 last 3 And this shows how the member variables change when a new item enters the queue. For a fixed size array, a new item may enter only if the current size of the queue is less than the size of the array. For a dynamic array, we could increase the size of the array when the queue grows beyond the current array size. [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . . 8 6 2

At the End of the Array size 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 first 3 last 5 An array implementation of a queue must have special behavior when the rear of the queue reaches the end of the array. In this example, suppose we want to add the number 4 to the queue. We can do so… [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] 2 6 1

At the End of the Array size The new element goes at the front of the array (if that spot isn’t already used): 4 first 3 last …by putting it at location 0 (if that location is not already used). [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] 4 2 6 1

Array Implementation size 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 first last 2 Here are some of the key aspects of an array implementation of a queue. [ 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 n-4 n-4 3 3 n-3 n-3 2 2 n-2 n-2 1 1 n-1 n-1
J4 J3 n-4 n-4 3 3 J1 J2 n-3 n-3 J2 2 J1 2 J3 J4 n-2 n-2 1 1 n-1 n-1 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.

Enqueue in a Circular Queue
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; }

Dequeue from 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]; }

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

Applications: Job Scheduling

Two kinds of priority queues: Min priority queue. Max priority queue.

Min Priority Queue empty size
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 empty size
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 Insert the five elements into a max priority queue.
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 five remove max operations placing removed elements into the sorted array from right to left.

After Inserting Into Max Priority Queue
8 4 6 Max Priority Queue 1 2 Sorted Array

After First Remove Max Operation
4 6 Max Priority Queue 1 2 8 Sorted Array

After Second Remove Max Operation
4 Max Priority Queue 1 2 6 8 Sorted Array

After Third Remove Max Operation
Max Priority Queue 1 2 4 6 8 Sorted Array

After Fourth Remove Max Operation
Max Priority Queue 1 2 4 6 8 Sorted Array

After Fifth Remove Max Operation
Max Priority Queue 1 2 4 6 8 Sorted Array

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 Example schedule is constructed by scheduling the jobs in the order they appear in the given job list (left to right); each job is scheduled on the machine on which it will complete earliest. 2 7 21 B 3 13 C time >

Machine Scheduling Example
6 13 A 2 7 21 B 3 13 C time > Finish time = 21 Objective: Find schedules with minimum finish time.

LPT Schedules 14, 10, 7, 6, 5, 3, 2 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 Finish time is 16! [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 m machines. 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.