0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.

Slides:



Advertisements
Similar presentations
§6 Leftist Heaps CHAPTER 5 Graph Algorithms  Heap: Structure Property + Order Property Target : Speed up merging in O(N). Leftist Heap: Order Property.
Advertisements

DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI.
COL 106 Shweta Agrawal and Amit Kumar
CMSC 341 Binary Heaps Priority Queues. 8/3/2007 UMBC CSMC 341 PQueue 2 Priority Queues Priority: some property of an object that allows it to be prioritized.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
Priority Queues And the amazing binary heap Chapter 20 in DS&PS Chapter 6 in DS&AA.
1 6.3 Binary Heap - Other Heap Operations There is no way to find any particular key without a linear scan through the entire heap. However, if we know.
CSE332: Data Abstractions Lecture 5: Binary Heaps, Continued Tyler Robison Summer
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
1 COSC 2P03 Lecture #5 – Trees Part III, Heaps. 2 Today Take up the quiz Assignment Questions Red-Black Trees Binary Heaps Heap sort D-Heaps, Leftist.
Priority Queues  Queues: first-in first-out in printer schedule  Disadvantage: short job, important job need to wait  Priority queue is a data structure.
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.
1 Binary heaps binary tree that satisfy two properties –structural property (is a complete tree) –heap-ordering property (minimum item on top) Can have.
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.
CSE 326: Data Structures Lecture #17 Priority Queues Alon Halevy Spring Quarter 2001.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lecture 11 Tuesday, 12/4/01 Advanced Data Structures Chapters.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Binary and Binomial Heaps These lecture slides are adapted from CLRS, Chapters.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
1 Priority Queues (Heaps)  Sections 6.1 to The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
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.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
Heaps, Heapsort, Priority Queues. Sorting So Far Heap: Data structure and associated algorithms, Not garbage collection context.
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
Data Structure & Algorithm II.  Delete-min  Building a heap in O(n) time  Heap Sort.
Chapter 21 Binary Heap.
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
Cpt S 223 – Advanced Data Structures Priority Queues
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.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Dan Grossman Fall 2013.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
1 Heaps (Priority Queues) You are given a set of items A[1..N] We want to find only the smallest or largest (highest priority) item quickly. Examples:
DATA STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
Priority Queues (Heaps)
1 Binomial Tree Binomial tree. n Recursive definition: B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4.
PRIORITY QUEUES AND HEAPS Slides of Ken Birman, Cornell University.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Kevin Quinn Fall 2015.
CSE332: Data Abstractions Lecture 5: Binary Heaps, Continued Dan Grossman Spring 2012.
Heaps Priority Queues. 1/6/2016Data Structure: Heaps2 Outline Binary heaps Binomial queues Leftist heaps.
Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things.
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
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.
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
1 CSE 326: Data Structures Priority Queues (Heaps) Lecture 9: Monday, Jan 27, 2003.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Catie Baker Spring 2015.
CSE 326: Data Structures Lecture #6 Putting Our Heaps Together Steve Wolfman Winter Quarter 2000.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Linda Shapiro Spring 2016.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
CSE373: Data Structures & Algorithms Priority Queues
School of Computing Clemson University Fall, 2012
CS 201 Data Structures and Algorithms
Heaps (8.3) CSE 2011 Winter May 2018.
Hashing Exercises.
CMSC 341: Data Structures Priority Queues – Binary Heaps
Binary and Binomial Heaps
Priority Queues (Heaps)
Priority Queues CSE 373 Data Structures.
Priority Queues Supports the following operations. Insert element x.
Heaps & Multi-way Search Trees
Presentation transcript:

0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures (Ch. 6) n Balanced Search Trees: general search structures (Ch ) n Union-Find data structure (Ch. 8.1–8.5) n Graphs: Representations and basic algorithms  Topological Sort (Ch )  Minimum spanning trees (Ch. 9.5)  Shortest-path algorithms (Ch ) n B-Trees: External-Memory data structures (Ch. 4.7) n kD-Trees: Multi-Dimensional data structures (Ch. 12.6) n Misc.: Streaming data, randomization

1 Priority Queue ADT In many applications, we need a scheduler  A program that decides which job to run next n Often the scheduler a simple FIFO queue  As in bank tellers, DMV, grocery stores etc n But often a more sophisticated policy needed  Routers or switches use priorities on data packets  File transfers vs. streaming video latency requirement  Processors use job priorities n Priority Queue is a more refined form of such a scheduler.

2 Priority Queue ADT A set of elements with priorities, or keys n Basic operations:  insert (element)  element = deleteMin (or deleteMax) n No find operation! n Sometimes also:  increaseKey (element, amount)  decreaseKey (element, amount)  remove (element)  newQueue = union (oldQueue1, oldQueue2)

3 Priority Queue: implementations Unordered linked list  insert is O(1), deleteMin is O(n) n Ordered linked list  deleteMin is O(1), insert is O(n) n Balanced binary search tree  insert, deleteMin are O(log n)  increaseKey, decreaseKey, remove are O(log n)  union is O(n) Most implementations are based on heaps...

4 Heap-ordered Binary trees Tree Structure: A complete binary tree  One element per node  Only vacancies are at the bottom, to the right  Tree filled level by level.  Such a tree with n nodes has height O(log n) n

5 Heap-ordered Binary trees n Heap Property  One element per node  key(parent) < key(child) at all nodes everywhere  Therefore, min key is at the root  Which of the following has the heap property?

