Rossella Lau Lecture 11, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 11: Queue & Priority Queue  Basic operations.

Slides:



Advertisements
Similar presentations
Queue Definition Ordered list with property: –All insertions take place at one end (tail) –All deletions take place at other end (head) Queue: Q = (a 0,
Advertisements

Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
Rossella Lau Lecture 12, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 12: Stack and Expression Evaluation  Stack basic.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture24.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Data Structure Dr. Mohamed Khafagy.
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Chapter 13 Queues and Priority Queues CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Rossella Lau Lecture 8, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 8: Trees  General model of a tree  Binary Tree.
Rossella Lau Lecture 5, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 5: Deque Comparison of sequence containers  Deque.
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
1 Queues CPS212 Gordon College. 2 Introduction to Queues A queue is a waiting line – seen in daily life –Real world examples – toll booths, bank, food.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Rossella Lau Lecture 1, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 1: Introduction What this course is about:  Data.
Rossella Lau Lecture 3, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 3: Basics of Linked List  C++ pointer revision.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Rossella Lau Lecture 2, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure.
Rossella Lau Lecture 4, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 4: C++ and list  Usage of Vector and List  C++
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
Standard Template Library (STL) Overview – Part 1 Yngvi Bjornsson.
Priority Queues Briana B. Morrison Adapted from Alan Eugenio Sell100IBM$122 Sell300IBM$120 Buy500IBM$119 Buy400IBM$118.
Rossella Lau Lecture 1, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 1: Introduction What this course is about:  Data.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 8 Ming Li Department of.
Rossella Lau Lecture 12, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced.
Containers Overview and Class Vector
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Queue 1 Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
Queue What is a queue?. Queues A queue is similar to waiting in line for a service, e.g., at the bank, at the bathroom –The first item put on the queue.
CS342 Data Structures End-of-semester Review S2002.
EC-211 DATA STRUCTURES LECTURE 9. Queue Data Structure An ordered group of homogeneous items or elements. Queues have two ends: – Elements are added at.
Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview of Lecture  Introduction  The Queue ADT  The Radix.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Computer Science and Software Engineering University of Wisconsin - Platteville 11.Standard Template Library Yan Shi CS/SE 2630 Lecture Notes.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
1 Queues Queue API Application: Radix Sort Implementation: Using Deque Using Deque Circular Array Circular Array Priority Queue Priority Queue API Implementation.
Fall 2006 METU EEEEE 441 S. Ece (GURAN) SCH MIDT EE 441 Data Structures Lecture 6 Stacks and Queues.
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
Properties: -The value in each node is greater than all values in the node’s subtrees -Complete tree! (fills up from left to right) Max Heap.
Copyright © Curt Hill STL Priority Queue A Heap-Like Adaptor Class.
1 The Standard Template Library The STL is a collection of Container classes These are class templates for containers. A container is an object that stores.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
1 Priority Queues (Heaps)  Sections 6.1 to Priority Queues  Regular queues which supports –First In, First Out –Enqueue(): add a new element.
Review Array Array Elements Accessing array elements
Queues.
Priority Queues (Heaps)
Briana B. Morrison Adapted from Alan Eugenio
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Priority Queues Sections 6.1 to 6.5.
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
CMSC 341 Lecture 5 Stacks, Queues
Lecture 13 – Heaps Container Adapters priority_queue – impl. with heap
CSC 143 Queues [Chapter 7].
Queues Jyh-Shing Roger Jang (張智星)
Containers: Queue and List
Visit for more Learning Resources
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
Data Structures & Programming
Presentation transcript:

Rossella Lau Lecture 11, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 11: Queue & Priority Queue  Basic operations of a queue  Applications of a queue  Priority queue and the STL’s priority queue -- By Rossella Lau

Rossella Lau Lecture 11, DCO20105, Semester A, Queue  Queue is an ordered list of items, ordered in the input sequence  Basically, items  are deleted at one end (the front/head of the queue) and  added at the other end (the rear/tail of the queue)  An example: ABC D The original queue Take an itemAdd an item BC

Rossella Lau Lecture 11, DCO20105, Semester A, Operations of a queue  Basic:  push() – to add an item; formerly called enqueue()  pop() – to remove an item; formerly called dequeue()  Assistance:  size() – returns the number of items in a queue  empty() – to check if a queue is empty  front() – returns the first element of a queue

Rossella Lau Lecture 11, DCO20105, Semester A, Exercises  Ford’s 8:9 queue q; int x = 5, y = 3; q.push(8); q.push(9); q.push(y); X = q.fron(); q.pop(); q.push(18); x = q.front(); q.push(22); while (!q.empty()) { y = q.front(); q.pop(); cout << y << “ “; } cout << x << endl;  Ford’s 8:10: List the elements in intQ after each of the following operations: queue intQ; intQ.push(18); intQ.push(2); intQ.push(intQ.front()); intQ.pop();

Rossella Lau Lecture 11, DCO20105, Semester A, Typical applications  A real life application – check out queues in a super market (Ford’s slide: 8.2)  When a customer goes to a cashier – push() If there is a cashier available, check out at that cashier Line up a queue at the end and wait for checking out  When a cashier is working – pop() Perform check out for the first customer on her/his line one by one

Rossella Lau Lecture 11, DCO20105, Semester A, Services for others  As a service for other data structures and algorithms  Radix sort  Traverse a binary tree in a breadth first order; i.e., from top to bottom and at each level, from left to right

Rossella Lau Lecture 11, DCO20105, Semester A, Re-visit the Radix sort push(n i ) to qs[k] for (i=0;i<10,i++) while (!qs[i].empty()) qs[i].pop();

Rossella Lau Lecture 11, DCO20105, Semester A, Breadth First Search (BFS) order  A BT’s Breadth First Search order is similar to the implicit array index  E.g., The BFS order for the BT on the right hand side is: 92, 37, 86, 33, 12, 48, 57,

Rossella Lau Lecture 11, DCO20105, Semester A, #include …… void bfs(void) const { queue *> children; children.push(root); while (! children.empty()) { if (children.front()->left) children.push(children.front()->left); if (children.front()->right) children.push(children.front()->right); cout getItem() << " " ; children.pop(); } cout << endl; } BFS traversal

Rossella Lau Lecture 11, DCO20105, Semester A, Implementation of a queue When using a vector:  As elements grow (shift) in one direction, slots in the beginning become useless while new elements may be required to re-size the array frequently  Another approach is to use an array in a cyclic method -- calculation of the index becomes a bit more complicated and care should be taken in how to determine the front and the rear of the queue and an empty queue

Rossella Lau Lecture 11, DCO20105, Semester A, Using a cyclic vector for implementing queue ABC frontrear pop() : BCD frontrear push() : CD frontrear pop() : CDE frontrear push() : FCDE front rear push() : FDE frontrear pop() : BC frontrear Initial :

Rossella Lau Lecture 11, DCO20105, Semester A, Test of empty()  When front points to the first element of a queue, it is difficult to test if a queue is empty or has a single element Initial or empty: front rear front rear single element: A front rear

Rossella Lau Lecture 11, DCO20105, Semester A, Modification of front A special way is to make the front point to the vector index immediately preceding the first element. BC frontrear pop() : BCD frontrear push() : CD frontrear pop() : CDE frontrear push() : FCDE front rear push() : FDE front rear pop() :

Rossella Lau Lecture 11, DCO20105, Semester A, list-based queue  Seems a more natural way to implement queue  But the cost of the operation new/delete makes a list- based container not favorable

Rossella Lau Lecture 11, DCO20105, Semester A, Implementation of a queue using a list // Queue.h template class Queue { private: list queue; public: T const & front() const { return queue.front();} void push(T const & item) {queue.push_back(item);} T & pop() {T t(front()); queue.pop_front(); return t;} size_t size() const { return queue.size();} bool empty() const { return queue.empty();} };

Rossella Lau Lecture 11, DCO20105, Semester A, Execution time for operations of a queue  No matter which basic container is used to build a queue,  a push() is actually a push_back()  a pop() is similar to a pop_front() Although pop_front() is not allowed in a STL’s vector, it can use a self-defined index to remember its head and move it forward to represent a pop() operation It can also use deque  Both operations are at O(1)  Operations of a STL’s queue: Ford’s slides: 8.4-6

Rossella Lau Lecture 11, DCO20105, Semester A, Priority Queue  Many applications need elements to queue up with priority.  Queues in bank: general a/c holders, priority a/c holders  Task queues: jobs assigning a higher priority can be executed earlier

Rossella Lau Lecture 11, DCO20105, Semester A, Exercises  Ford’s 8:15 priority_queue pq; int arr[] = {10,50,30,40,60}; int i, x, y; for (i=0; i< 5; i++) pq.push(arr[i]); pq.push(25); x = pq.top(); pq.pop(); pq.push(35); y = pq.top(); pq.pop(); cout << x << “ “ << y << endl; while (!pq.empty()) { cout << pq.top() << “ “; pq.pop(); } cout << endl;  Ford’s 8:16: priority_queue intPQ; intPQ.push(5); intPQ.push(27); intPQ.push(25); intPQ.pop(); intPQ.push(intPQ.top()*2); intPQ.push(intPQ.top()*5); While (!intPQ.empty()) { cout<< intPQ.top() << “ “; intPQ.opp(); } cout << endl;

Rossella Lau Lecture 11, DCO20105, Semester A, priority_queue  The STL provides the class priority_queue  E.g., push(taskA,5), push(taskB,5), push(taskC,8), push(taskD,2), push(taskE,5), push(taskF,8)  pop() returns (taskC, 8)  priority_queue uses a heap to insert an item A heap works quite efficiently on vector and the STL uses a vector to implement priority_queue by default  The templated class of the priority_queue must provide the comparison operator <() for the heap construction

Rossella Lau Lecture 11, DCO20105, Semester A, An example application  class task { string taskName, int priority; …}  operator< must be overloaded  bool task::operator<(task const & rhs) const { return priority < rhs.priority; }  The application:  priority_queue taskQ;  ……  taskQ.push(taskObj);  ……  resultTask=taskQ.front(); taskQ.pop();

Rossella Lau Lecture 11, DCO20105, Semester A, Summary  Queue can use different kinds of sequential containers with restricted operations as adaptor containers in the STL  Both the basic operations of a queue are at O(1) and make a queue one of the favored data structures being used in many other algorithms  Priority queue, in addition to the behaviors of a queue having a priority, uses a heap with a comparison method built on a vector or a deque

Rossella Lau Lecture 11, DCO20105, Semester A, Reference  Ford: 8  STL online references   END --