CMSC 341 Lecture 19.

Slides:



Advertisements
Similar presentations
§6 Leftist Heaps CHAPTER 5 Graph Algorithms  Heap: Structure Property + Order Property Target : Speed up merging in O(N). Leftist Heap: Order Property.
Advertisements

DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI.
COL 106 Shweta Agrawal and Amit Kumar
Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
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.
BY Lecturer: Aisha Dawood. Heapsort  O(n log n) worst case like merge sort.  Sorts in place like insertion sort.  Combines the best of both algorithms.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Heaps and heapsort COMP171 Fall 2005 Part 2. Sorting III / Slide 2 Heap: array implementation Is it a good idea to store arbitrary.
Priority Queues, Heaps & Leftist Trees
CSC 172 DATA STRUCTURES. Priority Queues Model Set with priorities associatedwith elements Priorities are comparable by a < operator Operations Insert.
Heapsort Based off slides by: David Matuszek
Leftist Trees Linked binary tree. Can do everything a heap can do and in the same asymptotic complexity.  insert  remove min (or max)  initialize Can.
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.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Data Structure & Algorithm II.  Delete-min  Building a heap in O(n) time  Heap Sort.
Chapter 21 Binary Heap.
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
Priority Queues (Heaps)
Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue. Nov 4,
Review for Exam 2 Topics covered (since exam 1): –Splay Tree –K-D Trees –RB Tree –Priority Queue and Binary Heap –B-Tree For each of these data structures.
8 January Heap Sort CSE 2011 Winter Heap Sort Consider a priority queue with n items implemented by means of a heap  the space used is.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
1 Heap Sort. A Heap is a Binary Tree Height of tree = longest path from root to leaf =  (lgn) A heap is a binary tree satisfying the heap condition:
Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Leftist Trees Linked binary tree.
Lecture 2 Sorting.
Heaps, Heap Sort and Priority Queues
Topics covered (since exam 1):
Binary Heaps Priority Queues
October 30th – Priority QUeues
Source: Muangsin / Weiss
Bohyung Han CSE, POSTECH
CMSC 341 Lecture 13 Leftist Heaps
Review for Final Exam Non-cumulative, covers material since exam 2
The Heap Data Structure
CMSC 341: Data Structures Priority Queues – Binary Heaps
Initializing A Max Heap
Part-D1 Priority Queues
Binary Heaps Priority Queues
Data Structures & Algorithms Priority Queues & HeapSort
Heapsort Heap & Priority Queue.
Section 10 Questions Heaps.
Draw pictures to indicate the subproblems middleMax solves at each level and the resulting maxPtr and PrevPtr for each on this linked list:
Topics covered (since exam 1):
CMSC 341 Lecture 14 Priority Queues & Heaps
Heaps 11/27/ :05 PM Heaps Heaps.
Heaps and the Heapsort Heaps and priority queues
Tree Representation Heap.
Heaps A heap is a binary tree.
Binary Heaps Priority Queues
Heap Sort The Heap Data Structure
Heap Sort CSE 2011 Winter January 2019.
HEAPS.
Algorithms: Design and Analysis
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Topics covered (since exam 1):
CS 6310 Advanced Data Structure Wei-Shian Wang
Computer Algorithms CISC4080 CIS, Fordham Univ.
A Heap Is Efficiently Represented As An Array
EE 312 Software Design and Implementation I
Presentation transcript:

CMSC 341 Lecture 19

Announcements Expect graded exams on Wed Truncated office hours today (until 1:30) Proj5 up today, example solution later

Constructing a Binary Heap A BH can be constructed in O(n) time. Suppose an array in arbitrary order. It can be put in heap order in O(n) time. Create the array and store n elements in it in arbitrary order. O(n) Heapify the array start at vertex i = n/2 percolateDown(i) repeat for all vertices down to i Example: Insert in level order: 18, 2, 13, 10, 15, 3, 7, 16, 8 n = 9, n/2 = 4 siftDown(4) -- 10 moves down siftDown(3) -- 13 moves down siftDown(2) -- no action required siftDown(1) -- 18 moves down

BinaryHeap.C (cont) template <class Comparable> void BinaryHeap<Comparable>::buildHeap() { for(int i = currentSize/2; i >0; i--) percolateDown(i); }

Performance of Construction A CBT has 2h-1 vertices on level h-1. On level h-l, at most 1 swap is needed per node. On level h-2, at most 2 swaps are needed. … On level 0, at most h swaps are needed. Number of swaps = S = 2h*0 + 2h-1*1 + 2h-2*2 + … + 20*h = = h(2h+1-1) - ((h-1)2h+1+2) = 2h+1(h-(h-1))-h-2 = 2h+1-h-2

Performance of Construction (cont) But 2h+1-h-2 = O(2h) But n = 1 + 2 + 4 + … + 2h = Therefore, n = O(2h) So S = O(n) A heap of n vertices can be built in O(n) time.

Heap Sort Given n values, can sort in O(n log n) time (in place). Insert values into array -- O(n) heapify -- O(n) repeatedly delete min -- O(lg n) n times Using a min heap, this code sorts in reverse order. With a max heap, it sorts in normal order. for (i = n-1; i >= 1; i--) { x =findMin(); deleteMin(); A[i+1] = x; } Sort: 2, 10, 3, 13, 17, 5, 9

Limitations Binary heaps support insert, findMin, deleteMin, and construct efficiently. They do not efficiently support the meld or merge operation in which 2 PQs are merged into one. If P1 and P2 are of size n1 and n2, then the merge is in O(n1 + n2)

Leftist Heap Supports findMin -- O(1) deleteMin -- O(lg n) insert -- O(lg n) construct -- O(n) merge -- O(lg n)

Leftist Tree A LT is a binary tree in which at each vertex v, the path length, dr, from v’s right child to the nearest non-full vertex is not larger than that from the vertex’s left child to the nearest non-full vertex. An important property of leftist trees: At every vertex, the shortest path to a non-full vertex is along the rightmost path. Suppose this was not true. Then, at the same vertex the path on the left would be shorter than the path on the right.

Leftist Heap A leftist heap is a leftist tree in which the values in the vertices obey heap order (the tree is partially ordered). Since a LH is not necessarily a CBT we do not implement it in an array. An explicit tree implementation is used. Operations findMin -- return root value, same as BH deleteMin -- done using meld operation insert -- done using meld operation construct -- done using meld operation

Meld Algorithm: Meld (H1, H2) { if (!root(H1) || (root_value(H1) > root_value(H2) ) swap (H1, H2) if (root(H1) != NULL)) right(H1) <-- Meld(right(H1),H2) if (left_length(H1) < right_length(H1) swap(left(H1), right(H1); }

Meld (cont) Performance: O(lg n) the rightmost path of each tree has at most lg(n+1) vertices. So O(lg n) vertices will be involved.

Leftist Heap Operations Other operations implemented in terms of Meld insert (item) make item into a 1-vertex LH, X Meld(*this, X) deleteMin Meld(left subtree, right subtree) construct from N items make N LH from the N values, one element in each meld each in one at a time : use queue and build pairwise :

LH Construct Algorithm: make N heaps each with one data value Queue Q; for (I=1; I <= N; I++) Q.Enqueue(Hi); Heap H = Q.Dequeue(); while (!Q.IsEmpty()) Q.Enqueue(meld(H,Q.Dequeue()); H = Q.Dequeue(); Performance first time: N/2 melds, height = 1 second time: N/4 melds, height = 2 = N/2 + 2N/4 + 3N/8 + … 1 logN = O(N)