1 / 37

CSE 30331 Lecture 10 – Queues, Heaps, …

CSE 30331 Lecture 10 – Queues, Heaps, …. Queues Radix Sort Time driven Simulation Priority Queues Quick introduction to heaps Psort (heapsort w/o details) Quick aside Ncurses subwindows. Reading Reminder. Queues, Radix Sort & Simulation Cormen: Ch 8.3 & Ch 10 Ford: Ch 8

caine
Download Presentation

CSE 30331 Lecture 10 – Queues, Heaps, …

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 30331Lecture 10 – Queues, Heaps, … • Queues • Radix Sort • Time driven Simulation • Priority Queues • Quick introduction to heaps • Psort (heapsort w/o details) • Quick aside • Ncurses subwindows

  2. Reading Reminder • Queues, Radix Sort & Simulation • Cormen: Ch 8.3 & Ch 10 • Ford: Ch 8 • Priority Queues, Heaps & Heapsort • Ford: 8.6, 14.1 & 14.2 • Cormen: Ch 6

  3. Grocery Store Checkout: A Model for a Queue

  4. The Queue A Queue is a FIFO (First in First Out) Data Structure. Elements are inserted in the Rear of the queue and are removed at the Front.

  5. queue(); Create an empty queue. CLASS queue CLASS queue <queue> <queue> Constructor Operations bool empty() const; Check whether the queue is empty. Return true if it is empty and false otherwise. T& front(); Return a reference to the value of the item at the font of the queue. Precondition: The queue is not empty.

  6. const T& front() const; Constant version of front(). CLASS queue <queue> Operations void pop(); Remove the item from the front of the queue. Precondition: The queue is not empty. Postcondition: The element at the front of the queue is the element that was added immediately after the element just popped or the queue is empty.

  7. CLASS queue <queue> Operations void push(const T& item); Insert the argument item at the back of the queue. Postcondition: The queue has a new item at the back int size() const; Return the number of elements in the queue.

  8. Radix Sort • List of items is sorted based on values of keys • Repeatedly (from least to most significant digit) • (1) List items are shuffled into sublists (queues) based on the value of individual digits in their keys • (2) The list is rebuilt from the queues • Basic algorithm (numbers – radix 10) For each power of 10 (10p = 1, 10, 100, ...) // distribute the list items into 10 queues based on // value of the item’s digit at position for 10p Distribute(nlist,nqueues[],p) // collect queues back into list in order of q digit Collect(nlist,nqueus)

  9. The Radix Sort Order ten 2 digit numbers in 10 bins from smallest number to largest number. Requires 2 calls to the distribute() and collect() Algorithms. Initial Sequence: 91 6 85 15 92 35 30 22 39 Pass 0: Distribute the cards into bins according to the 1's digit (100).

  10. The Radix Sort After first Distribute() & Collect() Sequence(0): 30 91 92 22 85 15 35 6 39 Pass 0: Collect the values from queues in numerical order of queue digit -- left to right, bottom (front) to top (back)

  11. The Radix Sort Sequence(0): 30 91 92 22 85 15 35 6 39 Pass 1: Take the new sequence and distribute the cards into bins determined by the 10's digit (101).

  12. The Radix Sort Sequence(1): 6 15 22 30 35 39 85 91 92 Pass 1: Collect the values from queues in numerical order of queue digit -- left to right, bottom (front) to top (back)

  13. RadixSort() // each key has d digits (radix 10) void radixSort (vector<int>& v, int d) { int power = 1; queue<int> digitQueue[10]; // base 10 numbers for (int i=0; i<d; i++) { distribute (v, digitQueue, power); collect (digitQueue, v); power = power*10; } }

  14. Distribute() // distribute elements of v to digitQueues based on // digit value corresponding to power of 10 void distribute ( vector<int>& v, queue<int> digitQueue[], int power ) { for (int i=0; i<v.size(); i++) // (v[i]/power)%10 is digit corresponding // to position for power of 10, it gets // used as index into array of queues & // we push v[i] into that queue digitQueue[ (v[i]/power)%10 ].push(v[i]); }

  15. Collect() // collect all values from queues back into // vector v, using queues in order of the digits // for which they stand void collect ( queue<int> digitQueue[], vector<int>& v ) { int i=0; for (int digit=1; digit<10; digit++) // append all values from this queue to v while (!digitQueue[digit].empty()) { v[i] = digitQueue[digit].front(); digitQueue[digit].pop(); i++; } }

  16. Radix Sort Analysis • Number of values in vector = n • Number of queues = radix r (10 in this case) • Number of digits in key = d (independent of n) • NOTE: rand d are CONSTANTS, independent of the problem size n For each digit position d Distribute n values into r queues Collect n values from r queues • F(n) = d*(2*n) = (2d)n  Θ(n) linear

  17. Simulation • Simulations of interest to us are time driven • Events are typically arrivals of entities seeking some service, benefit or access to a resource • Arrival events over time may be generated randomly, uniformly or in accordance with a scripted sequence • What we measure is waiting time, service time, resource utilization, etc • Over time and multiple simulations using different constraints, the individual stats are summarized and compared to assess the best situation

  18. Simulation • Example: Customers at the drive-up bank teller • Variables: • How many tellers are on duty • How many drive-up lanes are open • Time taken for a teller to serve a customer • Use of Queues • Each drive-up lane is represented by a queue with time of arrival stored for each customer waiting • Tellers (vector of count-down timers) • Teller[k] is available if timer[k] == 0 • Teller[k] is put to work by setting timer[k] to time required for a given job

  19. Simulation – main loop • For each time interval • Number of customer arrivals is determined (probabilistically) • Lane placement of each arrival is determined (random or shortest-line or …) • Current time is pushed onto each queue getting a new arrival (this is the customer’s arrival time) • For each available teller and waiting customer • Customer in queue waiting longest is popped and assigned to teller • Teller’s counter is set to timer required • Waiting time and total time for service are computed • Current time is updated and all teller times are decremented by the interval

  20. Traffic Simulation • Queues • One for each lane of traffic entering intersection • Arrivals determined by random chance, but based on known frequencies for time of day and real intersection • Time of arrival kept in queue for each vehicle • Rules of the road • Assume no accidents • Assume all drivers are law abiding • Order of progression through intersection based on traffic rules (e.g., left turn yields to oncoming traffic unless left turn arrow, etc)

  21. Traffic Simulation • Traffic lights • No lights • Lights, but no special turn arrows • Lights with turn arrows • Different light timing sequences • Statistics • Longest waiting time • Average waiting time (each lane and overall) • Average number of cars waiting at red light • Number of cars through intersection • ……..

  22. Simulation • Advanced options • Light changes keyed to presence of cars in lane • Simulation of grids of connected intersections • Coordinated timing sequences at different intersections • Simulations run for different times of day and different days of week (traffic arrival frequencies differ) • Simulation of highway segment through multiple lights

  23. Priority Queue A Special form of queue from which items are removed according to their designated priority and not the order in which they entered. Items entered the queue in sequential order but will be removed in the order #2, #1, #4, #3.

  24. priority_queue(); Create an empty priority queue. Type T must implement the operator <. CLASS priority_queue CLASS priority_queue <queue> <queue> Constructor Operations bool empty() const; Check whether the priority queue is empty. Return true if it is empty, and false otherwise. Create void pop(); Remove the item of highest priority from the queue. Precondition: The priority queue is not empty. Postcondition: The priority queue has 1 less element

  25. CLASS priority_queue <queue> Operations void push(const T& item); Insert the argument item into the priority queue. Postcondition: The priority queue contains a new element. int size() const; Return the number of items in the priority queue. T& top(); Return a reference to the item having the highest priority. Precondition: The priority queue is not empty. const T& top(); Constant version of top().

  26. Priority Queue == Heap? • A Heap is a good underlying implementation for a priority queue • A Heap is a complete binary tree • Each parent node has a value (priority) greater than that of either child • Depth (height) of tree is lg n • Insertion & deletion make use of heapAdjust() to reorder heap in lg n time • The single out of order value is trickled up or down the tree until it is in place

  27. A Heap This pictorial version of a heap corresponds to the array 50 28 45 20 25 42 35 11 15 10 NOTE: 50 > 28 and 50 > 45 (parent’s value is > either child’s)

  28. Removing Elements From a Heap

  29. Psort() == heapsort • Basic algorithm • For each element in vector v • Add element to priority queue pq • While priority queue pq is not empty • Remove item from pq and append to vector v • Analysis: • Number of elements in v = n • Effort to add element to pq = lg n • Effort to remove element from pq = lg n • F(n) = n(2lg n) = 2n lg n  Θ(n lg n)

  30. Psort() // a heapsort without the details template <typename T> void psort(vector<T>& v) { int i, n = v.size(); priority_queue<T> pq; // build priority queue from vector for (i=0; i<n;i++) pq.push(v[i]); // rebuild vector from priority queue for (i=0; i<n;i++) { v[i]=pq.top(); pq.pop(v[i]); } }

  31. Ncurses Subwindows • Subwindow shares memory with parent • Indexing (row,col) relative to subwindow, not parent • Subwindows may be scrollable and allow text wrapping • Versions of move, refresh, and printw exist for subwindows as well as for use with stdscr

  32. Subwindow Example // declaration of pointer to WINDOW WINDOW *myWin; // create subwindow of size (rows x cols) with // upper left corner at location r,c relative to // the parent window (stdscr) myWin = subwindow(stdscr,rows,cols,r,c); // move to a location in subwindow and print an // integer value wmove(myWin,r,c); wprintw(myWin,”%d”,intValue); // alternately, do it all a t once mvwprintw(myWin,r,c,” ,”%d”,intValue);

  33. Subwindow Example (cont) // to refresh the subwindow wrefresh(myWin); // NOTE: to make sure shared section of window // memory is kept updated, it is generally required // that the parent window be forced to refresh // before accessing and refreshing the subwindow // first touchwin(); // trick stdscr into updating refresh(); // refresh it // then do what you want with the subwindow mvwprintw(myWin,r,c,” ,”%d”,intValue); wrefresh(myWin);

  34. Summary • Queue • A first-come-first-served data structure. • Insertion operation push() occurs at the back of the sequence • deletion operation pop() occurs at the front of the sequence.

  35. Summary • The radix sort algorithm • Orders an integer vector by using queues (bins). • This sorting technique has running time O(n) but has only specialized applications. • The more general in-place O(n lg n) sorting algorithms are preferable in most cases.

  36. Summary • Time driven simulations naturally use queues to store events waiting to occur • Values in the queues indicate queue entry time • Combined with time of queue exit this allows • Computation of average and maximum wait times • Identification of bottlenecks in the scenario • Etc …

  37. Summary • Priority queue • Pop() returns the highest priority item (largest or smallest). • Normally implemented by a heap, which is discussed in Ford & Topp Chapter 14. • The push() and pop() operations have running time O(lg n) • Sorting can be accomplished in O(n lg n) time using a priority queue (essentially, this is a heapsort.

More Related