Priority queues implemented via heaps

Similar documents
Divide-and-Conquer Algorithms Part Two

Heaps and Priority Queues

CS Data Structures and Algorithm Analysis

Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn

Assignment 5: Solutions

8 Priority Queues. 8 Priority Queues. Prim s Minimum Spanning Tree Algorithm. Dijkstra s Shortest Path Algorithm

ENS Lyon Camp. Day 2. Basic group. Cartesian Tree. 26 October

Objec&ves. Review. Data structure: Heaps Data structure: Graphs. What is a priority queue? What is a heap?

COMP 250. Lecture 24. heaps 2. Nov. 3, 2017

Search Trees. Chapter 10. CSE 2011 Prof. J. Elder Last Updated: :52 AM

CS 240 Data Structures and Data Management. Module 4: Dictionaries

Randomized Sorting Algorithms Quick sort can be converted to a randomized algorithm by picking the pivot element randomly. In this case we can show th

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: Oct 26. Homework #2. ( Due: Nov 8 )

CS361 Homework #3 Solutions

Review Of Topics. Review: Induction

CS213d Data Structures and Algorithms

INF2220: algorithms and data structures Series 1

Search Trees. EECS 2011 Prof. J. Elder Last Updated: 24 March 2015

Insertion Sort. We take the first element: 34 is sorted

Quiz 1 Solutions. Problem 2. Asymptotics & Recurrences [20 points] (3 parts)

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

Data Structures and Algorithms " Search Trees!!

past balancing schemes require maintenance of balance info at all times, are aggresive use work of searches to pay for work of rebalancing

Binary Search Trees. Lecture 29 Section Robb T. Koether. Hampden-Sydney College. Fri, Apr 8, 2016

Fibonacci (Min-)Heap. (I draw dashed lines in place of of circular lists.) 1 / 17

Part IA Algorithms Notes

AVL Trees. Manolis Koubarakis. Data Structures and Programming Techniques

Fibonacci Heaps These lecture slides are adapted from CLRS, Chapter 20.

Binary Search Trees. Motivation

Data Structures and Algorithms

Tutorial 4. Dynamic Set: Amortized Analysis

Functional Data Structures

Advanced Implementations of Tables: Balanced Search Trees and Hashing

Premaster Course Algorithms 1 Chapter 3: Elementary Data Structures

Dictionary: an abstract data type

Outline. 1 Introduction. 3 Quicksort. 4 Analysis. 5 References. Idea. 1 Choose an element x and reorder the array as follows:

Greedy. Outline CS141. Stefano Lonardi, UCR 1. Activity selection Fractional knapsack Huffman encoding Later:

Lecture 5: Splay Trees

Breadth First Search, Dijkstra s Algorithm for Shortest Paths

Dictionary: an abstract data type

Design and Analysis of Algorithms

CS 4407 Algorithms Lecture: Shortest Path Algorithms

Algorithm runtime analysis and computational tractability

Data Structures 1 NTIN066

A Simple Implementation Technique for Priority Search Queues

Selection and Adversary Arguments. COMP 215 Lecture 19

25. Minimum Spanning Trees

25. Minimum Spanning Trees

Queues. Principles of Computer Science II. Basic features of Queues

Fundamental Algorithms

Slides for CIS 675. Huffman Encoding, 1. Huffman Encoding, 2. Huffman Encoding, 3. Encoding 1. DPV Chapter 5, Part 2. Encoding 2

21. Dynamic Programming III. FPTAS [Ottman/Widmayer, Kap. 7.2, 7.3, Cormen et al, Kap. 15,35.5]

FIBONACCI HEAPS. preliminaries insert extract the minimum decrease key bounding the rank meld and delete. Copyright 2013 Kevin Wayne

Analysis of Algorithms. Outline 1 Introduction Basic Definitions Ordered Trees. Fibonacci Heaps. Andres Mendez-Vazquez. October 29, Notes.

Advanced Analysis of Algorithms - Midterm (Solutions)

4.8 Huffman Codes. These lecture slides are supplied by Mathijs de Weerd

Algorithms. Algorithms 2.4 PRIORITY QUEUES. Pro tip: Sit somewhere where you can work in a group of 2 or 3

Amortized Complexity Main Idea

Lecture 5: Hashing. David Woodruff Carnegie Mellon University

Algorithms and Data Structures 2016 Week 5 solutions (Tues 9th - Fri 12th February)

Analysis of Algorithms - Midterm (Solutions)

Lecture 1 : Data Compression and Entropy

Solutions. Prelim 2[Solutions]

Graphs and Trees Binary Search Trees AVL-Trees (a,b)-trees Splay-Trees. Search Trees. Tobias Lieber. April 14, 2008

