Download presentation

Presentation is loading. Please wait.

1
Data Structure Dr. Mohamed Khafagy

2
Queues

3
**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

4
**First In First Out rear front D C B A B A C B A D C B A rear front**

5
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

6
**Basic operations of a queue**

Enqueue New ? Dequeue Empty? Full?

7
**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 };

8
**Shifting Elements in Queue**

front rear front rear front rear

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

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

11
**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

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

13
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

14
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

15
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

16
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

17
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

18
**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

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

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

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

22
**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; }

23
**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]; }

24
**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

25
**Applications: Job Scheduling**

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

27
**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)

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

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

30
**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

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

32
**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.

33
**After Inserting Into Max Priority Queue**

8 4 6 Max Priority Queue 1 2 Sorted Array

34
**After First Remove Max Operation**

4 6 Max Priority Queue 1 2 8 Sorted Array

35
**After Second Remove Max Operation**

4 Max Priority Queue 1 2 6 8 Sorted Array

36
**After Third Remove Max Operation**

Max Priority Queue 1 2 4 6 8 Sorted Array

37
**After Fourth Remove Max Operation**

Max Priority Queue 1 2 4 6 8 Sorted Array

38
**After Fifth Remove Max Operation**

Max Priority Queue 1 2 4 6 8 Sorted Array

39
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

40
**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 >

41
**Machine Scheduling Example**

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

42
**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.

43
**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!

44
**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

OK

ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Free download ppt on diversity in living organisms S tv do ppt online Ppt on blue ocean strategy Ppt on aircraft emergencies videos Ppt on india political history Ppt on barack obama leadership behaviors Ppt on environmental protection Ppt on film industry bollywood dance Ppt on combination of resistances training Ppt on building management system