CSI33 Data Structures

Similar documents
CSI33 Data Structures

CHAPTER 5. QUEUE v2 by

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

PA3 Design Specification

Queues. Queue ADT Queue Implementation Priority Queues

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

Outline. Stacks. 1 Chapter 5: Stacks and Queues. favicon. CSI33 Data Structures

CSI33 Data Structures

Programming Abstractions

CSI33 Data Structures

8. Fundamental Data Structures

The Abstract Data Type Queue. Module 8. The Abstract Data Type Queue. The Abstract Data Type Queue. The Abstract Data Type Queue

811312A Data Structures and Algorithms, , Exercise 1 Solutions

1 P age DS & OOPS / UNIT II

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

CSI33 Data Structures

CSI33 Data Structures

CS24 Week 4 Lecture 2

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

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

Another common linear data structure similar to. new items enter at the back, or rear, of the queue. Queue is an ADT with following properties

CS 206 Introduction to Computer Science II

Data structure and algorithm in Python

CSCA48 Term Test 1 Seminar

CSC 110 Lab 9 Operating Systems Algorithms. Names: What might be an advantage of this algorithm? What might be a disadvantage of this algorithm?

Stacks. Revised based on textbook author s notes.

CSC148 Week 2. Larry Zhang

Queues Fall 2018 Margaret Reid-Miller

Abstract Data Types Chapter 1

Data Abstraction and Specification of ADTs

CSI33 Data Structures

Your Topic Goes Here Queue

Data structure and algorithm in Python

File Processing. CS 112: Introduction to Programming: File Processing Sequence. File Processing. File IO

CSI33 Data Structures

CS 331 Midterm Exam 2

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.

csci 210: Data Structures Stacks and Queues

The Stack ADT. Stacks and Queues. Stack: Illustration. What is a Stack? Accessors top size isempty

Python Programming: An Introduction to Computer Science

STACKS AND QUEUES. Problem Solving with Computers-II

Computer Science 302 Spring 2007 Practice Final Examination: Part I

Spring 2011 PROGRAMMING ASSIGNMENT Encrypted Message Program Due Tuesday, May 17th

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

Sequence Abstract Data Type

Stacks and Queues. CSE 373 Data Structures Lecture 6

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

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

CS 206 Introduction to Computer Science II

COSC160: Data Structures: Lists and Queues. Jeremy Bolton, PhD Assistant Teaching Professor

How can we improve this? Queues 6. Topological ordering: given a sequence of. should occur prior to b, provide a schedule. Queues 5.

12 Abstract Data Types

In addition to the correct answer, you MUST show all your work in order to receive full credit.

CS302 - Data Structures using C++

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

Introduction Programming Using Python Lecture 8. Dr. Zhang COSC 1437 Fall 2017 Nov 30, 2017

Chapter 6: Files and Exceptions. COSC 1436, Summer 2016 Dr. Ling Zhang 06/23/2016

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

ITI Introduction to Computing II

CS S-04 Stacks and Queues 1. An Abstract Data Type is a definition of a type based on the operations that can be performed on it.

CSI33 Data Structures

Basic Data Structures (Version 7) Name:

Programming Abstractions

CSI33 Data Structures

Data Structures and Algorithms

Data Structures and Algorithms for Engineers

Priority Queues. Binary Heaps

What Can You Use a Queue For?

Stacks, Queues and Hierarchical Collections

Computer Science Spring 2005 Final Examination, May 12, 2005

Plan for Week. What is a linear structure? Stacks Queues and Deques, Oh My!

Separate Compilation and Namespaces Week Fall. Computer Programming for Engineers

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

How to Win Coding Competitions: Secrets of Champions. Week 2: Computational complexity. Linear data structures Lecture 5: Stack. Queue.

March 13/2003 Jayakanth Srinivasan,

SCHOOL OF COMPUTER AND COMMUNICATION ENGINEERING. EKT224: ALGORITHM AND DATA STRUCTURES (Stack, Queue, Linked list)

Plan for Week. Linear structures: Stack, Queue. Friday: More examples of recursion and alternatives. Guide to using practice-it for APT-like problems

Examination Questions Midterm 1

infix expressions (review)

CSE 143. Lecture 4: Stacks and Queues

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

Advanced Linked Lists. Doubly Linked Lists Circular Linked Lists Multi- Linked Lists

This exam has 10 pages including the title page. Please check to make sure all pages are included.

Simulating a Queue in Python

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

Topic: List processing (with application to files)

.:: UNIT 4 ::. STACK AND QUEUE

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal

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

Sequential Containers Cont'd

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

Munster Programming Training - Cycle 2

Extra Credit: write mystrlen1 as a single function without the second parameter int mystrlen2(char* str)

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

Abstract Data Types. Abstract Data Types

== isn t always equal?

Test #2 October 8, 2015

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE. Sample Final Exam

Outline. Simple Recursive Examples In-Class Work. 1 Chapter 6: Recursion. Recursive Definitions Simple Recursive Examples In-Class Work

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Transcription:

Outline Department of Mathematics and Computer Science Bronx Community College October 2, 2017