Insert Sorted List Insert as the Last element (the First element?) Delete Chaining. 2 Slide courtesy of Dr. Sang-Eon Park

Ecient and improved implementations of this method were, for instance, given in [], [7], [9] and [0]. These implementations contain more and more ecie

Data Structures 1 NTIN066

ECE608 Midterm 1 Spring 2009 THEN PUT YOUR ADDRESS ON EVERY PAGE OF THE EXAM! NAME: Neatness counts. We will not grade what we cannot read.

data structures and algorithms lecture 2

On my honor I affirm that I have neither given nor received inappropriate aid in the completion of this exercise.

CSE 202: Design and Analysis of Algorithms Lecture 3

Data Structures and and Algorithm Xiaoqing Zheng

Prelim 2[Solutions] Solutions. 1. Short Answer [18 pts]

8: Splay Trees. Move-to-Front Heuristic. Splaying. Splay(12) CSE326 Spring April 16, Search for Pebbles: Move found item to front of list

Math Models of OR: Branch-and-Bound

CPSC 320 Sample Final Examination December 2013

CS 161 Summer 2009 Homework #2 Sample Solutions

Weak Heaps and Friends: Recent Developments

Single Source Shortest Paths

Inge Li Gørtz. Thank you to Kevin Wayne for inspiration to slides

2-INF-237 Vybrané partie z dátových štruktúr 2-INF-237 Selected Topics in Data Structures

Data Structures and Algorithms Winter Semester

Module 1: Analyzing the Efficiency of Algorithms

University of New Mexico Department of Computer Science. Final Examination. CS 561 Data Structures and Algorithms Fall, 2013

Chapter 8 The Disjoint Sets Class

CSE 373: Data Structures and Algorithms Pep Talk; Algorithm Analysis. Riley Porter Winter 2017

Problem Set 4 Solutions

ECEN 689 Special Topics in Data Science for Communications Networks

Randomization in Algorithms and Data Structures

Sorting Algorithms. We have already seen: Selection-sort Insertion-sort Heap-sort. We will see: Bubble-sort Merge-sort Quick-sort

Problem: Data base too big to fit memory Disk reads are slow. Example: 1,000,000 records on disk Binary search might take 20 disk reads

Outline. Computer Science 331. Cost of Binary Search Tree Operations. Bounds on Height: Worst- and Average-Case

Binary Search Trees. Dictionary Operations: Additional operations: find(key) insert(key, value) erase(key)

CSE 4502/5717 Big Data Analytics Spring 2018; Homework 1 Solutions

/463 Algorithms - Fall 2013 Solution to Assignment 4

CMPS 6610 Fall 2018 Shortest Paths Carola Wenk

16. Binary Search Trees. [Ottman/Widmayer, Kap. 5.1, Cormen et al, Kap ]

Algorithms Theory. 08 Fibonacci Heaps

Minimizing the Number of Tardy Jobs

Discrete Event Simulation. Motive

Transcription:

Priority queues implemented via heaps Comp Sci 1575 Data s

Outline 1 2 3

Outline 1 2 3

Priority queue: most important first Recall: queue is FIFO A normal queue data structure will not implement a priority queue efficiently because search for the element with highest priority will take Θ(n) time. A list, whether sorted or not, will also require Θ(n) time for either or removal. A BST that organizes records by priority could be used, with the total of n inserts and n remove operations requiring Θ(n log n) time in the average case. How could we design and sort a tree so that the highest priority items are most quickly accessible?

Outline 1 2 3

Heaps must be complete trees By comparison, any given BST can be complete, but a heap is required to be (at, deletion, and construction)

Outline 1 2 3

Heaps are sorted top to bottom Value in a node the values in the node s children (Max heap) Alternatively, min heap has minimum at top root BST is full ordering (left to right, and thus top to bottom also), while heap is partial ordering (just top to bottom, but not left/right); no sibling relationships specified. Not as good as BST for finding an arbitrary value in the collection; heap would be O(n) for that, but better for finding most extreme value; merely O(1) Like normal queue, not interested in finding an arbitrary value.

Outline 1 2 3

Outline 1 2 3

Reminder: trees as arrays

The total number of nodes in the tree is n. The index of the node in question is r, which must fall in the range 0 to n - 1.... How fast are indices?

in heaps Which indices are important for a heap? Which nodes elements need to be compared?

First element can be 0 or 1 for easy indexing

in heaps If you scoot the first element back to 1, then for any element in array position i: the left child is in position 2i, the right child is in the cell after the left child (2i + 1), the parent is in position i/2 Heap array formal definition: Node than its children With array starting at 0, and i being each node: heap[i] heap[2 i + 1], for 0 i n 1 2 heap[i] heap[2 i + 2], for 0 i n 2 2

