Algorithms and Theory of Computation. Lecture 7: Priority Queue

Similar documents
Sorting and Searching

Sorting and Searching

CS711008Z Algorithm Design and Analysis

CS711008Z Algorithm Design and Analysis

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

Chapter 6 Heapsort 1

Heaps Goodrich, Tamassia. Heaps 1

Heaps. 2/13/2006 Heaps 1

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

Heaps 2. Recall Priority Queue ADT. Heaps 3/19/14

Chapter 5 Data Structures Algorithm Theory WS 2016/17 Fabian Kuhn

Priority Queues. T. M. Murali. January 23, T. M. Murali January 23, 2008 Priority Queues

Priority Queues. T. M. Murali. January 29, 2009

Binary Heaps in Dynamic Arrays

CH 8. HEAPS AND PRIORITY QUEUES

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs

CH. 8 PRIORITY QUEUES AND HEAPS

Topic: Heaps and priority queues

Priority Queues. T. M. Murali. January 26, T. M. Murali January 26, 2016 Priority Queues

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

Properties of a heap (represented by an array A)

Recall: Properties of B-Trees

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

Design and Analysis of Algorithms

2pt 0em. Computer Science & Engineering 423/823 Design and Analysis of Algorithms. Lecture 04 Minimum-Weight Spanning Trees (Chapter 23)

Priority Queues. Meld(Q 1,Q 2 ) merge two sets

Binary heaps (chapters ) Leftist heaps

Priority Queues. Priority Queues Trees and Heaps Representations of Heaps Algorithms on Heaps Building a Heap Heapsort.

Heaps Outline and Required Reading: Heaps ( 7.3) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Lecture 5 Using Data Structures to Improve Dijkstra s Algorithm. (AM&O Sections and Appendix A)

Priority Queues and Binary Heaps

CSE 548: Analysis of Algorithms. Lectures 14 & 15 ( Dijkstra s SSSP & Fibonacci Heaps )

Height of a Heap. Heaps. 1. Insertion into a Heap. Heaps and Priority Queues. The insertion algorithm consists of three steps

Partha Sarathi Manal

Dictionaries. Priority Queues

Priority Queues. Lecture15: Heaps. Priority Queue ADT. Sequence based Priority Queue

Stores a collection of elements each with an associated key value

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap

Heapsort. Heap data structure

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Priority Queues and Heaps. Heaps and Priority Queues 1

CS 310 Advanced Data Structures and Algorithms

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

What is a minimal spanning tree (MST) and how to find one

Algorithm Analysis Graph algorithm. Chung-Ang University, Jaesung Lee

Heaps and Priority Queues

BM267 - Introduction to Data Structures

Algorithms for Minimum Spanning Trees

Heaps. (our first advanced data structure)

Minimum Spanning Trees

Lecture Summary CSC 263H. August 5, 2016

managing an evolving set of connected components implementing a Union-Find data structure implementing Kruskal s algorithm

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs

Analysis of Algorithms

l Heaps very popular abstract data structure, where each object has a key value (the priority), and the operations are:

Lower Bound on Comparison-based Sorting

CSE 241 Class 17. Jeremy Buhler. October 28, Ordered collections supported both, plus total ordering operations (pred and succ)

Trees (Part 1, Theoretical) CSE 2320 Algorithms and Data Structures University of Texas at Arlington

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

Data Structures and Algorithms for Engineers

CSE 5311 Notes 4a: Priority Queues

Priority Queues Heaps Heapsort

CS4800: Algorithms & Data Jonathan Ullman

Fibonacci Heaps Priority Queues. John Ross Wallrabenstein

Week 9 Student Responsibilities. Mat Example: Minimal Spanning Tree. 3.3 Spanning Trees. Prim s Minimal Spanning Tree.

CSE 431/531: Analysis of Algorithms. Greedy Algorithms. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

Questions from the material presented in this lecture

Algorithm Theory, Winter Term 2015/16 Problem Set 5 - Sample Solution

CSCI2100B Data Structures Heaps

Priority queues. Priority queues. Priority queue operations

Data Structures and Algorithms

Introduction to Algorithms 3 rd edition

Collection of priority-job pairs; priorities are comparable.

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

Priority Queue ADT ( 7.1) Heaps and Priority Queues 2. Comparator ADT ( 7.1.4) Total Order Relation. Using Comparators in C++

Algorithms and Theory of Computation. Lecture 3: Graph Algorithms

Priority Queues Heaps Heapsort

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

Computer Science 210 Data Structures Siena College Fall Topic Notes: Priority Queues and Heaps

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

1 Heaps. 1.1 What is a heap? 1.2 Representing a Heap. CS 124 Section #2 Heaps and Graphs 2/5/18

CS106X Programming Abstractions in C++ Dr. Cynthia Bailey Lee

Chapter 2: Basic Data Structures

Heap Model. specialized queue required heap (priority queue) provides at least

Priority Queues. Outline. COMP9024: Data Structures and Algorithms. Priority Queue ADT. Total Order Relations. Entry ADT

403: Algorithms and Data Structures. Heaps. Fall 2016 UAlbany Computer Science. Some slides borrowed by David Luebke

CS521 \ Notes for the Final Exam

Week 12: Minimum Spanning trees and Shortest Paths

CS Final - Review material

Adding a Node to (Min) Heap. Lecture16: Heap Sort. Priority Queue Sort. Delete a Node from (Min) Heap. Step 1: Add node at the end

