Fundamentals of Python: From First Programs Through Data Structures

Slides:



Advertisements
Similar presentations
Computer Science 112 Fundamentals of Programming II Queues and Priority Queues.
Advertisements

Queues.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 4.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
CSC 231 Queues 1 Devon M. Simmonds University of North Carolina, Wilmington TIME: Tuesday/Thursday 11:11:50am in 1012 & Thursday 3:30-5:10pm in Office.
DATA STRUCTURE & ALGORITHMS
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Unit : Overview of Queues.
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Comp 245 Data Structures Queues. Introduction to the Queue ADT It is a FIFO (first-in, first-out) structure Access to the Queue can take place at two.
Queues and Priority Queues
Chapter 13 Queues and Priority Queues CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Queues Chapter 6. Chapter Objectives  To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface for insertion.
ITEC200 Week06 Queues. 2 Learning Objectives – Week06 Queues (Ch6) Students can Manage data using the queue Abstract Data Type.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
CS 206 Introduction to Computer Science II 10 / 20 / 2008 Instructor: Michael Eckmann.
Cmpt-225 Queues. A queue is a data structure that only allows items to be inserted at the end and removed from the front Queues are FIFO (First In First.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Queues Chapter 6. Chapter 6: Queues2 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface.
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Queues What is a Queue? Queue Implementations: Queue As Array
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
Queues and Priority Queues
Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract.
COMP 121 Week 14: Queues. Objectives Learn how to represent a queue Learn how to use the methods in the Queue interface Understand how to implement the.
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
CHP-4 QUEUE.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues.
Computer Science 112 Fundamentals of Programming II Modeling and Simulation.
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
Data structures Abstract data types Java classes for Data structures and ADTs.
Data Structures Using Java1 Chapter 7 Queues. Data Structures Using Java2 Chapter Objectives Learn about queues Examine various queue operations Learn.
Stacks And Queues Chapter 18.
The Abstract Data Type Queue A queue New items enter at the back, or rear, of the queue Items leave from the front of the queue First-in, first-out (FIFO)
Fundamentals of Python: From First Programs Through Data Structures Chapter 13 Collections, Arrays, and Linked Structures.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
© 2011 Pearson Addison-Wesley. All rights reserved 8 B-1 Chapter 8 (continued) Queues.
Chapter 7 Queues Introduction Queue applications Implementations.
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.
Introduction Of Queue. Introduction A queue is a non-primitive linear data structure. It is an homogeneous collection of elements in which new elements.
 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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Queues Chapter 8 (continued)
Review Array Array Elements Accessing array elements
Data Abstraction & Problem Solving with C++
Data Structures Using C++ 2E
Queues.
CC 215 Data Structures Queue ADT
Program based on queue & their operations for an application
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Chapter 13 Queues and Priority Queues
CMSC 341 Lecture 5 Stacks, Queues
Queues.
Queue and Priority Queue Implementations
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
Queues.
Presentation transcript:

Fundamentals of Python: From First Programs Through Data Structures Chapter 15 Linear Collections: Queues

Objectives After completing this chapter, you will be able to: Describe the behavior of a queue from a user’s perspective Explain how a queue can be used to support a simulation Describe the use of a queue in scheduling processes for computational resources Fundamentals of Python: From First Programs Through Data Structures

Objectives (continued) Explain the difference between a queue and a priority queue Describe a case where a queue would be used rather than a priority queue Analyze the performance trade-offs between an array-based implementation of a queue and a linked implementation of a queue Fundamentals of Python: From First Programs Through Data Structures

Overview of Queues Fundamentals of Python: From First Programs Through Data Structures

Overview of Queues (continued) Insertions are restricted to one end (rear) Removals are restricted to one end (front) Queues supports a first-in first-out (FIFO) protocol Fundamental operations: enqueue and dequeue Item dequeued, or served next, is always the item that has been waiting the longest Priority queue: Higher-priority items are dequeued first; equal priority items dequeued in FIFO order Most queues in CS involve scheduling access to shared resources: CPU/Disk/Printer access Fundamentals of Python: From First Programs Through Data Structures

The Queue Interface and Its Use You can use a Python list to emulate a queue Use append to add an element to rear of queue Use pop to remove an element from front of queue Drawback: Queue can be manipulated by all of the other list operations as well Violate spirit of queue as ADT We define a more restricted interface, or set of operations, for any queue implementation and show how these operations are used Fundamentals of Python: From First Programs Through Data Structures

The Queue Interface and Its Use (continued) Fundamentals of Python: From First Programs Through Data Structures

The Queue Interface and Its Use (continued) Assume that any queue class that implements this interface will also have a constructor that allows its user to create a new queue instance Later, we’ll consider two different implementations: ArrayQueue and LinkedQueue q1 = ArrayQueue() q2 = LinkedQueue() Fundamentals of Python: From First Programs Through Data Structures

Two Applications of Queues We now look briefly at two applications of queues: One involving computer simulations The other involving round-robin CPU scheduling Fundamentals of Python: From First Programs Through Data Structures

Simulations Computer simulations are used to study behavior of real-world systems, especially if it is impractical or dangerous to experiment with these systems directly Example: Mimic traffic flow on a busy highway Another example: Manager of supermarket wants to determine number of checkout cashiers to schedule at various times of the day and must consider: Frequency with which new customers arrive Number of checkout cashiers available Number of items in a customer’s shopping cart Period of time considered Fundamentals of Python: From First Programs Through Data Structures

Simulations (continued) Simulations avoid need for formulas by imitating actual situation and collecting pertinent statistics Simple technique to mimic variability: Suppose new customers are expected to arrive on average once every four minutes During each minute of simulated time, a program can generate a random number between 0 and 1 If number is less than 1/4, program adds a new customer to a checkout line; otherwise, it does not Probability distribution functions produce more realistic results Fundamentals of Python: From First Programs Through Data Structures

Simulations (continued) Examples presented involve service providers and service consumers We associate each service provider with a queue of service consumers Simulations operate by manipulating these queues At each tick of an imaginary clock, add consumer(s) to the queues and give consumers at the head of each queue another unit of service OO methods can be used to implement simulations Fundamentals of Python: From First Programs Through Data Structures

Simulations (continued) Example: Supermarket simulation A Customer object keeps track of when the customer starts standing in line, when service is first received, and how much service is required A Cashier object has a queue of customer objects A simulator object coordinates customer/cashier activities by doing the following at each clock tick: Generates new customer objects as appropriate Assigns customers to cashiers Tells each cashier to provide one unit of service to the customer at the head of the queue Fundamentals of Python: From First Programs Through Data Structures

Round-Robin CPU Scheduling Each process on the ready queue is dequeued in turn and given a slice of CPU time Improvement: Can use a priority queue Fundamentals of Python: From First Programs Through Data Structures

Implementations of Queues Our approach to the implementation of queues is similar to the one we used for stacks The structure of a queue lends itself to either an array implementation or a linked implementation The linked implementation is somewhat more straight-forward Fundamentals of Python: From First Programs Through Data Structures

A Linked Implementation enqueue adds a node at the end For fast access to both ends of a queue’s linked structure, provide external pointers to both ends Instance variables front and rear of LinkedQueue are given an initial value of None size tracks number of elements currently in queue Fundamentals of Python: From First Programs Through Data Structures

A Linked Implementation (continued) Fundamentals of Python: From First Programs Through Data Structures

An Array Implementation Array implementations of stacks and queues have less in common than the linked implementations Array implementation of a queue must access items at the logical beginning and the logical end Doing this in computationally effective manner is complex We approach problem in a sequence of three attempts Fundamentals of Python: From First Programs Through Data Structures

A First Attempt Fixes front of queue at position 0 rear variable points to last item at position n – 1 n is the number of items in queue Analysis: enqueue is efficient dequeue entails shifting all but first item  O(n) Fundamentals of Python: From First Programs Through Data Structures

A Second Attempt Maintain a second index (front) that points to item at front of queue Starts at 0 and advances as items are dequeued Analysis: dequeue is O(1) Maximum running time of enqueue is O(n) Fundamentals of Python: From First Programs Through Data Structures

A Third Attempt Use a circular array implementation rear starts at –1; front starts at 0 front chases rear pointer through the array When a pointer is about to run off the end of the array, it is reset to 0 Running times of enqueue and dequeue are O(1) Fundamentals of Python: From First Programs Through Data Structures

A Third Attempt (continued) What happens when the queue becomes full? Maintain a count of the items in the queue When this count equals the size of the array: Resize After resizing, we would like queue to occupy initial segment of array, with front pointer set to 0 If front pointer is less than rear pointer Copy positions 0 through size-1 in new array If rear pointer is less than front pointer Copy positions 0 through size-front and size – front + 1 through size-1 in new array Resizing process is linear Fundamentals of Python: From First Programs Through Data Structures

Time and Space Analysis for the Two Implementations Linked implementation: Running time: __str__ is O(n); all other are O(1) Space requirement: 2n + 3, n: size of queue Circular array implementation: Static array: Maximum running time of all methods other than __str__ is O(1) Dynamic array: enqueue/dequeue are O(n) when array is resized, but are O(1) on average Space utilization: For load factors above 1⁄2, array implementation makes more efficient use of memory than a linked implementation Fundamentals of Python: From First Programs Through Data Structures

Case Study: Simulating a Supermarket Checkout Line Request: Write program that allows user to predict behavior of supermarket checkout line under various conditions Analysis: Fundamentals of Python: From First Programs Through Data Structures

Case Study: Simulating a Supermarket Checkout Line (continued) The Interface: Classes and responsibilities: We divide the system into a main function and several model classes Fundamentals of Python: From First Programs Through Data Structures

Case Study: Simulating a Supermarket Checkout Line (continued) Fundamentals of Python: From First Programs Through Data Structures

Case Study: Simulating a Supermarket Checkout Line (continued) Fundamentals of Python: From First Programs Through Data Structures

Case Study: Simulating a Supermarket Checkout Line (continued) Fundamentals of Python: From First Programs Through Data Structures

Priority Queues A priority queue is a specialized type of queue Items added to queue are assigned an order of rank Items of higher priority are removed before those of lower priority Items of equal priority are removed in FIFO order Item A has a higher priority than item B if A < B Objects that recognize the comparison operators can be ordered in priority queues If not, object can be wrapped with a priority number in another object that does recognize these operators Fundamentals of Python: From First Programs Through Data Structures

Priority Queues (continued) Fundamentals of Python: From First Programs Through Data Structures

Priority Queues (continued) Wrapper class used to build a comparable item from one that is not already comparable: Fundamentals of Python: From First Programs Through Data Structures

Priority Queues (continued) During insertions, a priority queue does not know whether it is comparing items in wrappers or just items When a wrapped item is accessed (e.g., with peek or dequeue), it must be unwrapped with the method getItem before processing Two implementations: Sorted linked list or heap Fundamentals of Python: From First Programs Through Data Structures

Priority Queues (continued) Search is linear, so enqueue is now O(n) Fundamentals of Python: From First Programs Through Data Structures

Case Study: An Emergency Room Scheduler Request: Write a program that allows a supervisor to schedule treatments for patients coming into emergency room Patients are assigned a priority when admitted Higher priority patients receive attention first Analysis: Patient priorities: critical, serious, and fair Fundamentals of Python: From First Programs Through Data Structures

Case Study: An Emergency Room Scheduler (continued) Fundamentals of Python: From First Programs Through Data Structures

Case Study: An Emergency Room Scheduler (continued) Fundamentals of Python: From First Programs Through Data Structures

Case Study: An Emergency Room Scheduler (continued) Classes: Design and Implementation: Patient and Condition classes maintain a patient’s name and condition Patients can be compared (according to their conditions) and viewed as strings Fundamentals of Python: From First Programs Through Data Structures

Case Study: An Emergency Room Scheduler (continued) Fundamentals of Python: From First Programs Through Data Structures

Summary A queue is a linear collection that adds elements to the “rear” and removes them from the “front” Queues are used in applications that manage data items in a first-in, first-out order (FIFO) Example: Scheduling items for processing or access to resources Arrays and singly linked structures support simple implementations of queues Priority queues schedule their elements using a rating scheme as well as a FIFO order Fundamentals of Python: From First Programs Through Data Structures