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

Similar documents
Sorting and Searching

Sorting and Searching

Binary Search Trees. Nearest Neighbor Binary Search Trees Insertion Predecessor and Successor Deletion Algorithms on Trees.

Binary Search Trees. Binary Search Trees. Nearest Neighbor. Nearest Neighbor

Chapter 6 Heapsort 1

Module 2: Priority Queues

Module 2: Priority Queues

Algorithms and Theory of Computation. Lecture 7: Priority Queue

Priority Queues and Binary Heaps

Binary Heaps in Dynamic Arrays

Module 2: Priority Queues

CS 240 Data Structures and Data Management. Module 2: Priority Queues

CS 240 Data Structures and Data Management. Module 2: Priority Queues

Design and Analysis of Algorithms

Binary search trees. Support many dynamic-set operations, e.g. Search. Minimum. Maximum. Insert. Delete ...

Introduction to Algorithms 3 rd edition

Lower Bound on Comparison-based Sorting

CS711008Z Algorithm Design and Analysis

CS711008Z Algorithm Design and Analysis

CH 8. HEAPS AND PRIORITY QUEUES

CH. 8 PRIORITY QUEUES AND HEAPS

BM267 - Introduction to Data Structures

Balanced search trees

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

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

Heaps. 2/13/2006 Heaps 1

Comparisons. Θ(n 2 ) Θ(n) Sorting Revisited. So far we talked about two algorithms to sort an array of numbers. What is the advantage of merge sort?

Appendix A and B are also worth reading.

Heaps, Heap Sort, and Priority Queues.

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

Definition of a Heap. Heaps. Priority Queues. Example. Implementation using a heap. Heap ADT

Stores a collection of elements each with an associated key value

Comparisons. Heaps. Heaps. Heaps. Sorting Revisited. Heaps. So far we talked about two algorithms to sort an array of numbers

Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

Binary Trees, Binary Search Trees

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;

Weighted Graphs and Greedy Algorithms

CS2 Algorithms and Data Structures Note 6

Binary heaps (chapters ) Leftist heaps

CSE 5311 Notes 4a: Priority Queues

9. Heap : Priority Queue

Data Structures Question Bank Multiple Choice

quiz heapsort intuition overview Is an algorithm with a worst-case time complexity in O(n) data structures and algorithms lecture 3

Data Structures and Algorithms

Dictionaries. Priority Queues

CS350: Data Structures Heaps and Priority Queues

Analysis of Algorithms

CS2 Algorithms and Data Structures Note 6

Chapter 6 Heap and Its Application

Lecture 7. Transform-and-Conquer

Course Review for Finals. Cpt S 223 Fall 2008

CSE2331/5331. Topic 6: Binary Search Tree. Data structure Operations CSE 2331/5331

Heaps. Heapsort. [Reading: CLRS 6] Laura Toma, csci2000, Bowdoin College

Heapsort. Heap data structure

CSC263 Week 2. Larry Zhang

Trees. CSE 373 Data Structures

CSci 231 Final Review

Binary Trees

13.4 Deletion in red-black trees

Properties of a heap (represented by an array A)

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

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

COMP Analysis of Algorithms & Data Structures

PRIORITY QUEUES AND HEAPS

Design and Analysis of Algorithms Lecture- 9: Binary Search Trees

PRIORITY QUEUES AND HEAPS

Round 3: Trees. Tommi Junttila. Aalto University School of Science Department of Computer Science. CS-A1140 Data Structures and Algorithms Autumn 2017

COMP Analysis of Algorithms & Data Structures

CS 380 ALGORITHM DESIGN AND ANALYSIS

CSCI2100B Data Structures Heaps

Technical University of Denmark

The Heap Data Structure

Heaps Goodrich, Tamassia. Heaps 1

Collection of priority-job pairs; priorities are comparable.

1 Tree Sort LECTURE 4. OHSU/OGI (Winter 2009) ANALYSIS AND DESIGN OF ALGORITHMS

CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

Search Tree Applications

Lecture Summary CSC 263H. August 5, 2016

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

( ) + n. ( ) = n "1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1

Section 4 SOLUTION: AVL Trees & B-Trees

AVL Trees. (AVL Trees) Data Structures and Programming Spring / 17

Basic Data Structures (Version 7) Name:

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

COMP Analysis of Algorithms & Data Structures

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Topic: Heaps and priority queues

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

Administration CSE 326: Data Structures

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Transform & Conquer. Presorting

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

Algorithms and Data Structures. (c) Marcin Sydow. Priority Queue. Example Applications. Extensions of. Priority Queue. Binomial Heap.

