Version 1.0 1 TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
1 HeapSort CS 3358 Data Structures. 2 Heapsort: Basic Idea Problem: Arrange an array of items into sorted order. 1) Transform the array of items into.
Priority Queue (Heap) & Heapsort COMP171 Fall 2006 Lecture 11 & 12.
CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
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  Queues: first-in first-out in printer schedule  Disadvantage: short job, important job need to wait  Priority queue is a data structure.
1 TCSS 342, Winter 2005 Lecture Notes Priority Queues and Heaps Weiss Ch. 21, pp
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.
PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University.
Heapsort CIS 606 Spring Overview Heapsort – O(n lg n) worst case—like merge sort. – Sorts in place—like insertion sort. – Combines the best of both.
CSE 373 Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
CSC 172 DATA STRUCTURES. Priority Queues Model Set with priorities associatedwith elements Priorities are comparable by a < operator Operations Insert.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
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.
Heaps, Heapsort, Priority Queues. Sorting So Far Heap: Data structure and associated algorithms, Not garbage collection context.
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.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
COSC2007 Data Structures II Chapter 12 Tables & Priority Queues III.
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Kevin Quinn Fall 2015.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
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.
CSE 373: Data Structures and Algorithms Lecture 11: Priority Queues (Heaps) 1.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
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 © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
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.
Lecture on Data Structures(Trees). Prepared by, Jesmin Akhter, Lecturer, IIT,JU 2 Properties of Heaps ◈ Heaps are binary trees that are ordered.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Heaps and Priority Queues What is a heap? A heap is a binary tree storing keys at its internal nodes and satisfying the following properties:
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)
"Teachers open the door, but you must enter by yourself. "
CSE373: Data Structures & Algorithms Priority Queues
Heaps (8.3) CSE 2011 Winter May 2018.
Priority Queues and Heaps Suggested reading from Weiss book: Ch. 21
Source: Muangsin / Weiss
Bohyung Han CSE, POSTECH
Priority Queues (Heaps)
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
ADT Heap data structure
CSE 373: Data Structures and Algorithms
Heaps and the Heapsort Heaps and priority queues
Tree Representation Heap.
CSE 373: Data Structures and Algorithms
Hassan Khosravi / Geoffrey Tien
Priority Queues & Heaps
CSE 373 Data Structures and Algorithms
CSE 373 Priority queue implementation; Intro to heaps
CSC 380: Design and Analysis of Algorithms
Priority Queues (Heaps)
CSE 373: Data Structures and Algorithms
Heaps & Multi-way Search Trees
EE 312 Software Design and Implementation I
CSE 373: Data Structures and Algorithms
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps

2 Objectives Discuss the Priority Queue ADT Discuss implementations of the PQ Learn about Heaps See how Heaps can implement a priority queue.

3 Priority Queue ADT A simple collection of objects with the following main operations: addElement removeMin Motivating examples: Banks managing customer information Often will remove or get the information about the customer with the minimum bank account balance Shared Printer queue list of print jobs, must get next job with highest priority, and higher-priority jobs always print before lower-priority jobs addElementremoveMin

4 Simple implementations of the Priority Queue ADT addElementremoveMin (unsorted) list sorted list BST AVL tree (overkill?)

5 Discussion of implementations list: store all in an unordered list, remove min/max one by searching for it problem: expensive to search sorted list: store all in a sorted list, then search it in O(log n) time with binary search problem: expensive to add/remove binary tree: store in a BST, search it in O(log n) time for the min (leftmost) element problem: tree could be unbalanced on nonrandom input balanced BST good for O(log n) inserts and removals, but is there a better way?

6 Priority queue ADT priority queue: an collection of ordered elements that provides fast access to the minimum (or maximum) element a mix between a queue and a BST often implemented with a heap priority queue operations (using minimum): add O(1) average, O(log n) worst-case getMin- returns Min w/o removing it O(1) removeMin- returns Min and removes it O(log n) worst-case isEmpty, clear, size, iterator O(1)

7 Heaps and priority queues heap: a tree with the following two properties: 1. completeness complete tree: every level is full except possibly the lowest level, which must be filled from left to right with no leaves to the right of a missing node (i.e., a node may not have any children until all of its possible siblings exist) Heap shape:

8 Heaps and priority queues 2 2. heap ordering a tree has heap ordering if P < X for every element X with parent P in other words, in heaps, parents' element values are always smaller than those of their children implies that minimum element is always the root is a heap a BST? These are min-heaps; Can also have max-heaps.

