Queues Everywhere - PowerPoint PPT Presentation

1 / 66
About This Presentation
Title:

Queues Everywhere

Description:

Has First-In-First-Out (FIFO) behavior; Items added at one end and removed from the other end ... Recognizing palindromes. Shared resource usage (CPU, memory access, ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 67
Provided by: martha69
Category:

less

Transcript and Presenter's Notes

Title: Queues Everywhere


1
Queues Everywhere!
2
Queues
  • Queue
  • Position-oriented ADTs
  • Linear, homogeneous structure
  • Has First-In-First-Out (FIFO) behavior
  • Items added at one end and removed from the other
    end
  • Middle elements are logically inaccessible
  • Important in simulation analyzing the behavior
    of complex systems

3
Queue Applications
  • Real-World Applications
  • Buy a movie ticket
  • Check out at a bookstore
  • Bank / ATM
  • Call an airline
  • Cashier lines in any store
  •  Computer Science Applications
  • OS task scheduling
  • Print lines of a document
  • Printer shared between computers
  • Convert digit strings to decimal
  • Recognizing palindromes
  • Shared resource usage (CPU, memory access, )

4
ADT Queue
  • Specification
  • Elements
  • Homogeneous
  • Linear
  • Structure
  • Elements added to rear removed from front
  • Items
  • Item type provided by the user
  • Maximum number of items provided either by the
    user or by the capacity of available memory

5
ADT Queue
  • Specification
  • Operations
  • Create an empty queue
  • Create a copy of a queue
  • Destroy a queue
  • Check if a queue is empty / Full
  • Enqueue (Enq, Enque, Add, Insert) new element to
    the back (rear)
  • Dequeue (Deq, Deque, Remove, Serve, Delete) an
    element from the front
  • Retrieve an element from a queue
  • Like the stack, if we need to modify an item in
    the queue, we must remove it, change its
    contents, and then add it back to the queue

6
ADT Queue
  • Implementation
  • Pointer-based (Linear or Circular)
  • Array-based (Linear or Circular)
  • ADT list

7
UML Diagram for Queue Class
  • ADT queue operations
  • Queue()
  • Queue()
  • isEmpty()boolean query
  • enqueue(in newItemQueueItemType)
  • throw QueueException
  • dequeue()
  • throw QueueException
  • dequeue(out queueFrontQueueItemType) throw
    QueueException
  • peek(out queueFrontQueueItemType) throw
    QueueException

8
ADT Queue
  • Pseudocode
  • Queue()
  • // Creates an empty queue
  • // Precondition None
  • // Postcondition A queue is created and is empty
  • Queue()
  • // Destroys a queue
  • // Precondition Queue exists
  • // Postcondition Queue elements deleted and
    queue destroyed
  • isEmpty() boolean query
  • // Determines whether a queue is empty
  • // Precondition Queue is created
  • // Postcondition Returns true is the queue is
    empty false otherwise
  • enqueue( int newItem) throw QueueException
  • // Inserts (Enqueue) newItem at the back of a
    queue.

9
ADT Queue
  • Pseudocode
  • dequeue( ) throw QueueException
  • // Removes (Dequeue) the front of a queue i.e.
    the item that was
  • // added the earliest. Throws an exception if
    deletion isnt successful.
  • // PreconditionQueue exists and is not empty
  • // PostconditionIf success, front element is
    removed from the queue
  • // otherwise, queue is unchanged
  • dequeue ( int queueFront) throw QueuException
  • // Retrieves into queueFront and removes the
    front of a queue.
  • // Throws an exception if deletion is not
    successful
  • // PreconditionQueue exists and is not empty
  • // PostconditionIf success, front element is
    removed from the queue,
  • // QueuFront holds its value otherwise,
  • // QueueFront is undefined and queue is
    unchanged
  • peek(int queueFront) throw QueueException
  • // Retrieves into queueFront the front of a
    queue. Queue is

10
Queue Applications
  • A queue can retain characters in the order in
    which they are typed
  • Queue aQueue
  • while (not end of line)
  • Read a new character ch
  • aQueue.enqueue(ch)
  • //end while
  • Once the characters are in a queue, the system
    can process them as necessary

11
Queue Applications
  • Converting Digit Strings to Decimal
  • // Convert digits in aQueue into decimal integer
    n
  • // Get first digit, ignoring any leading blanks
  • do aQueue.dequeue( ch)
  • until ( ch ! blank)
  • // Assertion ch contains first digit
  • // Compute n from digits in queue
  • n 0
  • done false
  • do n 10 n integer that ch represents
  • if (! aQueue.isEmpty( ) )
  • aQueue.dequeue(ch)
  • else
  • done true
  • until ( done ch ! digit)
  • // Assertion n is result

12
ADT Queue Implementation
  • Possible implementations
  • Pointer-based
  • Array-based
  • Linear
  • Circular
  • ADT List-based
  • Linear
  • Circular
  • STL
  • All implementations will use the following
    exception
  • include ltexceptopngt
  • include ltstringgt
  • using namespaces std
  • class QueueException public logic_error
  • public
  • QueueException(const message )
    logic_error (message.c_str( ) )
  • // end QueueException

13
ADT Queue Implementation
  • Pointer-Based Implementation
  • More straightforward than array-based
  • Possible options
  • Linear linked list
  • Two external pointer (Front Back)
  • Circular linked list
  • One pointer will be enough (Back)
  • Both the copy constructor destructor must be
    written, because of the dynamic allocation
  • Insertion
  • Inserting a new node at the Back needs 3 pointer
    changes
  • Change Next pointer in the new node
  • Change the Next pointer in the Back node
  • Change the External pointer
  • Special case
  • If the queue is empty
  • Deletion
  • Delete from the Front
  • Only one pointer change is needed
  • Special case

14
ADT Queue Implementation
  • Pointer-Based Implementation Option 1
  • Linear linked list with 2 external pointers
  • Insertion to an empty queue
  • frontPtr backPtr newPtr
  • Insertion to a non-empty queue
  • newPtr -gt next NULL
  • backPtr -gt next newPtr
  • backPtr newPtr
  • Deletion
  • tempPtr frontPtr
  • frontPtr frontPtr -gt next
  • tempPtr -gt next NULL
  • delete tempPtr

15
ADT Queue Implementation
  • Pointer-Based Implementation
  • Circular linear linked list with one external
    pointer
  • Insertion
  • Into an empty queue
  • NewPtr -gt Next NewPtr
  • BackPtr NewPtr
  • Into a non-empty queue
  • NewPtr -gt Next BackPtr-gt Next
  • BackPtr -gt Next NewPtr
  • BackPtr NewPtr

16
ADT Queue Implementation
  • Pointer-Based Implementation
  • Deletion
  • form a one-node (one item) queue
  • FrontPtr BackPtr -gt Next
  • If (Frontptr BackPtr)
  • BackPtr NULL
  • delete FrontPtr
  • form anon-empty, more than one item queue
  • FrontPtr BackPtr -gt Next
  • BackPtr -gt Next FrontPtr -gt Next
  • delete FrontPtr

17
ADT Queue Implementation
  • Pointer-Based Implementation
  • Header file QueueP.h
  • Implementation file QueueP.cpp

18
Using ADT Queue
  • Example
  • include QueueP.h
  • Int main ( )
  • Queue aQueue
  • aQueue.enqueue ( 15 )
  • . . .

19
ADT Queue Implementation
  •  Array-Based Implementation
  • If statically allocated arrays are used, the
    compiler-generated destructor copy constructor
    are sufficient
  • If dynamically allocated arrays are used, we must
    provide the (deep) copy constructor destructor
  • Pre- Post-conditions are the same for
    pointer-based implementation

20
ADT Queue Implementation
  •  Array-Based Implementation
  •  1. Linear Array implementation
  • front back are indexes in the array
  • Initial condition front 0 back -1
  • Initial state
  • After 5 additions

21
ADT Queue Implementation
  •  Array-Based Implementation
  • back will point to the last added element
  • Initial values
  • front 0
  • back -1
  • Insertion
  • Increment back
  • Insert item in item back
  •  Deletion
  • Increment front
  •  Queue Empty Condition
  • back lt front

22
ADT Queue Implementation
  • 1. Linear Array implementation
  • Problem Rightward-Drifting
  • After a sequence of additions removals, items
    will drift towards the end of the array

23
ADT Queue Implementation
  • Rightward drifting solutions
  • Shift array elements after each deletion
  • ? Shifting dominates the cost of the
    implementation
  • Use a circular array When Front or Back reach
    the end of the array, wrap them around to the
    beginning of the array
  • Problem
  • Front Back can't be used to distinguish
    between queue-full queue-empty conditions
  • Solution
  • Use a counter
  • Count 0 means empty queue
  • Count MAX_QUEUE means full queue
  • Another solution is to let front points to the
    array element preceding the front element
  • front back gt Queue is empty
  • Additional convention Reserve the array position
    pointed to by the front to contain no elements
  • Initially front back MAX_QUEUE 1

24
ADT Queue Implementation
  • Circular Array Implementation
  • const int MAX_QUEUE maximum-size-of-queue
  • typedef desired-type-of-queue-item QueuItemType
  • QueueItemType items MAX_QUEUE
  • int front
  • int back
  • int count
  • Insertion
  • back ( back 1 ) MAX_QUEUE
  • itemsback newItem
  • count
  • Deletion
  • front ( front 1 ) MAX_QUEUE
  • - - count

25
ADT Queue Implementation
  • Circular Arrays Implementation
  • Initial condition
  • count 0, front 0, back MAX_QUEUE 1
  • The Wrap-around effect is obtained by using
    modulo arithmetic (-operator)
  •  Insertion
  • Increment back, using modulo arithmetic
  • Insert item
  • Increment count
  • Deletion
  • Increment front using modulo arithmetic
  • Decrement count
  • Disadvantage
  • Overhead of maintaining a counter or flag

26
ADT Queue Implementation
  • Circular Array Implementation
  • Solution
  • Use IsFull flag
  • ? Same overhead as using the count
  • Use extra array location
  • Same space requirement
  • More time-efficient
  • Queue is Full
  • front (back 1) (MAX_QUEUE 1)
  • Queue is Empty
  • front back
  • If the elements of the queue are complex data
    structure, the memory wasted may be significant

27
ADT Queue Implementation
  • Circular Array Implementation

(a) front passes back when the queue becomes
empty (b) back catches up to front when the
queue becomes full
28
ADT Queue Implementation
  • Circular Array Implementation
  • Header File QueueA.h
  • Implementation file QueueA.cpp

29
ADT Queue Implementation
  • ADT List Implementation
  • Use an instance of List class to represent the
    queue
  • Pre- Post-conditions are the same as before
  • The implementation is much simpler

30
ADT Queue Implementation
  • ADT List Implementation
  • If the item in position 1 of a list aList
    represents the front of the queue, the following
    implementations can be used
  • dequeue()
  • aList.remove(1)
  • getFront(queueFront)
  • aList.retrieve(1, queueFront)
  • If the item at the end of the list represents the
    back of the queue, the following implementations
    can be used
  • enqueue(newItem)
  • aList.insert(getLength() 1, newItem)

31
ADT Queue Implementation
  • ADT List Implementation
  • Header File QueueL.h
  • Implementation fiile QueueL.cpp

32
Comparing Implementations
  • Fixed size versus dynamic size
  • A statically allocated array
  • Prevents the enqueue operation from adding an
    item to the queue if the array is full
  • A resizable array or a reference-based
    implementation
  • Does not impose this restriction on the enqueue
    operation
  • Pointer-based implementations
  • A linked list implementation
  • More efficient
  • The ADT list implementation
  • Simpler to write

33
Comparing Implementations
  • Pointer-Based
  • More complicated than ADT List
  • Most flexible No size restrictions
  • Array-Based
  •   No overhead of pointer manipulation
  •   Prevents adding elements if the array is full
  • ADT List-Based
  • Simpler to write
  • Not as efficient as using a linked list directly

34
Summary of Position-Oriented ADTs
  • Lists
  • Operations are defined in terms of position of
    data items
  • No restrictions on the position. All positions
    can be accessed
  • Operations
  • create
  • Creates an empty ADT of the List type
  • isEmpty
  • Determines whether an item exists in the ADT
  • insert
  • Inserts a new item in any given position
  • remove
  • Deletes an item from a given position
  • retrieve
  • Retrieves the item in the specified position

35
Summary of Position-Oriented ADTs
  • Stacks
  • Operations are defined in terms of position of
    data items
  • Position is restricted to the Top of the stack.
    Only one end position can be accessed
  • Operations
  • create
  • Creates an empty ADT of the Stack type
  • isEmpty
  • Determines whether an item exists in the ADT
  • push
  • Inserts a new item into the Top position
  • pop
  • Deletes an item from the Top position
  • peek
  • Retrieves the item from the Top position

36
Summary of Position-Oriented ADTs
  • Queues
  • Operations are defined in terms of position of
    data items
  • Position is restricted to the front back of the
    queue. Only the end positions can be accessed
  • Operations
  • create
  • Creates an empty ADT of the Queue type
  • isEmpty
  • Determines whether an item exists in the ADT
  • enqueue
  • Inserts a new item in the Back position
  • dequeue
  • Deletes an item from the Front position
  • peek
  • Retrieves the item from the Front position

37
Summary of Position-Oriented ADTs
  • Stacks and queues are very similar
  • Operations of stacks and queues can be paired off
    as
  • createStack and createQueue
  • Stack isEmpty and queue isEmpty
  • push and enqueue
  • pop and dequeue
  • Stack getTop and queue getFront

38
Application Simulation
  • Simulation
  • A technique for modeling the behavior of both
    natural and human-made systems. The behavior of a
    system Model should behave similar to real system
  • Goal
  • Generate statistics that summarize the
    performance of an existing system by constructing
    a mathematical or a physical model capturing the
    relevant information about the system
  • Factors affecting the model are changed and the
    effect on the behavior of the system is watched
    and measured
  • Predict the performance of a proposed system
  • Example
  • A simulation of the behavior of a bank
  • Computer tasks
  • Phone answering systems
  • Supermarket
  • Real-word systems are called Queuing Systems
  • Theory used in simulation is called Queuing
    Theory
  • Wikipedia

39
Simulation
  • Goal of simulation
  • Generate statistics summarizing or predicting the
    performance of systems, that could be used for
    improvement
  • What should be known to simulate a queuing
    system?
  • Number and types of events and how they affect
    the system
  • Number of servers
  • Distribution of arrival time of events
  • Expected service time
  • Example Bank system
  • Server Teller
  • Objects being served Customers
  • What should be observed Average waiting time
  • Events Arrivals departures of customers
  • Service time Time needed for each customer

40
Simulation
  • Example Bank system

41
Simulation
  • From the software point of view, simulation is a
    big loop that executes a set of rules
  • Example Rules for a bank system
  • R1
  • If a customer arrives, he/she gets in line
  • R2
  • If the teller is free, and if there is anyone
    waiting, the first customer in line leaves the
    line and advances to the teller window
  • Service time should be known
  • R3
  • If a customer is at the tellers window, the time
    remaining for that customer to be serviced is
    decremented
  • R4
  • If there are customers in line, an additional
    time that they have remained in the queue is
    recorded

42
Simulation
  • Definitions
  • Event-Driven Simulation
  • Simulation that uses events to evaluate the
    performance
  • Simulated time is advanced to time of the next
    event
  • Events are generated by a mathematical model that
    is based on statistics and probability
  • Time-Driven Simulation
  • Simulation depending on using random numbers to
    simulate the time of events' occurrences
  • Simulated time is advanced by a single time unit
  • The time of an event is determined randomly and
    compared with a simulated clock

43
Simulation
  • Note
  • Techniques for generating events to reflect the
    real world require a good deal of mathematics and
    will not be discussed in this class
  • Simulated time
  • The time from the beginning (Time 0) to the end
    of measurement
  • External event
  • Events that the system can't control
  • Internal events
  • Events that can be calculated by the system

44
Application Bank Simulation
  • Ms. Simpson, President of First City Bank of
    Springfield, has heard her customers complain
    about how long they have to wait for service.
    Because she fears that they may move their
    account to another bank, she is considering
    whether to hire a second teller.
  • Ms. Simpson needs an approximation of the
    average time that customer has to wait for
    service from the only teller available.
  • How can she get this information?

45
Application Bank Simulation
  • Questions that should be answered
  • How many teller does the bank employ?
  • How often customers arrive?
  • What is the average time does a customer have to
    wait before receiving service?
  • How many employers should be hired to improve the
    performance of the bank?

46
Application Bank Simulation
  • Events
  • Customer arrivals
  • External events
  • Customer departures when completing transaction
  • Internal events
  • Statistics needed
  • Average time a customer waits for service
  • Total waiting time for all customers / Number
    of customers

47
Application Bank Simulation
  • Assumptions
  • The list of all arrival events, is already
    available for use in a file in the form of pairs
  • (Arrival time, Transaction time)
  • The events are given in
  • Ascending order by arrival time
  • Departure events are not included in the file
    since it could be calculated

48
Application Bank Simulation
  • Event list
  • Keeps track of arrival and departure events that
    will occur but have not occurred yet
  • Contains at most one arrival event and one
    departure event
  • Departure time
  • Departure time arrival time transaction time
  • Waiting time
  • Time elapsed between arrival start of
    transaction

49
Application Bank Simulation
  • Required operations
  • Add / remove customers
  • Add remove events
  • Required data structures
  • A queue representing customers in line
  • will contain arrival time duration of
    transaction
  • A list representing the event list

50
Application Bank Simulation
  • The new customer always enters the queue, even if
    the queue is empty
  • When a customer is ready for service, the
    following operations take place
  • Remove the customer from the queue
  • Delete the arrival event for the new customer
    from the event list
  • Insert the departure event into the event list
  • The place that an event is inserted in the event
    list depends on the relative time of that event

51
Application Bank Simulation
  • Example
  • If the file contains the following
  • Arrival time Transaction duration
  • 20 5
  • 22 4
  • 23 2
  • 30 3
  • The result of simulation will be as follows
  • Time Event
  • 20 Cust1 enters bank begins transaction
  • 22 Cust2 enters bank stands at line end
  • 23 Cust3 enters bank stands at line end
  • 25 Cust1 departs Cust2 begins transaction
  • 29 Cust2 departs Cust3 begins transaction
  • 30 Cust4 enters bank stands at line end
  • 31 Cust3 departs Cust4 begins transaction
  • 34 Cust4 departs

52
Application Bank Simulation
  • Pseudocode (First Draft)
  • Increments the time by increments of 1
  • //Initialize
  • currentTime 0
  • Initialize the line to no customers
  • while (currentTime lt time of final event)
  • if (an arrival event occurs at time
    currentTime)
  • process the arrival event
  • if (a departure event occurs at time
    currentTime)
  • process the departure event
  • // when both arrival departure occur at the
    same time,
  • // arbitrarily process the arrival event first
  • currentTime
  • // end while

53
Application Bank Simulation
  • Pseudocode (Second Draft)
  • Considers only times of relevant events (arrival
    departure)
  • //Initialize the line to no customers
  • while (event remain to be processed)
  • currentTime time of next event
  • if (event is an arrival event )
  • process the arrival event
  • else
  • process the departure event
  • // when both arrival departure events occur at
    the same time,
  • // arbitrarily process the arrival event first
  • // end while

54
Application Bank Simulation
  • Observations
  • Each arrival event generates exactly one
    departure event
  • We cannot generate a departure event for a given
    arrival event independent of other events.
  • If we read the whole file of events, we will need
    to save the calculations that the simulation
    performs
  • gt It is better to store one arrival and one
    departure event at a time and let the simulation
    perform the calculation step-by-step
  • The event list will contain at most one event of
    each kind

55
Application Bank Simulation
  • Observations
  • For arrival events
  • Keep the earliest unprocessed arrival event
  • When the event is processed, replace it with the
    next unprocessed event
  • For departure events
  • The next departure event depends on the customer
    currently served
  • Evaluate time of next departure from
  • Time service begins
  • Length of transaction
  • As soon as a customer begins service, we place
    the corresponding departure event corresponding
    to this customer in the event list

56
Application Bank Simulation
  • Example
  • Event list structure

57
Application Bank Simulation
  • Event list
  • Considers only times of relevant events (arrival
    departure)
  • We need to maintain an event list that contains
    future events
  • A file will contain all the events and they will
    be brought to the event list as needed

58
Application Bank Simulation
  • Possible event list configurations
  • Initially
  • One arrival event A read from the input file
  • Event list A
  • Generally
  • Two events (Arrival A Departure D)
  • Event list A D (next event is arrival)
  • or D A (next event is departure)
  • Special cases
  • If Arrival event is first and, after it is
    processed, the file is empty (eof)
  • Event list D (input has been exhausted)
  • If Departure event is first and teller line is
    empty
  • Event list A (departure leaves teller line
    empty)
  • Events are inserted in the event list the
    beginning or at the end depending on their
    arrival times

59
Application Bank Simulation
  • Algorithm for arrival events
  • // Update the event list
  • Delete the arrival event for customer C from the
    event list
  • if ( new customer C begins transaction
    immediately)
  • Insert a departure event for customer C into the
    event list
  • (time of event current time transaction
    length)
  • If (not at the end of the input file)
  • Read a new arrival event add it to the event
    list
  • (time of event time specified in file)

60
Application Bank Simulation
  • Algorithm for departure events
  • // Update the line
  • Delete customer at front of queue
  • If (the queue is not empty)
  • Current front customer begins transaction
  • // Update the event list
  • Delete the departure event from the event list
  • If (the queue is not empty)
  • Insert into the event list the departure event
    for the customer now at the front of the queue
  • (time of event current time transaction
    length)

61
Application Bank Simulation
  • Final simulation algorithm
  • simulate ( )
  • // perform the simulation
  • Create an empty queue bankQueue to represent the
    bank line
  • Create an empty event list eventList
  • Get the first arrival event from the input file
    place it in eventList
  • while ( eventList is not empty)
  • newEvent first event in eventList
  • if (newEvent is arrival event)
  • processArrival(newEvent, arrivalFile,
    eventList, bankQueue)
  • else
  • processDeparture(newEvent, eventList,
    bankQueue)
  • // end while

62
Application Bank Simulation
  • Final simulation algorithm
  • processArrival( in arrivalEvent Event,
    in arrivalFile File,
  • inout anEventList EventList, inout bankQueue
    Queue)
  • // Processes an arrival event
  • atFront bankQueue.isEmpty() // present queue
    status
  • // Update bankQueue by inserting the customer, as
    described in arrivalEvent,
  • // into the queue
  • bankQueue.enqueue ( arrivalEvent)
  • // Update the event list
  • Delete arrivalEvenet from anEventList
  • If (atFront)
  • // The line was empty, new customer at the
    front of
  • // the line begins transaction immediately
  • Insert into anEventList a departure event
    corresponding to the new customer with currentime
    currentTime transaction length
  • // end if
  • If (not at end of input file)
  • Get the next arrival event from arrivalFile

63
Application Bank Simulation
  • Final simulation algorithm
  • processDeparture( in departureEvent Event,
  • inout anEventList EventList, inout bankQueue
    Queue)
  • // Processes an departure event
  • // Update the line by deleting the front customer
  • bankQueue.dequeue ( )
  • // Update the event list
  • Delete departureEvent from anEventList
  • If (! bamkQueue.isEmpty)
  • // Customer at front of line begins transaction
  • Insert into the event list the departure event
    corresponding to
  • the customer now at the front of the line has
  • currentTime currentTime transaction length
  • // end if

64
Application Bank Simulation
  • ADT Event List Operations
  • createEventList() // Create an empty event list
  • destroyEventList() // Destroys an event list
  • isEmpty() boolean query
  • // Determines whether an event list is empty
  • insert(in anEvent Event)
  • // Inserts anEvent into an event list so that
    events are ordered
  • // by time. If an arrival event departure
    event have the same
  • // time, the arrival event precedes the
    departure event
  • delete() // Deletes the first event from an
    event list
  • retrieve( out anEvent Event)
  • // Sets anEvent to the first event in an event
    list

65
Summary
  • The definition of the queue operations gives the
    ADT queue first-in, first-out (FIFO) behavior
  • A pointer-based implementation of a queue uses
    either
  • A circular linked list
  • A linear linked list with a head reference and a
    tail reference
  • A circular array eliminates the problem of
    rightward drift in an array-based implementation

66
Summary
  • Distinguishing between the queue-full and
    queue-empty conditions in a circular array
  • Count the number of items in the queue
  • Use a isFull flag
  • Leave one array location empty
  • Simulations
  • In a time-driven simulation
  • Time is advanced by a single time unit
  • In an event-driven simulation
  • Time is advanced to the time of the next event
  • To implement an event-driven simulation, you
    maintain an event list that contains events that
    have not yet occurred
Write a Comment
User Comments (0)
About PowerShow.com