6 Basic Heap Operations  percolateUp  used for decreaseKey, insert percolateUp (e): while key(e) < key(parent(e)) swap e with its parent

7 Basic Heap Operations  percolateDown  used for increaseKey, deleteMin percolateDown (e): while key(e) > key(some child of e) swap e with its smallest child

8 Decrease or Increase Key ( element, amount ) Must know where the element is; no find! n DecreaseKey key(element) = key(element) – amount percolateUp (element) n IncreaseKey key(element) = key(element) + amount percolateDown (element)

9 insert ( element ) add element as a new leaf  ( in a binary heap, new leaf goes at end of array) n percolateUp (element) n O( tree height ) = O(log n) for binary heap

10 Binary Heap Examples n Insert 14: add new leaf, then percolateUp n Finish the insert operation on this example.

11deleteMin element to be returned is at the root n to delete it from heap: n swap root with some leaf  (in a binary heap, the last leaf in the array) n percolateDown (new root) n O( tree height ) = O(log n) for binary heap

12 Binary Heap Examples n deleteMin. Hole at the root. n Put last element in it, percolateDown.

13 Array Representation of Binary Heaps Heap best visualized as a tree, but easier to implement as an array n Index arithmetic to compute positions of parent and children, instead of pointers.

14 Short cut for perfectly balanced binary heaps n Array implementation parent =  child/2  Lchild = 2·parent Rchild = 2·parent+1

15 Heapsort and buildHeap n A naïve method for sorting with a heap. n O(N log N) n Improvement: Build the whole heap at once  Start with the array in arbitrary order  Then fix it with the following routine for (int i=0; i<n; i++) H.insert(a[i]); for (int i=0; i<n; i++) H.deleteMin(x); a[i] = x; template BinaryHeap ::buildHeap( ) for (int i=currentSize/2; i>0; i--) percolateDown(i);

16buildHeap Fix the bottom level Fix the next to bottom level Fix the top level

17 Analysis of buildHeap n For each i, the cost is the height of the subtree at i n For perfect binary trees of height h, sum:

18 Summary of binary heap operations n insert: O(log n) n deleteMin: O(log n) n increaseKey: O(log n) n decreaseKey: O(log n) n remove: O(log n) n buildHeap: O(n) n advantage: simple array representation, no pointers n disadvantage: union is still O(n)

19 Some Applications and Extensions of Binary Heap Heap Sort n Graph algorithms (Shortest Paths, MST) n Event driven simulation n Tracking top K items in a stream of data n d-ary Heaps:  Insert O(log d n)  deleteMin O(d log d n)  Optimize value of d for insert/deleteMin

20 Leftist heaps: Mergeable Heaps n Binary Heaps great for insert and deleteMin but do not support merge operation n Leftist Heap is a priority queue data structure that also supports merge of two heaps in O(log n) time. n Leftist heaps introduce an elegant idea even if you never use merging. n There are several ways to define the height of a node. n In order to achieve their merge property, leftist heaps use NPL (null path length), a seemingly arbitrary definition, whose intuition will become clear later.

21 Leftist heaps n NPL(X) : length of shortest path from X to a null pointer n Leftist heap : heap-ordered binary tree in which NPL(leftchild(X)) >= NPLl(rightchild(X)) for every node X. n Therefore, npl(X) = length of the right path from X n also, NPL(root)  log(N+1)  proof: show by induction that NPL(root) = r implies tree has at least 2 r - 1 nodes

22 Leftist heaps n NPL(X) : length of shortest path from X to a null pointer n Two examples. Which one is a valid Leftist Heap?

23 Leftist heaps n NPL(root)  log(N+1)  proof: show by induction that NPL(root) = r implies tree has at least 2 r - 1 nodes  The key operation in Leftist Heaps is Merge.  Given two leftist heaps, H 1 and H 2, merge them into a single leftist heap in O(log n) time.

24 Leftist heaps: Merge n Let H 1 and H 2 be two heaps to be merged  Assume root key of H 1 <= root key of H 2  Recursively merge H 2 with right child of H 1, and make the result the new right child of H 1  Swap the left and right children of H 1 to restore the leftist property, if necessary

25 Leftist heaps: Merge n Result of merging H 2 with right child of H 1

26 Leftist heaps: Merge n Make the result the new right child of H 1

27 Leftist heaps: Merge n Because of leftist violation at root, swap the children n This is the final outcome

28 Leftist heaps: Operations n Insert:create a single node heap, and merge n deleteMin: delete root, and merge the children n Each operation takes O(log n) because root’s NPL bound

29 Merging leftist heaps n insert: merge with a new 1-node heap n deleteMin: delete root, merge the two subtrees n All in worst-case O(log n) time Merge (t1, t2) if t1.empty() then return t2; if t2.empty() then return t1; if (t1.key > t2.key) then swap(t1, t2); t1.right = Merge(t1.right, t2); if npl(t1.right) > npl(t1.left) then swap(t1.left, t1.right); npl(t1) = npl(t1.right) + 1; return t1

30 Other priority queue implementations n skew heaps  like leftist heaps, but no balance condition  always swap children of root after merge  amortized (not per-operation) time bounds n binomial queues  binomial queue = collection of heap-ordered “ binomial trees ”, each with size a power of 2  merge looks just like adding integers in base 2  very flexible set of operations n Fibonacci heaps  variation of binomial queues  decreaseKey runs in O(1) amortized time, other operations in O(log n) amortized time