IndexMinPQ.

Slides:



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

CSC2100B Tutorial 7 Heap Jianye Hao.
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.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
Binary Heaps Text Read Weiss, § Binary Heap One-array representation of a tree Complete trees Building a Binary Heap Insert Delete.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R3. Priority Queues.
CompSci 100e 8.1 Scoreboard l What else might we want to do with a data structure? AlgorithmInsertionDeletionSearch Unsorted Vector/array Sorted vector/array.
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.
Properties: -The value in each node is greater than all values in the node’s subtrees -Complete tree! (fills up from left to right) Max Heap.
Heaps Data Structures & OO Development II 1 Computer Science Dept Va Tech August 2006 ©2006 McQuain Heaps A heap is a complete binary tree. A max-heap.
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 Tom Przybylinski. Maps ● We have (key,value) pairs, called entries ● We want to store and find/remove arbitrary entries (random.
The Heap ADT A heap is a complete binary tree where each node’s datum is greater than or equal to the data of all of the nodes in the left and right subtrees.
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:
Partially Ordered Data ,Heap,Binary Heap
Heaps (8.3) CSE 2011 Winter May 2018.
Data Structures and Algorithms I Day 9, 9/22/11 Heap Sort
Priority Queues and Heaps
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
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,
Data Structures and Algorithms I Sorting
Binary Search Tree Chapter 10.
Source: Muangsin / Weiss
Double-Ended Priority Queues
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Binary Heaps What is a Binary Heap?
March 31 – Priority Queues and the heap
CSCE 3100 Data Structures and Algorithm Analysis
Bohyung Han CSE, POSTECH
Heaps.
Priority Queues Linked-list Insert Æ Æ head head
Binary Heaps What is a Binary Heap?
IndexMinPQ.
Heaps and Priority Queue
Stacks Linked Lists Queues Heaps Hashes
- 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!
CSCI2100 Data Structures Tutorial 7
Binary Heaps Text Binary Heap Building a Binary Heap
Part-D1 Priority Queues
Heapsort Heap & Priority Queue.
Search Sorted Array: Binary Search Linked List: Linear Search
CMSC 341 Lecture 14 Priority Queues & Heaps
Binary Tree Application Operations in Heaps
- Alan Perlis Topic 24 Heaps "You think you know when you can learn,
ITEC 2620M Introduction to Data Structures
Priority Queues.
Tree Representation Heap.
Heaps A heap is a binary tree.
Ch. 8 Priority Queues And Heaps
Heaps A heap is a binary tree that satisfies the following properties:
CSE 332: Data Structures Priority Queues – Binary Heaps Part II
Ch. 12 Tables and Priority Queues
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
A Heap Implementation Chapter 26 Adapted from Pearson Education, Inc.
Priority Queues & Heaps
Data Structures Lecture 29 Sohail Aslam.
CS 367 – Introduction to Data Structures
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Heaps By JJ Shepherd.
Priority Queue and Heap
Insertion Sort Array index Value Insertion sort.
Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

IndexMinPQ

MinPQ: Min-Heap A complete binary tree Root has smaller value than both children No relation between left and right children

Min-Heap Insert / Remove: Swim and Sink Heap property must be maintained 5 14 23 32 41 87 90 50 64 53

Min-Heap example Representation 5 14 23 Array (index starting at 0) Parent is i Left: i * 2 Right: i * 2 + 1 32 41 87 90 50 64 53 1 2 3 4 5 6 7 8 9 10 5 14 23 32 41 87 90 50 64 53

Min-Heap example Insert (Swim) 5 14 23 Insert 43 32 41 87 90 50 64 53 private void swim(int k) { while (k > 1 && greater(k/2, k)) { exch(k, k/2); k = k/2; } 1 2 3 4 5 6 7 8 9 10 11 5 14 23 32 41 87 90 50 64 53 43

Min-Heap example Insert (Swim) 5 Insert 18 14 23 32 41 87 90 50 64 53 43 18 private void swim(int k) { while (k > 1 && greater(k/2, k)) { exch(k, k/2); k = k/2; } 1 2 3 4 5 6 7 8 9 10 11 12 5 14 23 32 41 87 90 50 64 53 43 18

Min-Heap example Insert (Swim) 5 Insert 18 14 23 32 41 18 90 50 64 53 43 87 private void swim(int k) { while (k > 1 && greater(k/2, k)) { exch(k, k/2); k = k/2; } 1 2 3 4 5 6 7 8 9 10 11 12 5 14 23 32 41 18 90 50 64 53 43 87

Min-Heap example Insert (Swim) 5 Insert 18 14 18 32 41 23 90 50 64 53 43 87 private void swim(int k) { while (k > 1 && greater(k/2, k)) { exch(k, k/2); k = k/2; } 1 2 3 4 5 6 7 8 9 10 11 12 5 14 18 32 41 23 90 50 64 53 43 87

Min-Heap example Remove (Sink) 5 14 18 32 41 23 90 50 64 53 43 87 1 2 Remove min Return `5` Swap 87 14 18 32 41 23 90 50 64 53 43 87 private void sink(int k) { while (2*k <= n) { int j = 2*k; if (j < n && greater(j, j+1)) j++; if (!greater(k, j)) break; exch(k, j); k = j; } 1 2 3 4 5 6 7 8 9 10 11 12 5 14 18 32 41 23 90 50 64 53 43 87

Min-Heap example Remove (Sink) 87 14 18 32 41 23 90 50 64 53 43 1 2 3 private void sink(int k) { while (2*k <= n) { int j = 2*k; if (j < n && greater(j, j+1)) j++; if (!greater(k, j)) break; exch(k, j); k = j; } 1 2 3 4 5 6 7 8 9 10 11 12 87 14 18 32 41 23 90 50 64 53 43 -1

Min-Heap example Remove (Sink) 14 87 18 32 41 23 90 50 64 53 43 1 2 3 private void sink(int k) { while (2*k <= n) { int j = 2*k; if (j < n && greater(j, j+1)) j++; if (!greater(k, j)) break; exch(k, j); k = j; } 1 2 3 4 5 6 7 8 9 10 11 12 14 87 18 32 41 23 90 50 64 53 43 -1

Min-Heap example Remove (Sink) 14 32 18 87 41 23 90 50 64 53 43 1 2 3 private void sink(int k) { while (2*k <= n) { int j = 2*k; if (j < n && greater(j, j+1)) j++; if (!greater(k, j)) break; exch(k, j); k = j; } 1 2 3 4 5 6 7 8 9 10 11 12 14 32 18 87 41 23 90 50 64 53 43 -1

Min-Heap example Remove (Sink) 14 32 18 50 41 23 90 87 64 53 43 1 2 3 private void sink(int k) { while (2*k <= n) { int j = 2*k; if (j < n && greater(j, j+1)) j++; if (!greater(k, j)) break; exch(k, j); k = j; } 1 2 3 4 5 6 7 8 9 10 11 12 14 32 18 50 41 23 90 87 64 53 43 -1

IndexMinPQ ChangeKey n = new CardPrice("NE", 333.0); 349 BB ChangeKey n = new CardPrice("NE", 333.0); a = new CardPrice("AMZN", 339.0); x = new CardPrice("NCIX", 338.0); b = new CardPrice("BB", 349.0); Update price for NE: 340.00 Update price for NCIX: 345.00 Update price for BB: 200.00 339 AMZN 333 NE 338 NCIX Note: 0-based indexing!!!!!

IndexMinPQ ChangeKey Find the index in the array Some operation 349 BB ChangeKey Find the index in the array Some operation 339 AMZN 333 NE 338 NCIX

IndexMinPQ 349 BB ChangeKey If we know the entry, what operation should we process ? We changed the key, we need to maintain the heap property. 339 AMZN 333 NE 338 NCIX Update price for NCIX: 345.00

IndexMinPQ – ChangeKey (Case 1) public void changeKey(int i, Key key) { keys[i] = key; swim(qp[i]); sink(qp[i]); } We process both swim and sink since the entry may be arbitrary. 349 BB 339 AMZN 333 NE 338 345 NCIX Update price for NCIX: 345.00

IndexMinPQ – ChangeKey (Case 2) public void changeKey(int i, Key key) { keys[i] = key; swim(qp[i]); sink(qp[i]); } We process both swim and sink since the entry may be arbitrary. 349 BB 339 305 AMZN 333 NE 338 NCIX Update price for AMZN: 305.00

IndexMinPQ - ChangeKey changeKey(int i, Key key) Given the hash key `i`, change its associated sorting key to `key` E.g. Update price for AMZN: 305.00 Three mappings qp: hash key to index, e.g., qp[`AMZN`]=1 pq: index to hash key, e.g., pq[1]=`AMZN` keys: hash key to sorting key, e.g., keys[`AMZN`]=339

IndexMinPQ - ChangeKey 349 BB IndexMinPQ - ChangeKey 339 305 AMZN 333 NE In the textbook example, hash key can only be integer, (`i` here) public void changeKey(int i, Key key) { keys[i] = key; swim(qp[i]); sink(qp[i]); } Hash key and sorting key Hash key: i (`NE`, `AMZN`, `NCIX`, `BB`) Sorting key: key (349, 305, 333, 338) `pq` and `qp` in the textbook example `qp`: a mapping from hash key to the index in the array `pq`: a mapping from index of the array to hash key `keys`: priority mapping Maps hash keys to sorting keys 338 NCIX Update price for AMZN: 305.00 qp pq[0]=`BB` pq[1]=`AMZN` pq[2]=`NE` pq[3]=`NCIX` keys[`NE`]=333 keys[`AMZN`]=339 keys[`NCIX`]=338 keys[`BB`]=349 1 2 3 4 349 339 333 338

IndexMinPQ - ChangeKey private boolean greater(int i, int j) { return keys[pq[i]].compareTo(keys[pq[j]]) > 0; } private void swim(int k) { while (k > 1 && greater(k/2, k)) { exch(k, k/2); k = k/2; } private void sink(int k) { while (2*k <= n) { int j = 2*k; if (j < n && greater(j, j+1)) j++; if (!greater(k, j)) break; exch(k, j); k = j; private void exch(int i, int j) { int swap = pq[i]; pq[i] = pq[j]; pq[j] = swap; qp[pq[i]] = i; qp[pq[j]] = j; } public void changeKey(int i, Key key) { keys[i] = key; swim(qp[i]); sink(qp[i]); } Find what we are comparing Find what we are exchanging qp: hash key to index pq: index to hash key keys: hash key to sorting key

349 BB IndexMinPQ - delete 339 AMZN 333 NE public void delete(int i) { int index = qp[i]; exch(index, n--); swim(index); sink(index); keys[i] = null; qp[i] = -1; } 338 NCIX