Lecture 13 – Queues.  Ordered collection of data  Items are added to the back of the queue  Items are removed from the front of the queue  Remove.

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
COMPSCI 105 S Principles of Computer Science 13 Stacks.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
CHAPTER 7 Queues.
Queues CS 3358 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: – Elements are added.
Fundamentals of Python: From First Programs Through Data Structures
COMPSCI 105 SS 2015 Principles of Computer Science Queues.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
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.
CS 206 Introduction to Computer Science II 10 / 22 / 2008 Instructor: Michael Eckmann.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
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.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
1 Queues Queue Concept Queue Design Considerations Queues in Java Collections APIs Queue Applications Reading L&C , 9.3.
CS 206 Introduction to Computer Science II 10 / 20 / 2008 Instructor: Michael Eckmann.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
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.
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.
TCSS 342, Winter 2005 Lecture Notes
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed.
Chapter 6.6, (event-driven simulation) Queues 1CSCI 3333 Data Structures.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Lecture 14 - JSON.  Text-based notation for data interchange  Human readable  Object  Unordered set of name-value pairs  { name1 : value1, name2.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
Stacks and Queues Introduction to Computing Science and Programming I.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
CSC 205 Programming II Lecture 22 Carwash Simulation.
Queues Chapter 6. Chapter 6: Queues Chapter Objectives To learn how to represent a waiting line (queue) and how to use the five methods in the Queue interface:
Stacks And Queues Chapter 18.
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.
COMPSCI 105 SS 2015 Principles of Computer Science 09 ADT & Stacks.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Queues 1. Queue  a queue represents a sequence of elements where elements can be added at the back of the sequence and removed from the front of the.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
Chapter 7 A Queues. © 2004 Pearson Addison-Wesley. All rights reserved7 A-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear,
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
CSCE 210 Data Structures and Algorithms
Lecture 7 Queues Stacks Trees.
Queues.
Queues Rem Collier Room A1.02
Csc 2720 Data structure Instructor: Zhuojun Duan
COMPSCI 107 Computer Science Fundamentals
CSE 373: Data Structures and Algorithms
Stacks and Queues.
COMPSCI 107 Computer Science Fundamentals
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Building Java Programs
CMSC 341 Lecture 5 Stacks, Queues
Queues, Deques and Priority Queues
Queues 11/9/2018 6:32 PM Queues.
Queues, Deques and Priority Queues
Queue.
Linear Data Structures Chapter 3
Revised based on textbook author’s notes.
CSC 143 Queues [Chapter 7].
Node Applications in Abstract Data Types
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Chapter 8 Queues © 2006 Pearson Addison-Wesley. All rights reserved.
Queue.
Stacks, Queues, and Deques
CSCS-200 Data Structure and Algorithms
Queues Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Queues.
Presentation transcript:

Lecture 13 – Queues

 Ordered collection of data  Items are added to the back of the queue  Items are removed from the front of the queue  Remove data in the same order as the data added  First in, first out (FIFO)  Operations  Enqueue  Dequeue  Peek  Is_empty  Size 2COMPSCI Computer Science Fundamentals

 Implementation using Python list  What is the big-O of enqueue()?  What is the big-O of dequeue()? 3COMPSCI Computer Science Fundamentals class QueueV1: def __init__(self): self.items = [] def is_empty(self): return self.items == [] def enqueue(self, item): self.items.insert(0,item) def dequeue(self): return self.items.pop() def size(self): return len(self.items)

 Problem: Can the current printer handle the task load if it were set to print with a better quality but slower page rate?  Write a simulation which models the printing tasks as random events of various lengths and arrival times.  The current setting on the printer is 10 pages per minute and gives lower quality printing. The output will tell us if the wait times are significantly different if we use the better quality setting on the printer (5 pages per minute).  On average, a job is sent to the printer every 3 minutes. Each job is between 1 and 20 pages. 4COMPSCI Computer Science Fundamentals

 Run ten simulations each for 3600 (simulated) seconds. Each second there is 1 in 180 chance that a new print job of 1 to 20 pages is sent to the printer. 5COMPSCI Computer Science Fundamentals simulation(10) 1. Average Wait: secs. 0 tasks queued 2. Average Wait: 2.72 secs. 0 tasks queued 3. Average Wait: secs. 1 tasks queued 4. Average Wait: 3.45 secs. 0 tasks queued 5. Average Wait: 5.60 secs. 0 tasks queued 6. Average Wait: secs. 0 tasks queued 7. Average Wait: secs. 0 tasks queued 8. Average Wait: secs. 0 tasks queued 9. Average Wait: 4.28 secs. 0 tasks queued 10. Average Wait: 5.79 secs. 0 tasks queued

 Run 10 simulations of an hour of printing  simulate_hour function returns a tuple consisting of:  list containing the number of seconds that each print job waited in the queue, and  an integer representing the number of jobs remaining in the queue 6COMPSCI Computer Science Fundamentals def simulation(ppm): for n in range(1, 11): wait_times, jobs_queued = simulate_hour(ppm) average_wait = sum(wait_times) / len(wait_times) print('{:2}. Average Wait: {:6.2f} secs. {} tasks queued'.format( n, average_wait, jobs_queued))

7COMPSCI Computer Science Fundamentals import random def simulate_hour(pages_per_minute): spooler = queue() pages_to_print = 0 wait_times = [] for current_time in range(3600): if random.randint(1, 180) == 1: #add a new job to the print queue pages = random.randint(1, 20) spooler.enqueue((pages, current_time)) if pages_to_print <= 0 and not spooler.is_empty(): #start printing a new job pages_to_print, start_time = spooler.dequeue() wait_times += [current_time - start_time] pages_to_print -= pages_per_minute / 60#pages printed each second return (wait_times, spooler.size())

One possible implementation of the ADT Queue is given here. Is it possible to make a more efficient implementation? Discuss in groups how you might improve the implementation of the Queue ADT in Python. 8COMPSCI Computer Science Fundamentals class QueueV1: def __init__(self): self.items = [] def is_empty(self): return self.items == [] def enqueue(self, item): self.items.insert(0,item) def dequeue(self): return self.items.pop() def size(self): return len(self.items)

 A Python list is used to store the data  Create an initially empty list of a given size  Treat the list as if it were circular  Keep index of the front of the queue  Keep index of the back of the queue  Enqueue and Dequeue  Items are enqueued at the back  Items are dequeued at the front 9COMPSCI Computer Science Fundamentals

 Write the implementation for the Circular Queue. A circular queue is created by passing the constructor an initial capacity 10COMPSCI Computer Science Fundamentals q = circular_queue( 1000 ) class circular_queue: def __init__(self, capacity): #creates empty list, count, front, back def is_empty(self): def enqueue(self, item): def dequeue(self): def size():

 What is the big-O running time for each of the circular_queue methods? 11COMPSCI Computer Science Fundamentals class circular_queue: def __init__(self, capacity): #creates empty list, count, front, back def is_empty(self): def enqueue(self, item): def dequeue(self): def size():

 How does a user know if the circular_queue is full? What should happen when the circular_queue is full? Discuss 12COMPSCI Computer Science Fundamentals class circular_queue: def __init__(self, capacity): #creates empty list, count, front, back def is_empty(self): def enqueue(self, item): def dequeue(self): def size():

 A Double-Ended Queue or Deque (pronounced ‘Deck’)  An ordered collection of items where items are added and removed from either end, either front or back  add_front()  add_rear()  remove_front()  remove_rear()  is_empty()  size() 13COMPSCI Computer Science Fundamentals

 Use a double ended queue to write a function that determines if a string is a palindrome.  A palindrome is a sentence in which the letters appear in the same order forwards and reverse. Punctuation is ignored. 14COMPSCI Computer Science Fundamentals >>> is_palindrome(‘bob’) True

15COMPSCI Computer Science Fundamentals I, man, am regal - a German am I Never odd or even If I had a hi-fi Madam, I'm Adam Too hot to hoot No lemons, no melon Too bad I hid a boot Lisa Bonet ate no basil Warsaw was raw Was it a car or a cat I saw? Rise to vote, sir Do geese see god? "Do nine men interpret?" "Nine men," I nod Rats live on no evil star Won't lovers revolt now? Race fast, safe car Pa's a sap Ma is as selfless as I am May a moody baby doom a yam? Ah, Satan sees Natasha No devil lived on Lonely Tylenol Not a banana baton No "x" in "Nixon" O, stone, be not so O Geronimo, no minor ego "Naomi," I moan "A Toyota's a Toyota" A dog, a panic in a pagoda Oh no! Don Ho! Nurse, I spy gypsies - run! Senile felines Now I see bees I won UFO tofu We panic in a pew Oozy rat in a sanitary zoo God! A red nugget! A fat egg under a dog! Go hang a salami, I'm a lasagna hog