Queues. Queue ADT Queue Implementation Priority Queues

Similar documents
Priority Queue ADT. Revised based on textbook author s notes.

Heaps. Heaps Priority Queue Revisit HeapSort

Queues. ADT description Implementations. October 03, 2017 Cinda Heeren / Geoffrey Tien 1

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Linked Lists. Linked List vs Array vs Python List Singly Linked List Bag ADT Revisited Building Linked Lists

1. Introduction. Lecture Content

What is an algorithm?

Queue ADT. January 31, 2018 Cinda Heeren / Geoffrey Tien 1

Data structure and algorithm in Python

Stack and Queue. Stack:

1 P age DS & OOPS / UNIT II

CSc 120. Introduction to Computer Programming II. 14: Stacks and Queues. Adapted from slides by Dr. Saumya Debray

Queues. Gaddis 18.4, Molly A. O'Neil CS 2308 :: Spring 2016

CSI33 Data Structures

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal

Where does the insert method place the new entry in the array? Assume array indexing starts from 0(zero).

CSCA48 Summer 2018 Week 3: Priority Queue, Linked Lists. Marzieh Ahmadzadeh University of Toronto Scarborough

THE UNIVERSITY OF WESTERN AUSTRALIA

Data structure and algorithm in Python

! A data type for which: ! An ADT may be implemented using various. ! Examples:

Queues The Queue ADT. The Queue ADT is defined by the following operations: init Initialize a new empty queue.

Agenda & Reading. COMPSCI 105 SS 2015 Principles of Computer Science

ITI Introduction to Computing II

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

CSCA48 Winter 2018 Week 3: Priority Queue, Linked Lists. Marzieh Ahmadzadeh, Nick Cheng University of Toronto Scarborough

Assignment 1. Check your mailbox on Thursday! Grade and feedback published by tomorrow.

csci 210: Data Structures Stacks and Queues

CS-141 Exam 2 Review November 10, 2017 Presented by the RIT Computer Science Community

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

CSCA48 Term Test 1 Seminar

Your Topic Goes Here Queue

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

Queue Definition. Ordered list with property:

Ashish Gupta, Data JUET, Guna

CSCI 204 Introduction to Computer Science II Lab 8 Priority Queue ADT

COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.)

CSC 1052 Algorithms & Data Structures II: Queues

Name CPTR246 Spring '17 (100 total points) Exam 3

STACKS AND QUEUES. Problem Solving with Computers-II

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. Week 9. elements of the same type.

Queues Fall 2018 Margaret Reid-Miller

Queues. CITS2200 Data Structures and Algorithms. Topic 5

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18

CS302 - Data Structures using C++

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

CSEN 301 Data Structures and Algorithms

Name: After line 1: After line 3: After line 5: After line 8: After line 11:

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

CMSC 132: Object-Oriented Programming II. Stack and Queue

Linked Structures. See Section 3.2 of the text.

CSC148 Week 2. Larry Zhang

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

Queues. Lesson 4. CS 32: Data Structures Dept. of Computer Science

INFO1x05 Tutorial 6. Exercise 1: Heaps and Priority Queues

More Data Structures (Part 1) Stacks

(8 1) Container Classes & Class Templates D & D Chapter 18. Instructor - Andrew S. O Fallon CptS 122 (October 8, 2018) Washington State University

03/29/2004. A dispenser has three essential features: adding, removing, accessing. Dispensers

Linked Structures Chapter John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

What Can You Use a Queue For?

Stacks and Queues III

Name: 1 stack<int> s; 2 s.push(9) 3 s.push(5) 4 s.push(10) 5 s.push(1) 6 s.pop() 7 s.pop() 8 s.push(0) 9 s.pop() 10 s.pop() 11 s.

Linked List. April 2, 2007 Programming and Data Structure 1

Sequence Abstract Data Type

Chapter 8 : Computer Science. Datastructures: Class XII ( As per CBSE Board) lists, stacks, queues. Visit : python.mykvs.in for regular updates

Queues. October 20, 2017 Hassan Khosravi / Geoffrey Tien 1

8. Fundamental Data Structures

SCJ2013 Data Structure & Algorithms. Queue. Nor Bahiah Hj Ahmad & Dayang Norhayati A. Jawawi

CSI33 Data Structures

Points off Total off Net Score. CS 314 Final Exam Spring 2016

811312A Data Structures and Algorithms, , Exercise 1 Solutions

CS 231 Data Structures and Algorithms Fall DDL & Queue Lecture 20 October 22, Prof. Zadia Codabux

COMPSCI 105 S Principles of Computer Science. 17 Linked List(1)

CSC212:Data Structure

16. Dynamic Data Structures

data structures and algorithms lecture 6

Stacks and Queues. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

LIFO : Last In First Out


CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

CS 206 Introduction to Computer Science II

