Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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 First In First Out A rear front B A rear front C B A rear front D C B A rear front D C B 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 front a1 an-1 a2 rear 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(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 front rear rear front rear 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) 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] 4 8 6 An array of integers to implement a queue of integers [3] [4] [5] ... We don't care what's in this part of the array. 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] 4 8 6 [3] [4] [5] ... 3 size 0 first 2 last A Dequeue Operation When an element leaves the queue, size is decremented, and first changes, too. [0] [1] [2] 4 8 6 [3] [4] [5] ... 2 size 1 first 2 last An Enqueue Operation When an element enters the queue, size is incremented, and last changes, too. [0] [1] [2] [3] 8 6 2 [4] [5] ... 3 size 1 first 3 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]: [0] [1] [2] [3] [4] [5] 2 6 1 3 size 3 first 5 last At the End of the Array The new element goes at the front of the array (if that spot isn’t already used): [0] 4 [1] [2] [3] [4] [5] 2 6 1 4 size 3 first 0 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. [0] [1] [2] 4 8 6 [3] [4] [5] ... 3 size 0 first 2 last 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 J3 3 J2 2 n-3 J1 1 n-2 0 n-1 front = 0; rear = 4 n-4 3 J1 n-3 J2 2 J4 1 0 J3 n-2 n-1 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 front rear Q[0] Q[1] Q[2] Q[3] -1 -1 -1 0 J1 -1 1 J1 J2 -1 2 J1 J2 J3 0 2 J2 J3 1 2 J3 Comments queue is empty Job 1 is added Job 2 is added Job 3 is added Job 1 is deleted Job 2 is deleted 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 extract 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 five remove max operations placing removed elements into the sorted array from right to left. After Inserting Into Max Priority Queue 8 4 1 Sorted Array 2 6 Max Priority Queue After First Remove Max Operation 4 1 6 Max Priority Queue 2 8 Sorted Array After Second Remove Max Operation 4 1 Max Priority Queue 2 6 Sorted Array 8 After Third Remove Max Operation 1 2 4 Sorted Array Max Priority Queue 6 8 After Fourth Remove Max Operation Max Priority Queue 1 2 Sorted Array 4 6 8 After Fifth Remove Max Operation Max Priority Queue 1 Sorted Array 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 A 2 7 21 B 3 13 C time -----------> Finish time = 21 Objective: Find schedules with minimum finish 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 C Finish time is 16! 13 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.