CSE 373: Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

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.
Priority Queue (Heap) & Heapsort COMP171 Fall 2006 Lecture 11 & 12.
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.
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
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.
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.
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.
Chapter 21 Binary Heap.
data ordered along paths from root to leaf
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
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. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Kevin Quinn Fall 2015.
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.
CSE 373: Data Structures and Algorithms Lecture 11: Priority Queues (Heaps) 1.
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, 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)
CSE373: Data Structures & Algorithms Priority Queues
CSE373: Data Structures & Algorithms
CS 201 Data Structures and Algorithms
Heaps (8.3) CSE 2011 Winter May 2018.
Priority Queues and Heaps Suggested reading from Weiss book: Ch. 21
October 30th – Priority QUeues
Source: Muangsin / Weiss
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
March 31 – Priority Queues and the heap
Bohyung Han CSE, POSTECH
Binary Heaps What is a Binary Heap?
ADT Heap data structure
- Alan Perlis Heaps "You think you know when you can learn,
7/23/2009 Many thanks to David Sun for some of the included slides!
CMSC 341: Data Structures Priority Queues – Binary Heaps
Priority Queue & Heap CSCI 3110 Nan Chen.
CMSC 341 Lecture 14 Priority Queues & Heaps
B-Tree Insertions, Intro to Heaps
CSE332: Data Abstractions Lecture 4: Priority Queues
CSE 373 Data Structures and Algorithms
Heaps and the Heapsort Heaps and priority queues
Priority Queues.
Tree Representation Heap.
CSE 373: Data Structures and Algorithms
Hassan Khosravi / Geoffrey Tien
CE 221 Data Structures and Algorithms
CSE 326: Data Structures Priority Queues & Binary Heaps
CSE 332: Data Structures Priority Queues – Binary Heaps Part II
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
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.
Priority Queues CSE 373 Data Structures.
CSE 373: Data Structures and Algorithms
Heaps & Multi-way Search Trees
Heaps.
CSE 373: Data Structures and Algorithms
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

CSE 373: Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)

Motivating examples Bandwidth management: A router is connected to a line with limited bandwidth. If there is insufficient bandwidth, the router maintains a queue for incoming data such that the most important data will get forwarded first as bandwidth becomes available. Printing: A shared server has a list of print jobs to print. It wants to print them in chronological order, but each print job also has a priority, and higher-priority jobs always print before lower-priority jobs Algorithms: We are writing a ghost AI algorithm for Pac-Man. It needs to search for the best path to find Pac-Man; it will enqueue all possible paths with priorities (based on guesses about which one will succeed), and try them in order.

Priority Queue ADT priority queue: A collection of elements that provides fast access to the minimum (or maximum) element a mix between a queue and a BST basic priority queue operations: insert: Add an element to the priority queue (priority matters) remove (i.e. deleteMin): Removes/returns minimum element

Using PriorityQueues implements Queue interface PriorityQueue<E>() constructs a PriorityQueue that orders the elements according to their compareTo (element type must implement Comparable) add(element) inserts the element into the PriorityQueue remove() removes and returns the element at the head of the queue peek() returns, but does not remove, the element at the head of the queue Queue<String> pq = new PriorityQueue<String>(); pq.add("Kona"); pq.add("Daisy"); implements Queue interface

Potential Implementations O(1)/O(N)worst-array full, should say WHY, might reject on full instead. insert deleteMin Unsorted list (Array) Unsorted list (Linked-List) Sorted list (Array) Sorted list (Linked-List) Binary Search Tree AVL Trees O(N) – to find value O(1) O(N) – to find value O(log N) to find loc w. Bin search, but O(N) to move vals O(1) to find val, but O(N) to move vals, (or O(1) if in reverse order) O(N) to find loc, O(1) to do the insert O(1) O(N) O(N) We are going to do an insert = O(log N) worst case, O(1) on average (on average 1.67 levels) Deletemin O(log N) – worst and average case. Plus – good memory usage O(log N) close to O(1) 1.67 levels on average O(log N) Binary Heap

Potential Implementations O(1)/O(N)worst-array full, should say WHY, might reject on full instead. insert deleteMin Unsorted list (Array) (1) (n) Unsorted list (Linked-List) Sorted list (Array) (1)* Sorted list (Linked-List) Binary Search Tree (n) worst AVL Trees (log n) O(N) – to find value O(1) O(N) – to find value O(log N) to find loc w. Bin search, but O(N) to move vals O(1) to find val, but O(N) to move vals, (or O(1) if in reverse order) O(N) to find loc, O(1) to do the insert O(1) O(N) O(N) * Assume sorted array has lowest priority value item last, so we can easily delete it, without having to move anything. We are going to do an insert = (log N) worst case, (1) on average (on average 1.67 levels) DeleteMin (log N) – worst and average case. Plus – good memory usage O(log N) close to O(1) 1.67 levels on average O(log N) Binary Heap

Heap properties 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:

Heap properties 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 every a heap a BST? Are any heaps BSTs? Answer: no, it is not a BST.

Which are min-heaps? wrong! 10 20 10 20 80 10 80 20 80 40 60 85 99 40 30 15 50 700 50 700 10 10 20 80 10 40 60 85 99 20 80 20 80 50 700 40 60 99 40 60

Which are max-heaps? 7 wrong! 30 10 20 48 80 21 10 25 30 14 24 9 18 28 11 22 35 30 50 33 30 10 40 10 17 7 3

Heap height and runtime height of a complete tree is always log n, because it is always balanced 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: 2h  n  2h+1 – 1 h = log n

Implementation of a heap when implementing a complete binary tree, we actually can "cheat" and just use an array 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 parent index?

Implementing Priority Queue with Binary Heap public interface IntPriorityQueue { public void add(int value); public boolean isEmtpy(); public int peek(); public int remove(); } public class IntBinaryHeap implements IntPriorityQueue { private static final int DEFAULT_CAPACITY = 10; private int[] array; private int size; public IntBinaryHeap () { array = new int[DEFAULT_CAPACITY]; size = 0; ...

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! 10 10 20 80 20 80 40 60 85 99 40 60 85 99 50 700 65 50 700 65 15

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? 10 10 20 80 15 80 40 60 85 99 40 20 85 99 50 700 65 15 50 700 65 60

Adding to a max-heap same operations, but must bubble up larger values to top 16 5 11 3 18 16 18 11 3 5 18 16 11 3 5

Heap practice problem Draw the state of the min-heap tree after adding the following elements to it: 6, 50, 11, 25, 42, 20, 104, 76, 19, 55, 88, 2

Code for add method public void add(int value) { // grow array if needed if (size >= array.length - 1) { array = this.resize(); } // place element into heap at bottom size++; int index = size; array[index] = value; bubbleUp();

The bubbleUp helper private void bubbleUp() { int index = this.size; while (hasParent(index) && (parent(index) > array[index])) { // parent/child are out of order; swap them swap(index, parentIndex(index)); index = parentIndex(index); } // helpers private boolean hasParent(int i) { return i > 1; } private int parentIndex(int i) { return i/2; } private int parent(int i) { return array[parentIndex(i)]; }

The peek operation peek on a min-heap is trivial; because of the heap properties, the minimum element is always the root peek is O(1) peek on a max-heap would be O(1) as well, but would return you the maximum element and not the minimum one 99 60 40 80 20 10 50 700 85 65

Code for peek method public int peek() { if (this.isEmpty()) { throw new IllegalStateException(); } return array[1];