Hassan Khosravi / Geoffrey Tien

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

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.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
1 TCSS 342, Winter 2005 Lecture Notes Priority Queues and Heaps Weiss Ch. 21, pp
CSE 373 Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)
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:
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.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
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.
Sorting Dr. Yingwu Zhu. Heaps A heap is a binary tree with properties: 1. It is complete Each level of tree completely filled Except possibly bottom level.
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.
Sorting Dr. Yingwu Zhu. Heaps A heap is a binary tree with properties: 1. It is complete Each level of tree completely filled Except possibly bottom level.
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.
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.
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:
Sorting With Priority Queue In-place Extra O(N) space
Priority Queues A priority queue is an ADT where:
"Teachers open the door, but you must enter by yourself. "
Partially Ordered Data ,Heap,Binary Heap
Chapter 11 Heap.
Heaps (8.3) CSE 2011 Winter May 2018.
Design & Analysis of Algorithm Priority Queue
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,
October 30th – Priority QUeues
Hashing Exercises.
Bohyung Han CSE, POSTECH
Heap Sort Example Qamar Abbas.
Priority Queues (Heaps)
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
ADT Heap data structure
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!
CSCI2100 Data Structures Tutorial 7
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
CMSC 341: Data Structures Priority Queues – Binary Heaps
Part-D1 Priority Queues
Heapsort Heap & Priority Queue.
What is a heap? Always keep the thing we are most interested in close to the top (and fast to access). Like a binary search tree, but less structured.
Draw pictures to indicate the subproblems middleMax solves at each level and the resulting maxPtr and PrevPtr for each on this linked list:
CSE 373: Data Structures and Algorithms
CMSC 341 Lecture 14 Priority Queues & Heaps
Hassan Khosravi / Geoffrey Tien
B-Tree Insertions, Intro to Heaps
ITEC 2620M Introduction to Data Structures
Tree Representation Heap.
Ch. 8 Priority Queues And Heaps
"Teachers open the door, but you must enter by yourself. "
CS Data Structure: Heaps.
Tables and Priority Queues
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Priority Queues & Heaps
Sorting Dr. Yingwu Zhu.
CSE 373 Priority queue implementation; Intro to heaps
Priority Queues (Heaps)
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Priority Queues & Heaps
CO4301 – Advanced Games Development Week 4 Binary Search Trees
Hash Maps: The point of a hash map is to FIND DATA QUICKLY.
B-Trees.
Tree traversals BST properties Search Insertion
Data Structures and Algorithm Analysis Priority Queues (Heaps)
Heaps & Multi-way Search Trees
CMPT 225 Lecture 16 – Heap Sort.
Presentation transcript:

Hassan Khosravi / Geoffrey Tien Priority Queues Binary heaps November 06, 2017 Hassan Khosravi / Geoffrey Tien

The story thus far... CPSC 259 topics up to this point Abstract data types Tools Pointers Priority queue Dynamic memory allocation Stack Queue Dictionary Asymptotic analysis Recursion Data structures Algorithms Heapsort Linked list Circular array Array Binary heap (Binary) (search) tree November 06, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Priority queues? Suppose Geoff has made a to-do list (with priority values): 7 – Mark midterm 2 – Vacuum home 8 – Install winter tyres 1 – Sleep 9 – Fix overflowing sink 2 – Eat 8 – Lecture prep 1 – Bathe We are interested in quickly finding the task with the highest priority November 06, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Priority queue ADT A collection organised so as to allow fast access to and removal of the largest (or smallest) element Prioritisation is a weaker condition than ordering Order of insertion is irrelevant Element with the highest priority (whatever that means) is the first element to be removed Not really a queue: not FIFO! November 06, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Priority queue ADT Priority queue operations create destroy insert removeMin (or removeMax) isEmpty F(7) E(5) D(100) A(4) B(6) insert C(3) removeMin G(9) C(3) G(9) Priority queue property For two elements 𝑥 and 𝑦 in the queue, if 𝑥 has a lower priority value than 𝑦, 𝑥 will be removed before 𝑦 November 06, 2017 Hassan Khosravi / Geoffrey Tien

Priority queue properties A priority queue is an ADT that maintains a multiset of items vs set: a multiset allows duplicate entries Two or more distinct items in a priority queue may have the same priority If all items have the same priority, will it behave FIFO like a queue? not necessarily! Due to implementation details November 06, 2017 Hassan Khosravi / Geoffrey Tien

Priority queue applications Hold jobs for a printer in order of size Manage limited resources such as bandwidth on a transmission line from a network router Sort numbers Anything greedy: an algorithm that makes the "locally best choice" at each step November 06, 2017 Hassan Khosravi / Geoffrey Tien

Data structures for priority queues Worst case complexities Structure insert removeMin Unordered list 𝑂 1 𝑂 𝑛 Ordered list Binary search tree A balanced BST can do insert and removeMin in 𝑂 log 𝑛 , but we cannot guarantee balance Binary heap 𝑂 log 𝑛 November 06, 2017 Hassan Khosravi / Geoffrey Tien