Walk through previous lectures

Data Structure. Recitation VII

Priority Queues. Binary Heaps

CSE 214 Computer Science II Heaps and Priority Queues

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

Programming. Lists, Stacks, Queues

Computer Science Fundamentals 107

Algorithms and Data Structures

CSC148H Week 3. Sadia Sharmin. May 24, /20

CS200: Queues. Prichard Ch. 8. CS200 - Queues 1

An Introduction to Queues With Examples in C++

Queue with Array Implementation

Programming II (CS300)

Queues. Virtuelle Fachhochschule. Prof. Dr. Debora Weber-Wulff

Queues 4/11/18. Many of these slides based on ones by Cynthia Lee

Abstract Data Types Chapter 1

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING E&CE 250 ALGORITHMS AND DATA STRUCTURES

Circular Queue can be created in three ways they are: Using single linked list Using double linked list Using arrays

Problem 1: Building and testing your own linked indexed list

push(d), push(h), pop(), push(f), push(s), pop(), pop(), push(m).

Transcription:

Queues Queue ADT Queue Implementation Priority Queues

Queue A restricted access container that stores a linear collection. Very common for solving problems in computer science that require data to be processed in the order in which it was received. Provides a first- in first- out (FIFO) protocol. New items are added at the back while existing items are removed from the front of the queue.

The Queue ADT A queue stores a linear collection of items with access limited to a first- in first- out order. New items are added to the back. Existing items are removed from the front. Queue() isempty() length() enqueue( item ) dequeue()

Queue Example The following code creates the queue from earlier. Q = Queue() Q.enqueue( 28 ) Q.enqueue( 19 ) Q.enqueue( 45 ) Q.enqueue( 13 ) Q.enqueue( 7 )

Queue Example We can remove items from the queue and add more items. x = Q.dequeue() Q.enqueue( 21 ) Q.enqueue( 74 )

Queue Implementation Several common ways to implement a stack: Python list easiest to implement Circular array fast operations with a fixed size queue. Linked list reduces memory wastes by eliminating the extra capacity created with a vector.

Queue: Python List How is the data organized within the Python list? Add new items to the end of the list. Remove items from the front of the list.

Queue: Python List pylistqueue.py # Implementation of the Queue ADT using a Python list. class Queue : def init ( self ): self._qlist = list() def isempty( self ): return len( self ) == 0 def len ( self ): return len( self._qlist ) def enqueue( self, item ): self._qlist.append( item ) def dequeue( self ): assert not self.isempty(), "Cannot dequeue from an empty queue." return self._qlist.pop( 0 )

Queue Analysis: Python List Queue Operation q = Queue() len(q) q.isempty() q.enqueue(x) x = q.dequeue() Worst Case O(1) O(1) O(1) O(n) O(n)

Queue: Circular Array circular array an array viewed as a circle instead of a line. Items can be added/removed without having to shift the remaining items in the process. Introduces the concept of a maximum- capacity queue that can become full.

Queue: Circular Array How should the data be organized within the array? count field number of items in the queue. front and back markers indicate the array elements containing the queue items.

Queue: Circular Array To enqueue an item: new item is inserted at the position following back back is advanced by one position countis incremented by one. Suppose we enqueue 32:

Queue: Circular Array To dequeue an item: the value in the front position is saved frontis advanced by one position. countis decremented by one. Suppose we dequeue an item:

Queue: Circular Array Suppose we enqueue items 8 and 23:

Queue: Circular Array What happens if we enqueue 39? Since we are using a circular array, the same steps are followed. But since back is at the end of the array, it wraps around to the front.

Queue: Circular Array arrayqueue.py class Queue : def init ( self, maxsize ) : self._count = 0 self._front = 0 self._back = maxsize - 1 self._qarray = Array( maxsize ) def isempty( self ) : return self._count == 0 # A new operation specifically for the circular array. def isfull( self ) : return self._count == len(self._qarray) def len ( self ) : return self._count #...

Queue: Circular Array arrayqueue.py class Queue : #... def enqueue( self, item ): assert not self.isfull(), "Cannot enqueue to a full queue." maxsize = len(self._qarray) self._back = (self._back + 1) % maxsize self._qarray[self._back] = item self._count += 1 def dequeue( self ): assert not self.isempty(), "Cannot dequeue from an empty queue." item = self._qarray[ self._front ] maxsize = len(self._qarray) self._front = (self._front + 1) % maxsize self._count -= 1 return item

Queue Analysis: Circular Array Queue Operation q = Queue() len(q) q.isempty() q.enqueue(x) x = q.dequeue() Worst Case O(1) O(1) O(1) O(1) O(1)

Queue: Linked List How should the data be organized? Use both head and tail references. Let the head of the list represent the front of the queue and the tail the back.