Outline Outline 1 Chapter 5: Stacks and

Outline Chapter 5: Stacks and 1 Chapter 5: Stacks and

Chapter 5: Stacks and A Queue ADT A Container Class for First-In-First-Out Access A queue is a list-like container with access restricted to both ends of the list. Items are added at the (the back of the queue) and removed from the front. In real life: waiting in line or taking a number. The enqueue method puts an item at the back of the queue. The dequeue method returns the item at the front, and removes it from the queue. (precondition: queue is not empty size > 0) The front method returns that item (precondition: queue is not empty size > 0) The size method returns the number of items in the queue.

Chapter 5: Stacks and Simple Queue Application: A Palindrome Recognizer A Palindrome Recognizer def ispalindrome(phrase): forward = Queue() reverse = Stack() extractletters(phrase, forward, reverse) return samesequence(forward, reverse)

Chapter 5: Stacks and Simple Queue Application: A Palindrome Recognizer Phase I: Extract Letters def extractletters(phrase, q, s): for ch in phrase: if ch.isalpha(): ch = ch.lower() q.enqueue(ch) s.push(ch)

Chapter 5: Stacks and Simple Queue Application: A Palindrome Recognizer Phase II: Same Sequence def samesequence(q, s): while q.size() > 0: ch1 = q.dequeue() ch2 = s.pop() if ch1!= ch2: return False return True

Chapter 5: Stacks and Queue Implementions A Python List Is Not An Efficient Queue Both ends of the list must be used. The first item is either the front, and dequeue is Θ(n) every item must be moved down to delete the first item, or the back, and enqueue is Θ(n) every item must be moved up to insert the first item.

Chapter 5: Stacks and Queue Implementions Linked List This is the most flexible representation of a Queue ADT. By using a reference to the first node (front) and last node (back), a singly-linked list can perform enqueue and dequeue in constant time (Θ(1)). However, the links take up extra memory space.

Chapter 5: Stacks and Queue Implementions Circular Array A circular array avoids both the space inefficiency of links and the time inefficiency of the Python list (array) representation by not moving items. Instead, the front and back of the queue move, using changing indexes as markers. To enqueue a new item into the queue, the index marking the back of the queue is increased by one. The item that was in the back stays in the same position, but the new item goes behind it, into the new back position. Similarly, when an item is dequeued, the front of the queue is moved to the next item behind it. The array is circular because when either marker goes past the end of the array, it is put back at index zero.

Chapter 5: Stacks and Simulation of Retail Store with One Checkout Register A Typical Queuing Simulation To measure efficiency of service delivery, one runs a program that simulates these events: Random arrival times (customers finish shopping) Waiting for service (grocery checkout register) Random time to be serviced (number of items)

Chapter 5: Stacks and Simulation of Retail Store with One Checkout Register def gentestdata(filename, totalticks, maxitems, arrivalinterval): outfile = open(filename, "w") # step through the ticks for t in range(1,totalticks): if random() < 1./arrivalInterval: # a customer arrives this tick # with a random number of items items = randrange(1, maxitems+1) outfile.write("%d %d\n" % (t, items)) outfile.close()

Chapter 5: Stacks and Simulation of Retail Store with One Checkout Register class Customer(object): def init (self, arrivaltime, itemcount): self.arrivaltime = int(arrivaltime) self.itemcount = int(itemcount) def repr (self): return ("Customer(arrivalTime=%d, itemcount=%d)" % (self.arrivaltime, self.itemcount))

Chapter 5: Stacks and Simulation of Retail Store with One Checkout Register def createarrivalqueue(fname): q = Queue() infile = open(fname) for line in infile: time, items = line.split() q.enqueue(customer(time,items)) infile.close() return q

Chapter 5: Stacks and Simulation of Retail Store with One Checkout Register class CheckerSim(object): def init (self, arrivalqueue, avgtime): self.time = 0 self.arrivals = arrivalqueue self.line = Queue() self.servicetime = 0 self.totalwait = 0 self.maxwait = 0 self.customercount = 0 self.maxlength = 0 self.ticksperitem = avgtime

Chapter 5: Stacks and Simulation of Retail Store with One Checkout Register def run(self): while (self.arrivals.size() > 0 or self.line.size() > 0 or self.servicetime > 0): self.clocktick() def averagewait(self): float(self.totalwait) / self.customercount def maximumwait(self): return self.maxwait def maximumlinelength(self): return self.maxlength

Chapter 5: Stacks and Simulation of Retail Store with One Checkout Register def clocktick(self): self.time += 1 while (self.arrivals.size() > 0 and self.arrivals.front().arrivaltime == self.time): self.line.enqueue(self.arrivals.dequeue()) self.customercount += 1 self.maxlength = max(self.maxlength, self.line.size()) if self.servicetime > 0: self.servicetime -= 1 elif self.line.size() > 0: customer = self.line.dequeue() self.servicetime = customer.itemcount * self.ticksperitem waittime = self.time - customer.arrivaltime self.totalwait += waittime self.maxwait = max(self.maxwait, waittime)