Binary heap A heap is binary tree with two properties A complete, partially-ordered binary tree A heap is binary tree with two properties Heaps are complete All levels, except the bottom, must be completely filled in The leaves on the bottom level are as far to the left as possible Heaps are partially ordered For a max heap – the value of a node is at least as large as its children’s values For a min heap – the value of a node is no greater than its children’s values November 06, 2017 Hassan Khosravi / Geoffrey Tien

Review: complete binary trees incomplete binary trees November 06, 2017 Hassan Khosravi / Geoffrey Tien

Partially ordered tree max heap example 98 86 41 13 65 32 29 9 10 44 23 21 Heaps are not fully ordered – an in-order traversal would result in: 9, 13, 10, 86, 44, 65, 23, 98, 21, 32, 32, 41, 29 November 06, 2017 Hassan Khosravi / Geoffrey Tien

Duplicate priority values Min heap examples It is important to realise that two binary heaps can contain the same data, but items may appear in different positions in the structure 2 2 5 7 5 5 5 7 8 7 7 8 November 06, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Heap implementation Using an array Heaps can be implemented using arrays There is a natural method of indexing tree nodes Index nodes from top to bottom and left to right as shown on the right Because heaps are complete binary trees there can be no gaps in the array 1 2 3 4 5 6 1 2 3 4 5 6 ... November 06, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Referencing nodes To move around in the tree, it will be necessary to find the index of the parents of a node or the children of a node The array is indexed from 0 to 𝑛−1 Each level’s nodes are indexed from: 2 𝑙𝑒𝑣𝑒𝑙 −1 to 2 𝑙𝑒𝑣𝑒𝑙+1 −2 (where the root is level 0) The children of a node 𝑖, are the array elements indexed at 2𝑖+1 and 2𝑖+2 The parent of a node 𝑖, is the array element indexed at 𝑖−1 2 November 06, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Array heap example 98 86 41 13 65 32 29 9 10 44 23 21 Heap 1 2 3 4 5 6 7 8 9 10 11 12 Underlying array 98 86 41 13 65 32 29 9 10 1 2 3 4 5 6 44 23 21 11 12 7 8 value index November 06, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Heap implementation typedef struct MinHeap { int size; // number of stored elements int capacity; // maximum capacity of array int* arr; // array in dynamic memory } MinHeap; void initializeMinHeap(MinHeap* h, int initcapacity) { h->size = 0; h->capacity = initcapacity; h->arr = (int*) malloc(h->capacity * sizeof(int)); } November 06, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Heap insertion On insertion the heap properties have to be maintained, remember that A heap is a complete binary tree and A partially ordered binary tree There are two general strategies that could be used to maintain the heap properties Make sure that the tree is complete and then fix the ordering or Make sure the ordering is correct first Which is better? November 06, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Heap insertion sketch The insertion algorithm first ensures that the tree is complete Make the new item the first available (left-most) leaf on the bottom level i.e. the first free element in the underlying array Fix the partial ordering Compare the new value to its parent Swap them if the new value is greater than the parent Repeat until this is not the case Referred to as heapify up, percolate up, or trickle up, bubble up, etc. November 06, 2017 Hassan Khosravi / Geoffrey Tien

Heap insertion example max heap Insert 81 98 86 41 13 65 32 29 9 10 44 23 21 98 86 41 13 65 32 29 9 10 1 2 3 4 5 6 44 23 21 11 12 7 8 value index November 06, 2017 Hassan Khosravi / Geoffrey Tien

Heap insertion example max heap Insert 81 98 86 41 13 65 32 29 9 10 44 23 21 81 29 81 parent: 13−1 2 =6 98 86 41 13 65 32 29 9 10 1 2 3 4 5 6 44 23 21 11 12 7 8 value index 81 81 29 13 November 06, 2017 Hassan Khosravi / Geoffrey Tien

Heap insertion example max heap 81 is less than 98 so finished Insert 81 98 86 41 13 65 32 81 9 10 44 23 21 81 41 29 parent: 6−1 2 =2 98 86 41 13 65 32 81 9 10 1 2 3 4 5 6 44 23 21 11 12 7 8 value index 81 41 29 13 November 06, 2017 Hassan Khosravi / Geoffrey Tien

Heap insertion complexity Item is inserted at the bottom level in the first available space this can be tracked using the heap size attribute 𝑂 1 access using array index Repeated heapify-up operations. How many? each heapify-up operation moves the inserted value up one level in the tree Upper limit on the number of levels in a complete tree? 𝑂 log 𝑛 Heap insertion has worst-case performance of 𝑂 log 𝑛 November 06, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Exercise Insert the following sequence of items into an initially empty max heap: 34, 76, 11, 32, 73, 9, 50, 65, 41, 27, 3, 88 Draw the underlying array after all insertions have completed Insert the same sequence of items into an initially empty min heap and draw the array after all insertions have been completed. For inserting 𝑛 items, what is the total complexity? Implement recursive and iterative implementations: void heapifyUp(MinHeap* h, int index); November 06, 2017 Hassan Khosravi / Geoffrey Tien