Queue: Linked List # Implementation of the Queue ADT using a linked list. class Queue : def init ( self ): self._qhead = None self._qtail = None self._count = 0 llistqueue.py def isempty( self ): return self._qhead is None def len ( self ): return self._count #... # Private storage class for creating the linked list nodes. class _QueueNode: def init ( self, item ): self.item = item self.next = None

Queue: Linked List class Queue : #... def enqueue( self, item ): node = _QueueNode( item ) if self.isempty() : self._qhead = node else : self._qtail.next = node llistqueue.py self._qtail = node self._count += 1 def dequeue( self ): assert not self.isempty(), "Cannot dequeue from an empty queue." node = self._qhead if self._qhead is self._qtail : self._qtail = None self._qhead = self._qhead.next self._count -= 1 return node.item

Queue Analysis: Linked List Queue Operation q = Queue() len(q) q.isempty() q.enqueue(x) x = q.dequeue() Worst Case O(1) O(1) O(1) O(1) O(1)

Priority Queues Some applications require the use of a queue in which items are assigned a priority. higher priority items are dequeued first. items with equal priority still follow FIFO. Two types: bounded limited range of priorities. unbounded unlimited range.

The Priority Queue ADT A priority queue is a queue in which each item is assigned a priority and items with a higher priority are removed before those with lower priority. Integer values are used for the priorities. Smaller integers have a higher priority. PriorityQueue() BpriorityQueue( numlevels ) isempty() length() enqueue( item, priority ) dequeue()

Priority Queue Example Consider the following code segment: Q = BpriorityQueue( 6 ) Q.enqueue( purple, 5 ) Q.enqueue( black, 1 ) Q.enqueue( orange, 3 ) Q.enqueue( white, 0 ) Q.enqueue( green, 1 ) Q.enqueue( yellow, 5 )

Priority Queue Implementation How should the ADT be implemented. We must consider: A priority must be associated with each item in the queue. The next item dequeued is the item with the highest priority. If multiple items have the same priority, those must be dequeued in a FIFO order.

Unbounded Priority Queue We explore two approaches: Python list Linked list

Unbounded Priority Q: Python List We can use a Python list to implement the unbounded Priority Queue ADT. How do we associate a priority with each item? class PriorityQEntry : def init ( self, item, priority ): self.item = item self.priority = priority How should the entries be organized? append new items to the end, or keep the items in sorted order based on priority.

Unbounded Priority Q: Python List Sample instance for the earlier priority queue.

Unbounded Priority Q: Python List priorityq.py # Implementation of the unbounded Priority Queue ADT using a Python # list with new items appended to the end. class PriorityQueue : def init ( self ): self._qlist = list() def isempty( self ): return len( self ) == 0 def len ( self ): return len( self._qlist ) #...

Unbounded Priority Q: Python List priorityq.py class PriorityQueue : #... def enqueue( self, item, priority ): entry = _PriorityQEntry( item, priority ) self._qlist.append( entry ) def dequeue( self ) : assert not self.isempty(), "Cannot dequeue from an empty queue." # Find the entry with the highest priority. highest = self._qlist[0].priority high_index = 0 for i in range( self.len() ) : if self._qlist[i].priority < highest : highest = self._qlist[i].priority high_index = i # Remove the entry with the highest priority and return the item. entry = self._qlist.pop( high_index ) return entry.item

Unbounded Priority Q: Linked List We can use a singly linked list: Head and tail references. Append new entries to the end.

Priority Queue Analysis The worst case analysis for the two implementations. Queue Operation Python List Linked List q = PriorityQueue() O(1) O(1) len(q) O(1) O(1) q.isempty() O(1) O(1) q.enqueue(x) O(n) O(1) x = q.dequeue() O(n) O(n)

Bounded Priority Queue We can use the Python list or linked list to implement the bounded Priority Queue. Both require linear time to dequeue an item. We can improve this time using an array of queues.

Bounded Priority Q Implementation bpriorityq.py from array import Array from llistqueue import Queue class BPriorityQueue : def init ( self, numlevels ): self._qsize = 0 self._qlevels = Array( numlevels ) for i in range( numlevels ) : self._qlevels[i] = Queue() def isempty( self ): return len( self ) == 0 def len ( self ): return self._qsize #...

Bounded Priority Q Implementation bpriorityq.py class BPriorityQueue : #... def enqueue( self, item, priority ): assert priority >= 0 and priority < len(self._qlevels), \ "Invalid priority level." self._qlevels[priority].enqueue( item ) self._qsize += 1 def dequeue( self ) : # Make sure the queue is not empty. assert not self.isempty(), "Cannot dequeue from an empty queue." # Find the first non-empty queue. i = 0 p = len(self._qlevels) while i < p and self._qlevels[i].isempty() : i += 1 self._qsize -= 1 # We know the queue is not empty, so dequeue from the ith queue. return self._qlevels[i].dequeue()