CSE 100 Minimum Spanning Trees Prim s and Kruskal

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Greedy Algorithms. Textbook reading. Chapter 4 Chapter 5. CSci 3110 Greedy Algorithms 1/63

Outline. Computer Science 331. Heap Shape. Binary Heaps. Heap Sort. Insertion Deletion. Mike Jacobson. HeapSort Priority Queues.

Thus, it is reasonable to compare binary search trees and binary heaps as is shown in Table 1.

l So unlike the search trees, there are neither arbitrary find operations nor arbitrary delete operations possible.

Transcription:

Algorithms and Theory of Computation Lecture 7: Priority Queue Xiaohui Bei MAS 714 September 5, 2017 Nanyang Technological University MAS 714 September 5, 2017 1 / 15

Priority Queues Priority Queues Store a set S of n elements, where each element v S has an associated real/integer key k(v), with the following operations: 1 Make-Queue: create an empty queue 2 Find-Min: find the minimum key in S 3 Extract-Min: remove v S with the smallest key and return it 4 Add(v, k(v)): add new element v with key k(v) to S 5 Delete(v): remove element v from S 6 Decrease-Key(v, k (v)): decrease key of v from k(v) to k (v) Decrease-Key is implemented via delete and insert. Nanyang Technological University MAS 714 September 5, 2017 2 / 15

Priority Queues Applications: Prim s MST algorithm Dijkstra s shortest-path algorithm heapsort online median etc. Implementations: binary heaps d-ary heaps binomial heaps Fibonacci heaps Nanyang Technological University MAS 714 September 5, 2017 3 / 15

Complete Binary Tree A binary tree is a tree in which each vertex has at most two children. A complete tree is a binary tree such that every level, except possibly the last level, is completely filled, and all vertices are as far left as possible. Property The depth of a complete binary tree with n vertices is log 2 n. Proof. The depth increases (by 1) only when n is a power of 2. Nanyang Technological University MAS 714 September 5, 2017 4 / 15

Binary Tree in Nature Nanyang Technological University MAS 714 September 5, 2017 5 / 15

Binary Heap Binary Heap A Binary Heap is a complete binary tree such that for every element v, at a vertex i, the element w at i s parent satisfies k(w) k(v). parent 6 child 10 child 8 12 18 11 25 21 17 19 Nanyang Technological University MAS 714 September 5, 2017 6 / 15

Explicit Implementation Pointer representation. Vertex has a pointer to parent and two children. maintain number of elements n maintain pointer to root vertex root 6 10 8 12 18 11 25 21 17 19 Nanyang Technological University MAS 714 September 5, 2017 7 / 15

Implicit Implementation Array representation. Indices start at 1. take vertices in level order parent of vertex at k is at k/2 children of vertex at k are at 2k and 2k + 1 6 10 8 12 18 11 25 21 17 19 1 2 3 4 5 6 7 8 9 10 Nanyang Technological University MAS 714 September 5, 2017 8 / 15

Binary Heap: Insert Insert. Add element in new vertex at end; repeatedly exchange new element with element in its parent until heap order is restored. 6 10 7 8 12 18 710 11 25 21 17 19 18 7 Figure: swim up Time = O(log n) Nanyang Technological University MAS 714 September 5, 2017 9 / 15

Binary Heap: Extract-Min Extract-Min. Exchange element in root vertex with last vertex; repeatedly exchange element in root with its smaller child until heap order is restored. 18 67 18 10 7 8 12 10 18 11 25 21 17 19 18 6 Figure: sink down Time = O(log n) Nanyang Technological University MAS 714 September 5, 2017 10 / 15

Binary Heap: Delete and Find-Min Delete. Exchange the element in this vertex with the last vertex; either swim up or sink down the vertex until heap order is restored. Time = O(log n) Decrease-Key. Repeatedly swim up the vertex until heap order is restored. Time = O(log n) Find-Min. Return the element in the root vertex. Time = O(1) Nanyang Technological University MAS 714 September 5, 2017 11 / 15

Binary Heap: Analysis Dijkstra a Algorithm with a priority queue O(n) Insert operations O(n) Extract-Min operations O(m) Decrease-Key operations Dijkstra s Algorithm Dijkstras algorithm can be implemented in O((n + m) log n) time. Nanyang Technological University MAS 714 September 5, 2017 12 / 15

Heapify Heapify. Given n elements, construct a binary heap containing them. Can be done in O(n log n) time by inserting each element. Bottom-up Method. For i = n to 1, repeatedly exchange the element in vertex i with its smaller child until subtree rooted at i is heap-ordered. 83 12 7 93 8 12 11 7 21 15 39 26 14 11 12 15 21 22 Nanyang Technological University MAS 714 September 5, 2017 13 / 15

Heapify Theorem Given n elements, a binary heap containing those n elements can be constructed in O(n) time. Intuition: There are at most n/2 h+1 vertices of height h. The amount of work to sink a vertex is proportional to its height h. Nanyang Technological University MAS 714 September 5, 2017 14 / 15

More on Dijkstra and Priority Queues 1 Dijkstra s algorithm can be implemented in O(n log n + m) time using Fibonacci Heaps. If m = Ω(nlogn), running time is linear in input size. 2 Data structures are complicated to analyze/implement. Recent work has obtained data structures that are easier to analyze and implement, and perform well in practice. Rank-Pairing Heaps (European Symposium on Algorithms, September 2009) 3 Dijkstra s algorithm gives shortest paths from s to all vertices in V. Nanyang Technological University MAS 714 September 5, 2017 15 / 15