Data Structures for Shaping and Scheduling

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

Advanced Data Structures Chapter 16. Priority Queues Collection of elements each of which has a priority. Does not maintain a first-in, first-out discipline.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
Heaps and heapsort COMP171 Fall Sorting III / Slide 2 Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. Sizes: Job.
Priority queues CS310 – Data Structures Professor Roch Weiss, Chapter 6.9, 21 All figures marked with a chapter and section number are copyrighted © 2006.
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Priority Queues  Queues: first-in first-out in printer schedule  Disadvantage: short job, important job need to wait  Priority queue is a data structure.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Cpt S 223 – Advanced Data Structures Course Review Midterm Exam # 2
PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University.
1 Priority Queues (Heaps)  Sections 6.1 to The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
Data Structure & Algorithm II.  In a multiuser computer system, multiple users submit jobs to run on a single processor.  We assume that the time required.
Algorithms and data structures Protected by
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
CompSci 100e 8.1 Scoreboard l What else might we want to do with a data structure? AlgorithmInsertionDeletionSearch Unsorted Vector/array Sorted vector/array.
Heaps, Heap Sort, and Priority Queues. Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two children * A node that.
1 Priority Queues (Heaps)  Sections 6.1 to Priority Queues  Regular queues which supports –First In, First Out –Enqueue(): add a new element.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Heaps and Heap Sort. Sorting III / Slide 2 Background: Complete Binary Trees * A complete binary tree is the tree n Where a node can have 0 (for the leaves)
CSE373: Data Structures & Algorithms Priority Queues
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
CS 201 Data Structures and Algorithms
Heaps (8.3) CSE 2011 Winter May 2018.
AVL TREES.
Priority Queues (Heaps)
Priority Queues and Heaps
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Priority Queues Chuan-Ming Liu
Source: Muangsin / Weiss
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Bohyung Han CSE, POSTECH
CS Anya E. Vostinar Grinnell College
Priority Queues Sections 6.1 to 6.5.
O(lg n) Search Tree Tree T is a search tree made up of n elements: x0 x1 x2 x3 … xn-1 No function (except transverse) takes more than O(lg n) in the.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
7/23/2009 Many thanks to David Sun for some of the included slides!
CPSC 531: System Modeling and Simulation
Priority Queue & Heap CSCI 3110 Nan Chen.
Data Structures & Algorithms Priority Queues & HeapSort
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
CSE332: Data Abstractions Lecture 4: Priority Queues
ITEC 2620M Introduction to Data Structures
Ch. 8 Priority Queues And Heaps
CE 221 Data Structures and Algorithms
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Priority Queues & Heaps
Binary and Binomial Heaps
Definition Applications Implementations Heap Comparison
CSC 380: Design and Analysis of Algorithms
Priority Queues (Heaps)
Priority Queues & Heaps
Data Structures and Algorithm Analysis Priority Queues (Heaps)
Instructor: Dr. Michael Geiger Spring 2019 Lecture 34: Exam 3 Preview
Heaps & Multi-way Search Trees
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Priority Queues Binary Heaps
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Priority Queues (Heaps)
Presentation transcript:

Data Structures for Shaping and Scheduling Heap Calendar Queue Timing Wheel ECE 1545

Data Structures Data structures used to perform scheduling and shaping: Priority queues Calendar queues Timing wheel

Priority Queue A priority queue is an abstract data type that stores a collection of data with a priority Priority queue operations insert: insert an element with a priority deleteMin: remove the element with the lowest priority Property of priority queue: If two elements x and y are in the queue, if x has a lower priority than y, x will be removed before y

Implementing a Priority Queue: Candidates Linked list O(N) worst-case time on either insert() or deleteMin() Binary Search Tree O(log N) average time on insert() and deleteMin() Overkill: all elements are sorted However, we only need the minimum element Heap O(log N) worst case for both insertion and deleteMin operation

Heap A heap is a binary tree which satisfies the heap property: “The key of every node is less than or equal to the key of any of its children.” Properties: The smallest element in a heap is the root No conclusion can be drawn about the order of children The tree is completely filled on all levels except possibly the lowest root 3 4 7 5 6

Vector Representation Use a vector A to store a heap Store elements in level-order Parent (k) : ⌊ k/2 ⌋ Left (k) : 2k Right (k): 2k + 1 Heap property: A[Parent(k)] ≤ A[k] root R l r ll lr rl rr 7 6 5 4 3 2 1 A: R l r ll lr rl rr

insert(x) To insert an element with value x: Create a new leaf node If heap property is violated, swap node with parent node Repeat until parent node is less than x

insert(14) 14 14 14 Example from slides by A. Srinivasan (Florida State)

deleteMin() Replace root with the last leaf (last vector element) This maintains the complete binary tree property but may violate the partially ordered tree property Start at root: If heap property is violated swap position with smaller child 31 31

deleteMin() 31 31 Example from slides by A. Srinivasan (Florida State) 31