schemes Let n be the number of elements in the heap and i be an arbitrary valid index of the array storing the heap. If the tree root is at index 0, with valid indices 0 through n 1, then each element a at index i has children at indices 2i + 1 and 2i + 2 its parent at index floor((i 1)/2). If the tree root is at index 1, with valid indices 1 through n, then each element a at index i has children at indices 2i and 2i + 1 its parent at index floor(i/2).

Outline 1 2 3

Priority queue as heap What are the main operations we need in this queue?

Outline 1 2 3

What should enqueue do? What steps does is require? Draw it out.

: high level design first 1 Place the new entry in the heap in the first available location. This keeps the structure as a complete binary tree. However, it might no longer be a heap, since the new entry might have a higher value than its parent 2 while (new entry has priority that is higher than its parent) swap the new entry with its parent

Inserting 15: (a) Put 15 at end; (b) Swap 15 with 7 (c) Swap 15 with 10; (d) Done!

and sift up Place x i n heap i n f i r s t a v a i l a b l e l o c a t i o n ( to m a i n t a i n a complete b i n a r y t r e e ). while ( x > p a r e n t ) Swap x with i t s p a r e n t Stop when x becomes r o o t or when p a r e n t i s no l o n g e r < x heap ( e1 ) put e1 at the end o f heap ; while e1 i s not the r o o t and e1 > p a r e n t ( e1 ) swap e1 with i t s p a r e n t

and sift up 14 Walk through steps

Engueue 15 1 add element 2 swap 15 with 8 3 swap 15 with 11 What is Θ for this function?

Outline 1 2 3

What should dequeue do? What steps does it require? Draw it out

: high level design first 1 Copy the entry at the root of the heap to the variable that is used to return a value 2 Copy the last entry in the deepest level to the root, and take that last node out of the tree. This entry is now out of place 3 while(the out of place entry has a priority that is lower than any of its children) swap the out of place entry with its highest priority child Why highest priority child?

root (which happens to be 20) (a) Remove root (20) and move end (6) to root; (b) Swap 6 with larger child (15); (c) Move end (6) to root ; (d) Done!

and sift Remove r i g h t m o s t d e e p e s t e n t r y ; c a l l i t x. Make x the new r o o t. while ( x < one o f i t s c h i l d r e n ) ) swap x with i t s l a r g e s t c h i l d Stop when x becomes a l e a f or when x i s no l o n g e r < one o f i t s c h i l d r e n heap ( ) e x t r a c t the element from the r o o t ; move element from l a s t l e a f to i t s p l a c e ; remove the l a s t l e a f ; p = the r o o t ; while p i s not a l e a f and p < i t s c h i l d r e n swap p with the l a r g e r c h i l d ;

(13) and sift (31) (Note: min-heap)

root (11) 1 Copy root 2 Overwrite root with last 3 swap What is Θ for this function?

Outline 1 2 3

Building heaps How to build a heap from a randomly sorted complete tree?

Heapify via repeated Θ(n log n)

Sift : modular method Sift was already part of siftdown (A, i ) : l e f t = 2 i r i g h t = 2 i + 1 l a r g e s t = i i f l e f t <= h e a p l e n g t h [A] and A[ l e f t ] > A[ l a r g e s t ] : l a r g e s t = l e f t i f r i g h t <= h e a p l e n g t h [A] and A[ r i g h t ] > A[ l a r g e s t ] : l a r g e s t = r i g h t i f l a r g e s t!= i then : swap A[ i ] and A[ l a r g e s t ] siftdown (A, l a r g e s t ) For the above algorithm to re-heapify the array, the node at index i and at least one of its direct children must violate the heap property. If they do not, the algorithm will fall through with no change to the array.

Sift : modular method Sift 1 : Swap 1 (element 0) with 7 (larger child) Why higher priority child? Swap 1 (element 2) with 6 (larger child) Would it be a heap if we promoted lower priority child? What about reorganizing the whole array? Can we repeatedly apply sift to randomly sorted complete to form a heap? How do we cover all nodes? Where do we start?

Build via repeated sift- buildmaxheap (A ) : // heap i s s e t to same s i z e as a r r a y h e a p l e n g t h [A] = l e n g t h [A] // g o i n g backwards f o r each i n d e x i from f l o o r ( l e n g t h [A] / 2 ) to 1 do : siftdown (A, i ) Which node does this start with? Which node does it end with? What is Θ for this function?

Outline 1 2 3

Some housekeeping are also helpful (see heap.cpp)

Outline 1 2 3

Algorithm Average Worst Case Space O(n) O(n) Search O(n) O(n) Insert O(1) O(log n) Delete O(log n) O(log n) Find-min O(1) O(1) Peek O(1) O(1) Build via repeated insert O(n) O(n log n) Build via repeat sift- O(n) O(n)