CSE 373 Data Structures and Algorithms. Lecture 15: Priority Queues (Heaps) III

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

Binary search trees (BST) Binary search trees (BST)

Announcements. Problem Set 2 is out today! Due Tuesday (Oct 13) More challenging so start early!

Programming II (CS300)

Data Structures Lesson 9

CS165: Priority Queues, Heaps

Transcription:

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

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

Priority queues. Maintain dynamic set S supporting the following operations. Each element has key x.key and satellite data x.data. MAX(): return element med largest key. EXTRACTMAX(): return and remove element with largest key. INCREASEKEY(x, k): set x.key = k. (assume k x.key) INSERT(x): set S = S {x} 3 5 6 10 12 20 24 MAX

Applications. Scheduling Shortest paths in graphs (Dijkstra's algorithm) Minimum spanning trees in graphs (Prim's algorithm) Compression (Huffman's algorithm) Challenge. How can we solve problem with current techniques?

Solution 1: Linked list. Maintain S in a doubly-linked list. 7 42 18 23 5 10 56 2 MAX(): linear search for largest key. EXTRACTMAX(): linear search for largest key. Remove and return element. INCREASEKEY(x, k): set x.key = k. INSERT(x): add element to front of list (assume element does not exist in S beforehand). Time. MAX and EXTRACTMAX in O(n) time (n = S ). INCREASEKEY and INSERT in O(1) time. Space. O(n).

Solution 2: Sorted linked list. Maintain S in a sorted doubly-linked list. 56 42 23 18 10 7 5 2 MAX(): return first element. EXTRACTMAX(): return og remove first element. INCREASEKEY(x, k): set x.key = k. Linear search to move x to correct position. INSERT(x): linear search to insert x at correct position. Time. MAX and EXTRACTMAX in O(1) time. INCREASEKEY and INSERT in O(n) time. Space. O(n).

Data structure MAX EXTRACTMAX INCREASEKEY INSERT Space linked list O(n) O(n) O(1) O(1) O(n) sorted linked list O(1) O(1) O(n) O(n) O(n) Challenge. Can we do significantly better?

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

Trees Rooted trees. Nodes (or vertices) connected with edges. Connected and acyclic. Designated root node. Special type of graph. Terminology. Children, parent, descendant, ancestor, leaves, internal nodes, path,.. Depth and height. Depth of v = length of path from v to root. Height of v = length of path from v to descendant leaf. Depth of T = height of T = length of longest path from root to a leaf.

Trees Binary tree. Rooted tree. Each node has at most two children called the left child and right child Complete binary tree. Binary tree where all levels of tree are full. Almost complete binary tree. Complete binary tree with 0 or more rightmost leaves deleted. Lemma. Height af an (almost) complete binary tree with n nodes is Θ(log n). Pf. See exercises.

Heaps Heaps. Almost complete binary tree that satisfies heap-order. Heap-order. All nodes store one element. For all nodes v. all keys in left subtree and right subtree are v.key. 7 11 31 20 16 3 5 2 9 12 13 10 Max-heap vs min-heap. v v.key v.key

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

Heap Data structure. We need the following navigation operations on a heap. PARENT(x): return parent of x. LEFT(x) : return left child of x. RIGHT(x): return right child of x. Challenge. How can we represent a heap compactly to support fast navigation?

Heap Linked representation. Each node stores v.key v.parent v.left v.right PARENT, LEFT, RIGHT by following pointer. 7 11 31 20 16 3 5 2 9 12 13 10 Time. O(1) Space. O(n) root null 31 20 16 7 11 13 null null 10 null 3 null null 5 null null null 2 null null 9 null null 12 null

Heap Array representation. Array H[0..n] H[0] unused H[1..n] stores nodes in level order. PARENT(x): return x/2 LEFT(x) : return 2x. RIGHT(x): return 2x + 1 7 11 31 3 5 2 9 12 1 20 16 2 3 13 10 4 5 6 7 8 9 10 11 12 Time. O(1) Space. O(n) 0 1 2 3 4 5 6 7 8 9 10 11 12-31 20 16 7 11 13 10 3 5 2 9 12-31 20 16 7 11 13 10 3 5 2 9 12

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

Algorithms on Heaps BUBBLEUP(X): If heap order is violated at node x because key is larger than key at parent: Swap x and parent Repeat with parent until heap order is satisfied. 20 42 3 5 BUBBLEDOWN(x): If heap order is violated at node x because key is smaller than key at left or right child: Swap x and child c with largest key. Repeat with child until heap order is satisfied. 20 1 3 5

Algorithms on Heaps BUBBLEUP(X): If heap order is violated at node x because key is larger than key at parent: Swap x and parent Repeat with parent until heap order is satisfied. BUBBLEDOWN(x): If heap order is violated at node x because key is smaller than key at left or right child: Swap x and child c with largest key. Repeat with child until heap order is satisfied. Time. BUBBLEUP and BUBBLEDOWN in Θ(log n) time. How can we use them to implement a priority queue? 20 42 3 5 20 1 3 5

MAX() return H[1] EXTRACTMAX() r = H[1] H[1] = H[n] n = n - 1 BUBBLEDOWN(1) return r INSERT(x) n = n + 1 H[n] = x BUBBLEUP(n) INCREASEKEY(x,k) H[x] = k BUBBLEUP(x) 20 16 7 11 31 3 5 2 9 12 1 2 3 13 10 4 5 6 7 8 9 10 11 12 Ex. Trace execution of following sequence in initially empty heap: 2, 5, 7, 6, 4, E, E Numbers mean INSERT og E is EXTRACTMAX. Draw heap after each operation. 0 1 2 3 4 5 6 7 8 9 10 11 12-31 20 16 7 11 13 10 3 5 2 9 12-31 20 16 7 11 13 10 3 5 2 9 12

MAX() return H[1] EXTRACTMAX() r = H[1] H[1] = H[n] n = n - 1 BUBBLEDOWN(1) return r INSERT(x) n = n + 1 H[n] = x BUBBLEUP(n) INCREASEKEY(x,k) H[x] = k BUBBLEUP(x) 20 16 7 11 31 3 5 2 9 12 1 2 3 13 10 4 5 6 7 8 9 10 11 12 Time. MAX in Θ(1) time. EXTRACTMAX, INCREASEKEY, and INSERT in Θ(log n) time. 0 1 2 3 4 5 6 7 8 9 10 11 12-31 20 16 7 11 13 10 3 5 2 9 12-31 20 16 7 11 13 10 3 5 2 9 12

Data structure MAX EXTRACTMAX INCREASEKEY INSERT Space linked list O(n) O(n) O(1) O(1) O(n) sorted linked list O(1) O(1) O(n) O(n) O(n) heap O(1) O(log n) O(log n) O(log n) O(n) Heaps with array data structure is an example of an implicit data structure.

Prioritetskøer Prioritetskøer Træer og hobe Repræsentation af hobe Algoritmer på hobe Hobkonstruktion Hobsortering

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

Building a Heap Building a heap. Given n integers in a array H[0..n], convert array to a heap. 31 1 20 16 2 3 7 11 13 10 4 5 6 7 3 5 2 9 12 8 9 10 11 12 0 1 2 3 4 5 6 7 8 9 10 11 12-20 7 16 9 12 13 10 5 3 2 11 31 0 1 2 3 4 5 6 7 8 9 10 11 12-31 20 16 7 11 13 10 3 5 2 9 12

Building a Heap Solution 1: top-down construction For all nodes in increasing level order apply BUBBLEUP. Time. For each node of depth d, we use O(d) time. 1 node of depth 0, 2 nodes of depth 1, 4 nodes of depth 2,..., ~n/2 nodes of depth log n. total time is Θ(n log n) Challenge. Can we do better?

Building a Heap Solution 2: bottom-up construction For all nodes in decreasing level order apply BUBBLEDOWN. Time. For each node of height h we use O(h) time. 1 node of height log n, 2 nodes of height log n - 1,..., n/4 nodes of height 1, n/2 nodes of height 0. total time is Θ(n) (see exercise)

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

Heapsort Sorting. How can we sort an array H[1..n] using a heap? Solution. Build a heap for H. Apply n EXTRACTMAX. Insert results in the end of array. Return H. 31 1 20 16 2 3 7 11 13 10 4 5 6 7 3 5 2 9 12 8 9 10 11 12 Time. Heap construction in Θ(n) time. n EXTRACTMAX in Θ(nlog n) time. total time is Θ(nlog n). 0 1 2 3 4 5 6 7 8 9 10 11 12-31 20 16 7 11 13 10 3 5 2 9 12

Heapsort Theorem. We can sort an array in Θ(n log n) time. Uses only O(1) extra space. In-place sorting algorithm. Equivalence of sorting and priority queues. 31 1 20 16 2 3 7 11 13 10 4 5 6 7 3 5 2 9 12 8 9 10 11 12 0 1 2 3 4 5 6 7 8 9 10 11 12-31 20 16 7 11 13 10 3 5 2 9 12

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