cs302 topic simulation and priority queues
play

CS302 Topic: Simulation and Priority Queues The banking - PowerPoint PPT Presentation

CS302 Topic: Simulation and Priority Queues The banking simulation, Part I Tuesday, Oct. 4, 2005 Announcements Lab 4 (Stock Reports); due this Friday, Oct. 7 Dont procrastinate!! I like work: it fascinates me. I like work:


  1. CS302 Topic: Simulation and Priority Queues The banking simulation, Part I Tuesday, Oct. 4, 2005

  2. Announcements � Lab 4 (Stock Reports); due this Friday, Oct. 7 � Don’t procrastinate!! I like work: it fascinates me. I like work: it fascinates me. I can sit and look at it for hours. I can sit and look at it for hours. -- J.K. Jerome (Three Men in a Boat, 1889) J.K. Jerome (Three Men in a Boat, 1889) -- � Midterm : � 2 weeks from today (Tues., Oct. 18) � Includes material covered in class and on labs � Open book, open notes � No electronic devices

  3. Recall: What are properties of Priority Queue / Heap? � Heap � What are properties of heap? � Structure property: � Complete binary tree. In other words, tree is completely filled, with possible exception of last level, which is filled from left to right � Heap-Order property (for a min heap): � For every internal node v other than the root, key ( v ) ≥ key ( parent ( v )) �

  4. Recall: What are Priority Queue / Heap Runtimes? � Heap � Time complexities: � insert: � O (log n ), worst case � O (1), on average � Can be shown that, on average, don’t have to percolate new element all the way up � On average, percolation terminates early � On average, 2.607 comparisons are required to perform an insert � deleteMin: � O (log n ) � buildHeap: � O ( n )

  5. Recall: What are possible implementations of Priority Queues? � � Sorted linear list Unsorted linear list 4 5 2 3 1 1 2 3 4 5 � Heap � Red-black tree W hich did w e favor? Heaps Better runtime characteristics W hy?

  6. A C+ + Implementation of Priority Queues � NOTE: Here, we are giving an example of implementing priority queues using red-black trees � In Lab 6: You’ll implement Priority Queues using Heaps – THIS IS THE PREFERRED WAY � NOTE: Different implementations of Priority Queues can have different interfaces � We’ll describe one possible interface today � In Lab 6: Your interface might be slightly different!

  7. A Public Interface for a Priority Queue What operations should we provide? � Required (for any priority queue): � Insert() � DeleteMin() � Optional (depends on interface design): � minKey() � minVal() class pQueue { � size() public: � empty() pQueue(); ~pQueue(); void insert(int key, Jval val); int minKey(); Jval minVal(); void deleteMin(); int size(); bool empty(); };

  8. Example Program using pQueues #include <iostream> #include "pQueue.h" #include "Fields.h" What does this program do? #include <string> using namespace std; Sorts each line of standard input based on its first field (which we main() { assume is integer) pQueue p; Fields f; sortem.cpp string *s; int key; while (f.get_line() >= 0) { // we need to use a pointer to a string because a Jval cannot store // a string (it can store a char * but it cannot store a string) s = new string(f.get_current_line()); sscanf(s->c_str(), "%d", &key); p.insert(key, new_jval_v(s)); } while (!p.Empty()) { s = (string *) p.minVal().v; cout << *s; p.deleteMin(); } }

  9. Priority Queue interface using red-black trees � Hold elements of priority queue in red-black tree � Maintain size of queue � What should the data variables be for this priority queue interface? class pQueue { public: ... protected rbTree <int> tree; int queueSize; };

  10. Priority Queue interface using red-black trees (con’t.) � What should constructor do? pQueue::pQueue() { queuesize = 0; } � Since tree is statically allocated, it is automatically created and destroyed. So, no need for memory allocation in constructor. � What should destructor do? pQueue::~pQueue() { }

  11. Priority Queue interface using red-black trees (con’t.) � How do we implement insert()? void pQueue::insert(int key, Jval val) { tree.insert(key, val); queueSize++; }

  12. Priority Queue interface using red-black trees (con’t.) � How do we implement minKey()? int pQueue::minKey() { if (queueSize == 0) { fprintf(stderr, "pQueue::Minkey() called on an empty pQueue\n"); exit(1); } tree.first(); return tree.getKey(); }

  13. Priority Queue interface using red-black trees (con’t.) � How do we implement minVal()? Jval pQueue::minVal() { if (queueSize == 0) { fprintf(stderr, "pQueue::Minval() called on an empty pQueue\n"); exit(1); } tree.first(); return tree.getVal(); }

  14. Priority Queue interface using red-black trees (con’t.) � How do we implement deleteMin()? void pQueue::deleteMin() { if (queueSize == 0) { fprintf(stderr, "pQueue::deleteMin() called on an empty pQueue\n"); exit(1); } tree.first(); tree.deleteNode(); queueSize--; }

  15. Priority Queue interface using red-black trees (con’t.) � How do we implement size()? int pQueue::size() { return queueSize; } � How do we implement empty()? bool pQueue::empty() { return (queueSize == 0); }

  16. Recall: Applications of Priority Queues? � Example Applications: � Unix/ Linux job scheduling � Processes given a priority � Time allocated to process is based on priority of job � Priority of jobs in printer queue � Sorting � Standby passengers at airport � Auctions � Stock market � Event-driven simulations � VLSI design (channel routing, pin layout) � Artificial intelligence search algorithms

  17. Discrete Event Simulations � Here, we’re interested in procesess and systems that change at discrete instants � What are examples of discrete events ? � Pushing an elevator button � Starting of a motor � Stopping of a motor � Arrival of a customer � Departure of a customer � Turning on a light � These events are discrete events , because there is an instant of time at which each occurs

  18. Are these discrete events? � Train departing station � Yes � Train moving from point A to point B � No � Train arriving at point B � Yes � Note: � We can model an event of a train moving from point A to point B as two separate events (i.e., the departure and the arrival) � If we associate a time value with each discrete event, then we can model the duration of activities

  19. Discrete Event Simulators aren’t good for continuous time � For example, not good for simulating motion of planetary bodies � Better approach? � Differential equations

  20. Note on Queuing Theory � Queuing Theory: a branch of mathematics that deals with computing (probabilistically) delays in lines at servicing points � Answer depends on: � How frequently users arrive � How long it takes to process a user once their turn arrives � Both of above typically represented as probability distribution functions � Finding solutions: � Simple cases: Can compute answer analytically � More complex cases (e.g., with k operators): Solve using discrete event simulation

  21. The Banking Simulation � We have a bank: � Customers arrive and wait in a line until one of k tellers is available � Customer arrival governed by probability distribution � Service time governed by a probability distribution � We want to know: � How long (on average) does a customer have to wait? � How long (on average) is the line? � What is the optimal number of tellers? � Enough so that customers don’t have to wait too long � Not so many that we waste our money paying tellers

  22. The Simulation Process � Simulation consists of processing events. � Here, events are: � Customer arriving � Customer departing � Use probability distributions to generate: � Input stream consisting of ordered pairs of arrival time and service time, sorted by arrival time � Then, we run the simulation for a while and collect data on: � How the number of tellers impacts how long people wait � How long tellers are idle � From data, we can determine optimal number of tellers

  23. Example of simulation process � Our system: 2 tellers, 4 customers. � Simulator generates these events: New Person: 0 enters the bank at 1.5. Transaction time: 5.7 New Person: 1 enters the bank at 2.8. Transaction time: 1.9 New Person: 2 enters the bank at 3.3. Transaction time: 8.7 New Person: 3 enters the bank at 9.2. Transaction time: 2.7 based on “customer arrival” based on “service time” probability distribution probability distribution

Recommend


More recommend