9 Which are min-heaps?

wrong! Which are max-heaps?

11 Adding to a heap when an element is added to a heap, it should be initially placed as the rightmost leaf (to maintain the completeness property) heap ordering property becomes broken!

12 Adding to a heap, cont'd. to restore heap ordering property, the newly added element must be shifted upward ("bubbled up") until it reaches its proper place bubble up (book: "percolate up") by swapping with parent how many bubble-ups could be necessary, at most?

13 Adding to a max-heap same operations, but must bubble up larger values to top

14 The getMin operation getMin on a min-heap is trivial; because of the heap properties, the minimum element is always the root getMin is O(1)

15 Removing from a min-heap min-heaps only support removeMin ; min is the root must remove the root while maintaining heap completeness and ordering properties intuitively, the last leaf must disappear to keep it a heap initially, just swap root with last leaf (we'll fix it)

16 Removing from a heap, cont'd. must fix heap-ordering property; root is out of order shift the root downward ("bubble down") until it's in place swap it with its smaller child each time

17 Heap height and runtime height of a complete tree is always log n, because it is always balanced this suggests that searches, adds, and removes will have O(log n) worst-case runtime because of this, if we implement a priority queue using a heap, we can provide the O(log n) runtime required for the add and remove operations n-node complete tree of height h: h =  log n  2 h  n  2 h+1 - 1

18 Creating large heaps Can insert all items into a heap one at a time. What is run-time cost of this approach? Or, can recursive create a heap from an initial set of n items: Start with complete tree Create heap out of left subtree of root Create heap out of right subtree of root Bubble root down to create heap out of whole tree. Also can use an iterative technique (discussed next).

19 Turning any input into a heap we can quickly turn any complete tree of comparable elements into a heap with a buildHeap algorithm simply "bubble down" on every node that is not a leaf, starting from the lowest levels upward. Convenient to work from right to left. why does this buildHeap operation work? how long does it take to finish? (big-Oh)

20 Implementation of a heap when implementing a complete binary tree, more efficient to use array implementation index of root = 1(leave 0 empty for simplicity) for any node n at index i, index of n.left = 2i index of n.right = 2i + 1

21 Advantages of array heap the "implicit representation" of a heap in an array makes several operations very fast add a new node at the end (O(1)) from a node, find its parent (O(1)) swap parent and child (O(1)) a lot of dynamic memory allocation of tree nodes is avoided the algorithms shown usually have elegant solutions private void buildHeap() { for (int i = size()/2; i > 0; i--) bubbleDown(i); }

22 Heap sort heap sort: an algorithm to sort an array of N elements by turning the array into a heap, then doing a removeMin N times the elements will come out in sorted order! we can put them into a new sorted array what is the runtime?

23 A max-heap So far have been examining min-heaps, where minimum item is at root. a max-heap is the same thing, but with the items in reverse order max item is at root.

24 Improved heap sort Heapsort using min-heap requires two arrays One for heap one for storing results as min is pulled out. Use a max-heap to implement an improved heap sort that needs no extra storage O(n log n) runtime no external storage required! useful on low-memory devices elegant

25 Improved heap sort 1 use an array heap, but with 0 as the root index max-heap state after buildHeap operation:

26 Improved heap sort 2 state after one removeMin operation: modified removeMin that moves element to end

27 Improved heap sort 3 state after two removeMin operations: notice that the largest elements are at the end (becoming sorted!)

28 Sorting algorithms review Best caseAverage case ( † )Worst case Selection sortn 2 n 2 n 2 Bubble sortnn 2 n 2 Insertion sortnn 2 n 2 Mergesort n log 2 n 2 n 2 n Heapsort n log 2 n 2 n 2 n Treesort n log 2 n 2 nn 2 Quicksort n log 2 n 2 nn 2 † According to Knuth, the average growth rate of Insertion sort is about 0.9 times that of Selection sort and about 0.4 times that of Bubble Sort. Also, the average growth rate of Quicksort is about 0.74 times that of Mergesort and about 0.5 times that of Heapsort.

29 Other PQ Operations decreaseKey(p,  ): bubble up increaseKey(p,  ): bubble down remove(p): decreaseKey(p,  ) deleteMin() Running time: O(log N) findMax: O(N).

30 References Lewis & Chase book, chapter 15.