Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a heap abstract data structure Demonstrate.
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.
Trees Chapter 8.
ITEC200 – Week08 Trees. 2 Chapter Objectives Students can: Describe the Tree abstract data type and use tree terminology such as.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
© 2004 Goodrich, Tamassia Priority Queues1 Heaps: Tree-based Implementation of a Priority Queue.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
© 2004 Goodrich, Tamassia Heaps © 2004 Goodrich, Tamassia Heaps2 Priority Queue Sorting (§ 8.1.4) We can use a priority queue to sort a set.
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Priority Queues. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
Priority Queues. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Priority Queues1 Part-D1 Priority Queues. Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is.
Fundamentals of Python: From First Programs Through Data Structures
Heaps and Priority Queues Priority Queue ADT (§ 2.4.1) A priority queue stores a collection of items An item is a pair (key, element) Main.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Priority Queues and Heaps Bryce Boe 2013/11/20 CS24, Fall 2013.
Trees. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
Priority Queue What is that? Implementation with linked list with O(n) behaviour The Heap (O(log(n)) An implementation using an array Your mission …
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
CSC 213 – Large Scale Programming Lecture 15: Heap-based Priority Queue.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.
Heaps & Priority Queues
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
SkipLists and Balanced Search The Art Of MultiProcessor Programming Maurice Herlihy & Nir Shavit Chapter 14 Avi Kozokin.
Priority Queues CS 110: Data Structures and Algorithms First Semester,
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
2 Binary Heaps What if we’re mostly concerned with finding the most relevant data?  A binary heap is a binary tree (2 or fewer subtrees for each node)
Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists.
Binary Search Trees Chapter 7 Objectives
Chapter 11 Heap.
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
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,
Source: Muangsin / Weiss
Heaps 9/13/2018 3:17 PM Heaps Heaps.
ADT Heap data structure
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Heaps and Priority Queues
Part-D1 Priority Queues
Heaps and Priority Queues
Heaps 11/27/ :05 PM Heaps Heaps.
B-Tree Insertions, Intro to Heaps
© 2013 Goodrich, Tamassia, Goldwasser
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Heaps and Priority Queues
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Priority Queues & Heaps
Heaps and Priority Queues
Chapter 20: Binary Trees.
Heaps 9/29/2019 5:43 PM Heaps Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15

Definition  A set of items.  Each element has a score (indicating its priority).  Bounded range: the scores are taken from a discrete set of items.  Unbounded range: scores are taken from a large set.

Methods  add()  removeMin() – removes the top prioritized item.

Reminder  Linearizability – “requires that each method call appear to take effect at some instant between its invocation and its response.” (Art of Multiprocessor programming, chapter 15)  Quiescent consistency – “requires that in any execution, when all pending method calls complete, the values they return are consistent with some valid execution of the object.” (Art of Multiprocessor programming, chapter 15)

Array-Based Bounded Priority Queue  An array of bins.  Bin: a pool of arbitrary items. Supports put(x) and get(x) which returns an arbitrary item.  Items of score i will be in the i-th bin.

… 01ir-2r-1 … Picture taken from:

Tree-Based Bounded Queue  m leaves (Bins).  m-1internal nodes which are bounded counters.  Counters keep track of the number of items in the leaves of their left subtree.

Leaf to rootRoot to leaf

Properties  The SimpleTree algorithm is not linearizable: threads may overtake each other.  It is quiescently consistent.  Lock-free if the counters and bins are lock free.

Unbounded Heap-Based Queue  Each node holds an item and a score.  The root holds the minimal scored item, which is ‘first’ in the queue.  A child’s score is no smaller than its parent’s.  The (binary) heap is implemented with an array – children of entry i are entries 2i and 2i+1.

Sift Up

Sift Down

Concurrent Heap - Overview  We want to allow concurrent calls to add() and removeMin().  Sifting up/down steps should be interleaved between concurrent calls.  Linearizable

Concurrent Heap  Uses heapLock, to synchronize atomic modifications to the heap’s fields.  HeapNode now has a lock field, a tag field, and an owner.

Concurrent Heap  A node is with a BUSY tag, while it is being sifted up by an add() call.  The owner of a node is decided on its initialization.  add() sifts nodes up, while removeMin() sifts down.  removeMin() is not delayed by an add() call. It can “snatch” a node underneath an add().  In that case, add() simply searches for its node up the branch.

Concurrent Heap

Concurrent Heap – removeMin()

Concurrent Heap – add() Node is initialized with BUSY tag. The owner is the calling thread.

Concurrent Heap – add() The node was moved up by a removeMin().

Skiplist-Based Unbounded Queue  Drawback of the heap – requires constant rebalancing.  We’ll use a skiplist instead.  Reminder: a collection of ordered lists. Each list has a level. Bottom level contains all nodes, each higher level is a sublist of the lower level list.

Skiplist-Based Unbounded Queue  Items will be sorted by priority, not by value.  Removing an item is done lazily – first the node is marked as removed, and later is physically removed.  removeMin() scans the bottom level list for the first unmarked node and tries to mark it. On failure, it continues down the list.  On success, skiplist’s remove() is called to remove the node physically.

Skiplist-Based Unbounded Queue

Properties  Lock free.  Quiescently consistent: if an item x was present before the start of a removeMin() call, then the item returned will have a score less than or equal to that of x.  Not linearizable: a thread might add a higher priority item, and then a lower priority one, and the traversing thread might find and return the later inserted lower priority item (This behavior is quiescently consistent though. It is possible to reorder add() calls concurrent with the removeMin() calls).