Dr. Andrew Wallace PhD BEng(hons) EurIng

Slides:



Advertisements
Similar presentations
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)
Advertisements

CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
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.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2009 Heap Lecture Chapter 6 Use NOTES feature to see explanation.
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.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2007 Heap Lecture Chapter 6 Use NOTES feature to see explanation.
Week 10: Heap and Priority queue. Any feature here?
PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Heap Lecture 2 Chapter 7 Wed. 10/10/01 Use NOTES feature to.
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.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Binary and Binomial Heaps These lecture slides are adapted from CLRS, Chapters.
CSC 172 DATA STRUCTURES. Priority Queues Model Set with priorities associatedwith elements Priorities are comparable by a < operator Operations Insert.
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
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.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Heapsort By Pedro Oñate CS-146 Dr. Sin-Min Lee. Overview: Uses a heap as its data structure In-place sorting algorithm – memory efficient Time complexity.
Chapter 21 Binary Heap.
data ordered along paths from root to leaf
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
Trees Dr. Andrew Wallace PhD BEng(hons) EurIng
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
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.
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.
Binary Heaps Text Read Weiss, § Binary Heap One-array representation of a tree Complete trees Building a Binary Heap Insert Delete.
PRIORITY QUEUES AND HEAPS Slides of Ken Birman, Cornell University.
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
Heaps & Priority Queues
Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
1 Heap Sort. A Heap is a Binary Tree Height of tree = longest path from root to leaf =  (lgn) A heap is a binary tree satisfying the heap condition:
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
CS 367 Introduction to Data Structures Lecture 8.
Priority Queues and Heaps Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
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.
Fibonacci Heaps. Fibonacci Binary insert O(1) O(log(n)) find O(1) N/A union O(1) N/A minimum O(1) O(1) decrease key O(1) O(log(n)) delete O(log(n) O(log(n))
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
Lecture: Priority Queue
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,
Programming Abstractions
Hashing Exercises.
Source: Muangsin / Weiss
Heapsort.
Heap Sort Example Qamar Abbas.
Heapsort.
Heaps.
7/23/2009 Many thanks to David Sun for some of the included slides!
Initializing A Max Heap
Heapsort Heap & Priority Queue.
Priority Queues.
i206: Lecture 14: Heaps, Graphs intro.
Priority Queues.
ITEC 2620M Introduction to Data Structures
Tree Representation Heap.
"Teachers open the door, but you must enter by yourself. "
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Priority Queues & Heaps
HEAPS.
Heapsort.
Asst. Prof. Dr. İlker Kocabaş
Presentation transcript:

Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se Priority Queue / Heap Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Overview Priority Queue Priority Queue Examples Heap Heap Implementation Building a Heap Heap Operations Variations of Heap

Priority Queue A priority queue is a queue with … err … priorities! Special case: Queues Stacks Memory Address Memory Address P P-3 Memory Address Memory Address P-1 P-2 Memory Address Memory Address P-2 P-1 Memory Address Memory Address P-3 P Queue Stack

Priority Queue Operations Insert (push, enqueue) Remove (pop, dequeue) With priority Remove (pop, dequeue) isEmpty inspectFirst (peek)

Priority Queue Implementation Array Linked list Heap

Priority Queue 12 12 45 5 23 89

Priority Queue Examples Discrete Event Simulator

Priority Queue Examples Path finding in robotics

Priority Queue Examples Past path cost function Know distance from start Future path cost function Heuristics Priority queue Possible route segments

Priority Queue Examples A*(G, start) create priority queue F create came_from create cost_so_far F.put(start) came_from[start] ← NULL cost_so_far ← 0 While not F.empty() n ← F.get() if n = goal return for next ← G.number_of_neighbours do new_cost ← cost_so_far[n] + G.cost(n, next) if next not in cost_so_far or new_cost < cost_so_far[next] cost_so_far[next] = new_cost priority = new_cost + Heuristic(goal, next) F.put(next, priority) came_from[next] ← current Return came_from, cost_so_far

Priority Queue Examples Discrete Event Simulator Event + time

Heap Tree data structure Ordered relationship between child and parent 67 Tree data structure Often a binary tree Ordered relationship between child and parent Not between siblings Heap property 13 8 3 1

Heap 67 1 Max / Min heap 13 8 13 8 3 1 30 100

Heap Operations Create Insert Delete (max or min) isEmpty Extract max or min

Heap Implementation Usually as an array Binary tree Almost complete 67 13 8 67 13 8 9 10 6 5 3 1 4 2 9 10 6 5 3 1 4 2

Priority queue as a heap 20 Root at index 1 Left at index 2 Right index 3 For node n, children at 2n and 2n+1 10 19 7 8 12 18 1 5 3 6 20 10 19 7 8 12 18 1 5 3 6

Priority queue as a heap Insert Add at end New leaf on the tree Fix the priorities Compare with parent If smaller, swap Continue until priorities are fixed 20 10 19 7 8 17 12 18 1 5 3 6 12 17

Priority queue as a heap RemoveMax Return data at index 1 Copy end of array to index 1 Compare and swap to restore priorities 20 17 19 10 17 19 7 8 12 18 1 5 3 6 17

Building a Heap Build a heap Heapify HeapSort

Building a Heap 11 10 8 2 4 10 8 11 4 2 11 10 8 4 2

Building a Heap In a binary tree, where do the leaves start? 11 In a binary tree, where do the leaves start? 𝒏 𝟐 + 1 to n n = 5, leaves start at 3 n = 6, leaves start at 4 Every leaf is a heap with one node! 10 8 4 2 1

Building a Heap 1 3 6 9 12 67 211 211 1 12 3 211 1 6 67 9 3 12 67 1 211 6

Building a Heap 30 5 MaxHeapify(A, i) l ← 2i r ← 2i+1 if l <= A.size and A[l] > A[i] then largest ← l else largest ← i if r <= A.size and A[r] > A[largest] then largest = r if largest not = i then swap(A[i], A[largest]) MaxHeapify(A, largest) 5 30 12

Building a Heap BuildMaxHeap(A) size[A] = length[A] for i = length[A] to i MaxHeapify(A, i)

Building a Heap 𝒏 𝟐 𝒉+𝟏 Complexity of BuildHeap MaxHeapify is applied to all nodes at a given level How many nodes at each level? 𝒏 𝟐 𝒉+𝟏 Height h = 0, n= 7, number of nodes = 4 Height h = 1, n= 7, number of nodes = 2

Building a Heap 𝒏 𝟐 𝒉+𝟏 O(h) 𝒉=𝟎 𝒍𝒐𝒈𝒏 𝒏 𝟐 𝒉+𝟏 (ch) 𝒄𝒏 𝟐 𝒉=𝟎 𝒍𝒐𝒈𝒏 𝒉 𝟐𝒉 Complexity at h 𝒏 𝟐 𝒉+𝟏 O(h) 𝒉=𝟎 𝒍𝒐𝒈𝒏 𝒏 𝟐 𝒉+𝟏 (ch) 𝒄𝒏 𝟐 𝒉=𝟎 𝒍𝒐𝒈𝒏 𝒉 𝟐𝒉 O 𝒄𝒏 𝟐 𝒉=𝟎 ∞ 𝒉 𝟐𝒉 = O(n)

Heap Operations Extract Max Insert

Heap Operations 3 10 8 ExtractMax(A) max ← A[1] A[1] ← A[A.heap_size] A.heap_size ← A.heap_size – 1 MaxHeapify(A, 1) return max 8 3 6 2 1 5 3 10

Heap Operations O(h) O(logn)

Heap Operations Insert 10 8 6 Insert Insert at the end and copy upwards till heap property is satisfied Worst case O(logn) 5 7 3 2 12

Heap Operations 1 8 2 5 1 2 10 1 HeapSort(A, n) BuildMaxHeap(A) for i ← A.size to 2 swap(A[A.last], A[i]) MaxHeapify(A, i -1) 8 2 1 2 1 5 5 8 1 10 2 1 2 5 8 10

Heap Operations O(logn) Heap Increase Key(A, i, key) A[i] ← key while i > 1 and A.parent(i) < A[i] do swap(A[i], A.parent(i)) i ← parent(i) 23 8 12 1 3 6 7

Variations of Heap Fibonacci heaps Binomial heaps

Variations of Heap Fibonacci heaps Set of trees Minimum trees Fibonacci numbers used in the run time analysis 1 5 5 5 6 5 5

Variations of Heap Fibonacci heaps have no predefined order However, number of children are low A child is root of a subtree at k

Questions?