Calendar Queue A calendar queue is a priority queue with where the average enqueue and dequeue complexity is O(1) Implemented as an array of sorted linked lists Element of the array is called a ”bucket” Each bucket is associated with a time interval (“width”) Elements of linked list are sorted buckets linked lists current time pointer …

Calendar Queue … Inspired by a desk calendar: one page (bucket) per day of year items of same day are listed in sorted order listed items can be for subsequent years items not in current year are ignored buckets linked lists current time pointer March 18 3/18/2019, 3pm 3/18/2019, 6:30pm 3/18/2020 8am 2019 …

Calendar Queue Calendar queue with n buckets, where each bucket has width w Value x is added to bucket x/w mod n Important: Number and width of buckets must be adjusted Buckets should not have many items in list otherwise enqueue into sorted list is too long Not too many buckets should be empty  otherwise dequeue must search too many buckets Bucket width should not be too large  otherwise data is clustered in few buckets and most other buckets are empty Bucket width should not be too small  otherwise most data is not in “current year”

Calendar Queue: Adjusting number of buckets Target: One item for each bucket Rules of thumb: Double the number of buckets when number of stored entries is twice the number of buckets Halve the number of buckets when number of stored entries is half the number of buckets When bucket number is adjusted, copy items to a new calendar queue

Calendar Queue: Adjusting the bucket width Target: Set Bucket width equal to the average separation of adjacent elements  minimizes enqueue and dequeue times Rules of thumb: Recompute bucket width when copying to a new calendar queue Select samples around place of current time pointer and estimate average separation of adjacent elements Set bucket width to estimate of average separation

Calendar Queue: Tightly clustered items 1000 items, 512 buckets 500 items uniformly in [0, 0.1] 500 items uniformly in [5.6, 5.7] After removing elements in [0,0.01], current time pointer has to cycle through 54 “years” for next items Solution: After running through an “empty year”, make global search for smallest item bucket width = 0.002 buckets linked lists current time pointer [0, 0.002) 5.6 [0.002, 0.004) 0.002 5.6002 [0.004, 0.006) 0.004 5.6004 [0.006, 0.008) 0.006 5.6006 … [1.022, 1.024)

Calendar Queue: Evaluation Evaluation from 1988 (on 5 Mhz PC) Exponential separation between adjacent elements Comparison: Linear linked list Splay tree Supports claim of constant enqueue and dequeue times

Calendar Queue: Evaluation Different distributions of separation:

Calendar Queue: Applications Main application for calendar queue is the management of events in discrete event simulations Suitable to compute timers (for timeout values) Suitable for shaping functions that compute the release time (eligibility time) of packets Less suitable for a sorted scheduler queues (e.g., EDF, WFQ) Due to potential search for smallest element

Timing Wheel O(1) data structure for managing timers Inspired by timing wheel in logic simulators

Simple Timing Wheel Assumes that timers are set to at most MAX value into the future (Here: MAX=8), i.e., within a rotation from the current position A cursor in the timing wheel moves one location every time unit A list for each location If cursor is at position i and if timer is set for i+j, add timer to position (i+j) mod MAX 1 7 2 6 3 5 4 Enqueue and dequeue is O(1) Drawback: required wheel size can be large

Hashed Timing Wheel Works for arbitrary values of the timer Record the number of remaining rounds with the stored entry If cursor is at position i and if timer is set for i+j, add timer to position (i+j) mod MAX with remaining rounds (i+j) div MAX Lists for a position can be sorted or unsorted # of rounds remaining 4 1 7 2 6 3 1 2 5 4 2 1 In the worst-case, enqueue is O(n) with sorted lists If # entries is less than wheel size, average enqueue should be O(1)

Hierarchical Timing Wheel 10:25:30 Seconds pointer advanced per clock tick If seconds pointer wraps, move the minutes pointer If minutes pointer wraps, move the hour pointer Example: Current time: 10h 25m 30s Timer value: 50m 50s Timeout at: 11h 15m 20s Add entry for 11h, also store minutes and seconds hours wheel 1 23 2 … … 11 10 20 15 1 minutes wheel 59 2 … 3 25 … 1 seconds wheel 59 2 … 3 … 4

Hierarchical Timing Wheel 11:00:00 At 11h 0m 0s, remove all entries from 11h slot and move to minutes wheel, into position of minute value In minutes wheel, store the seconds hours wheel 1 23 2 … … 11 10 20 15 1 minutes wheel 59 2 … 3 15 … 20 1 seconds wheel 59 2 … 3 … 4

Hierarchical Timing Wheel 11:15:00 At 11h 15m 0s, remove all entries from 15m slot and move to seconds wheel, into position of seconds value At 11h 15m 20s, the seconds wheel moves to 20s and finds the entry hour wheel 1 23 2 … … 11 10 1 minute wheel 59 2 … 3 15 … 20 1 seconds wheel 59 2 … 3 20 4

Timing Wheels: Summary Application areas for timing wheels similar to calendar queues: Timers Simulations Shapers Hashed timing wheel is essentially a calendar queue Main difference of timing wheel and calendar queue: Timing wheel does not resize