Download presentation

Presentation is loading. Please wait.

Published byBella Dundon Modified about 1 year ago

1

2
Data Structure Dr. Mohamed Khafagy

3
Queues

4
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

5
A BABA CBACBA DCBADCBA DCBDCB rear front rear front rear front rear front rear front First In First Out

6
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. a0a0 a1a1 a2a2 a n-1 front rear

7
Basic operations of a queue New Enqueue Dequeue Empty? ? Full?

8
ADT 3.2 Abstract Data Type Queue Template 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 };

9
Shifting Elements in Queue front rear front rear front rear

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

11
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. 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. Queue ADT (cont’d)

12
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

13
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 ]... An array of integers to implement a queue of integers 48 6 We don't care what's in this part of the array.

14
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). [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] size 3 first 0 last 2

15
A Dequeue Operation When an element leaves the queue, size is decremented, and first changes, too. [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] size 2 first 1 last 2

16
An Enqueue Operation When an element enters the queue, size is incremented, and last changes, too. [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] size 3 first 1 last 3

17
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]: [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] 216 size 3 first 3 last 5

18
At the End of the Array The new element goes at the front of the array (if that spot isn’t already used): [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] 216 size 4 first 3 last 0 4

19
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. [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] size 3 first 0 last 2

20
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.

21
Circular Queue (Cont.) n-1 n-2 n-3 n-4 front = 0; rear = n-1 n-2 n-3 n-4 front = n-4; rear = 0 J1 J2 J3 J4 J1 J2 J3 J4

22
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.

23
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

24
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

25
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

26
Applications: Job Scheduling

27
Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue.

28
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)

29
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)

30
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

31
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

32
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)

33
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 five remove max operations placing removed elements into the sorted array from right to left.

34
After Inserting Into Max Priority Queue Sorted Array Max Priority Queue

35
After First Remove Max Operation Sorted Array Max Priority Queue

36
After Second Remove Max Operation Sorted Array Max Priority Queue

37
After Third Remove Max Operation Sorted Array Max Priority Queue

38
After Fourth Remove Max Operation Sorted Array Max Priority Queue

39
After Fifth Remove Max Operation Sorted Array Max Priority Queue

40
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

41
Machine Scheduling Example 3 machines and 7 jobs job times are [6, 2, 3, 5, 10, 7, 14] possible schedule A B C time >

42
Machine Scheduling Example Finish time = 21 Objective: Find schedules with minimum finish time. A B C time >

43
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.

44
LPT Schedule [14, 10, 7, 6, 5, 3, 2] A B C Finish time is 16!

45
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.

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google