Binary heaps and Heapsort. Priority Queues n The smallest element is removed item = remove();item = remove(); 17 24 81 12 5 7 17 12 8 Priority Queue Minimum.

Slides:



Advertisements
Similar presentations
DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI.
Advertisements

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.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
Priority Queues. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Heapsort CIS 606 Spring Overview Heapsort – O(n lg n) worst case—like merge sort. – Sorts in place—like insertion sort. – Combines the best of both.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Computer Science and Software Engineering University of Wisconsin - Platteville 12. Heap Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++ Plus.
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
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.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Data Structure & Algorithm II.  Delete-min  Building a heap in O(n) time  Heap Sort.
Chapter 21 Binary Heap.
Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
1 Heaps (Priority Queues) You are given a set of items A[1..N] We want to find only the smallest or largest (highest priority) item quickly. Examples:
DATA STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap.
Priority Queues (Heaps)
CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Kevin Quinn Fall 2015.
CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010.
Heaps and Heapsort Prof. Sin-Min Lee Department of Computer Science San Jose State University.
Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things.
Heaps & Priority Queues
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
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:
Internal and External Sorting External Searching
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
Priority Queues and Heaps Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
Priority Queues CS 110: Data Structures and Algorithms First Semester,
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
Priority Queues CS /02/05 L7: PQs Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
CSE373: Data Structures & Algorithms Priority Queues
CSE373: Data Structures & Algorithms
CS 201 Data Structures and Algorithms
October 30th – Priority QUeues
Source: Muangsin / Weiss
Bohyung Han CSE, POSTECH
Priority Queues (Heaps)
7/23/2009 Many thanks to David Sun for some of the included slides!
CMSC 341: Data Structures Priority Queues – Binary Heaps
i206: Lecture 14: Heaps, Graphs intro.
Heaps and the Heapsort Heaps and priority queues
Ch. 8 Priority Queues And Heaps
CE 221 Data Structures and Algorithms
Priority Queues Chapters 10 & 26.
Priority Queues (Heaps)
Priority Queues CSE 373 Data Structures.
Heaps By JJ Shepherd.
Heaps & Multi-way Search Trees
Heaps.
Presentation transcript:

Binary heaps and Heapsort

Priority Queues n The smallest element is removed item = remove();item = remove(); Priority Queue Minimum element is 5

Priority Queues n The smallest element is removed item = remove();item = remove(); Priority Queue Minimum element is 7

Priority Queues n Additions made wherever add(15);add(15); Priority Queue Minimum element is

List Representations n Represent P.Q. as a sorted list insert item into the appropriate position = O(N)insert item into the appropriate position = O(N) remove item at one end = O(1)remove item at one end = O(1) n Represent P.Q. as an unsorted list insert at one end of list = O(1)insert at one end of list = O(1) remove by finding minimum & deleting = O(N)remove by finding minimum & deleting = O(N) n Which to prefer?

List Representations n Sorted Rep Insert 14Insert 14 Remove-HighestRemove-Highest n Unsorted Rep

A Simpler Representation n Keep people semi-sorted by importance most important person right at frontmost important person right at front people near front are smallerpeople near front are smaller people near back are biggerpeople near back are bigger each person can have two people behindeach person can have two people behind »one to left, one to right n People arranged in rows: 1 in 1 st row, 2 in 2 nd row, 4 in 3 rd, 8 in 4 th, …1 in 1 st row, 2 in 2 nd row, 4 in 3 rd, 8 in 4 th, …

A “Heap” of People n Most important: 1 st row 2 nd row less important than 1 st2 nd row less important than 1 st 3 rd row less important than 2 nd3 rd row less important than 2 nd and so onand so on n Each person can have 2 more people behind the two people behind are less important (bigger) than the person in front of themthe two people behind are less important (bigger) than the person in front of them

Heap Order Property n Highest priority item at root here that’s the 5here that’s the 5 n Left & right children also heap ordered highest priority in that sub-tree at its roothighest priority in that sub-tree at its root here: 7 on left & 12 on righthere: 7 on left & 12 on right n Note: 5’s children are not next two smallest they could have been, but aren’t necessarilythey could have been, but aren’t necessarily

Duplicate Entries n Heaps allow duplicates n A highest priority item appears at root others of same priority will be top in their sub-treeothers of same priority will be top in their sub-tree duplicates may also appear in separate sub-treesduplicates may also appear in separate sub-trees

Starting the PQ n First person goes to front of “heap” n Second person arrives if smaller, goes to frontif smaller, goes to front »first person moves back to the left otherwise, new person goes to back and leftotherwise, new person goes to back and left n Third person arrives if smaller than FRONT person, goes to frontif smaller than FRONT person, goes to front »front person goes back and to the right otherwise, new person goes to back and rightotherwise, new person goes to back and right

Growing the PQ n As people arrive, they start in the back row as far left in that row as they can goas far left in that row as they can go if smaller than person immediately in front, the two exchange positionsif smaller than person immediately in front, the two exchange positions new person continues pushing forward until runs into someone smaller (more important)new person continues pushing forward until runs into someone smaller (more important) »or until reach front of PQ

New Person Joins the Heap n Insert requires adding a new element to the array & making the array back into a heap small (high priority) items must “percolate up”small (high priority) items must “percolate up” stop when parent is smaller…stop when parent is smaller… or reach rootor reach root

Storing Heaps in Arrays n Heap can be stored compactly in an array do a “level order” traversal of the tree into the arraydo a “level order” traversal of the tree into the array n Left child of i is in 2i n Right child of i is in 2i+1 n Parent of i is in i/2 n Leave location 0 empty! Note: array index starts at 1

Exercise n Store the following heap into an array

Exercise n Draw the complete “heap” represented by the following array: [2, 4, 9, 7, 13, 11, 20, 18, 23, 15, 14, 12, 16, 19][2, 4, 9, 7, 13, 11, 20, 18, 23, 15, 14, 12, 16, 19] n Is it actually a heap? if not, can you make it into a heap?if not, can you make it into a heap?

Insert item into Heap 1.Let N be the next available position in the array 2.While N > 1 & A[N/2] > item a)set A[N] to A[N/2] b)set N to N/2 3.Set A[N] to the new item 4.Add 1 to size of heap

Insertion Example n Insert 6 into this heap n N =

Insertion Example n Insert 6 into this heap n N  8 n A[N/2] = A[4] = 81 n 81 > 6  percolate up ?

Insertion Example n Insert 6 into this heap n N = 4 n A[N/2] = A[2] = 7 n 7 > 6  percolate up

Insertion Example n Insert 6 into this heap n N = 2 n A[N/2] = A[1] = 5 n 5  6  percolating done n Set A[N] = A[2] to

Exercise n Continuing the example: insert 14insert 14 insert 7insert 7 insert 4insert

Removing from a Heap n Front person leaves heap n How shall we fill the empty space? NOT by pushing forward into the open spaceNOT by pushing forward into the open space that might ruin our nice heapthat might ruin our nice heap n Instead we put the last person in the front(!) n Then people start pushing forward starting with the smaller of the people in the second row of the heapstarting with the smaller of the people in the second row of the heap

Deletion from Heap n Remove the root n Promote last item to root n Let it percolate down

Deletion from Heap n Remove the root n Promote last item to root n Let it percolate down choose smaller of two childrenchoose smaller of two children

Deletion from Heap n Remove the root n Promote last item to root n Let it percolate down choose smaller of two childrenchoose smaller of two children

Deletion from Heap n Remove the root n Promote last item to root n Let it percolate down choose smaller of two childrenchoose smaller of two children (if it has two children)(if it has two children)

Deletion from Heap n Remove the root n Promote last item to root n Let it percolate down choose smaller of two childrenchoose smaller of two children (if it has two children)(if it has two children)

Percolating Down n Check number of children if none – then we are done percolatingif none – then we are done percolating if one – it is the smallest childif one – it is the smallest child if two – choose whichever is smallerif two – choose whichever is smaller n If smaller child is smaller than percolator move the child upmove the child up continue percolating from the child’s positioncontinue percolating from the child’s position

Percolating Down 1.Let P be the last position in the heap 2.Let N be 1 3.If 2N<P (* has surviving children *) a)If 2N+1=P (* one child *), let S be 2N b)Else let S be (A[2N]<A[2N+1]) ? 2N : 2N+1 c)If (A[P]>A[S]) i.let A[N] be A[S], N be S & go to step 3 4.Let A[N] be A[P] NOTE: P is the position we are deleting from

Deleting from the Heap 1.Set temp to A[1] 2.Percolate Down// from previous slide 3.Reduce the size of the heap by 1 4.Return temp

Exercise n Delete from this heap n Delete again from this heap n And again

Our Heap is Now a PQ n remove method always gets smallest item and leaves next smallest item in its placeand leaves next smallest item in its place n add method inserts new item anywhere but moves it up to front if it’s smallestbut moves it up to front if it’s smallest n We can add convenience methods look at front, check if empty, make empty, …look at front, check if empty, make empty, …

17 “Almost” Heap Sort n Can sort an array using our heap/PQ add each element of array to heapadd each element of array to heap remove items from heap back into the arrayremove items from heap back into the array n But we can do better! we can do it all in the arraywe can do it all in the array turn the array into a heapturn the array into a heap

Random Array to Heap n Need to get smaller items up & bigger items down percolate up or downpercolate up or down n Need to be sure we get everything to its level only items with children matteronly items with children matter have childrenhave no children

Random Array to Heap n Start with the last item with children n Percolate it down n Move on to the previous node stop at the rootstop at the root have childrenhave no children

Random Array to Heap n Item 5 is an 8 its one child (item 10) is a 14its one child (item 10) is a 14 it’s OKit’s OK n Item 4 is a 20 its smaller child (item 9) is a 13its smaller child (item 9) is a 13 swap themswap them now OKnow OK have childrenhave no children

Random Array to Heap n Item 3 is a 13 its smaller child is a 9its smaller child is a 9 swap & OKswap & OK n Item 2 is a 19 smaller child is 8smaller child is 8 »swap its smaller child is 14:its smaller child is 14: »swap again have childrenhave no children

Random Array to Heap n Item 1 is a 29 smaller child is 8smaller child is 8 »swap its smaller child is 13its smaller child is 13 »swap again its smaller child is 14its smaller child is 14 »swap again donedone have childrenhave no children

Exercise n Use the BuildHeap algorithm to build a heap from the following array [9, 86, 54, 92, 91, 38, 28, 87, 93, 84, 88, 24, 64, 96, 3, 18, 1, 72, 66, 8][9, 86, 54, 92, 91, 38, 28, 87, 93, 84, 88, 24, 64, 96, 3, 18, 1, 72, 66, 8]

Code for Building a Heap n Uses a modified version of percolate down say where to percolate down fromsay where to percolate down from To BuildHeap(Array A, int Length) i  Length ÷ 2; while (i  1) PercolateDown(A, i, Length); i – –;

Code for Percolate Down To PercolateDown(Array A, int N, int L) T  A[N]; while (2N  L) if (2N = L)S  2N; elseS  (A[2N]<A[2N+1]) ? 2N : 2N+1; if (A[S]<T)A[N]  A[S], N  S; elsebreak; A[N]  T;

Building a Heap is O(N) n There will be less than N swaps establish an upper bound on the # of swapsestablish an upper bound on the # of swaps n Let k be the height of the tree n Worst case # of swaps: # of swaps for left sub-tree# of swaps for left sub-tree plus # of swaps for rightplus # of swaps for right plus k swaps for the rootplus k swaps for the root assume all leaves at same level (simplifies math)assume all leaves at same level (simplifies math)

Maximum Number of Swaps kNformulamax swapsN – k – * * * * Equality can be proven by induction on k

Heap Class n Operations Insert, FindMin, DeleteMinInsert, FindMin, DeleteMin IsEmpty, MakeEmptyIsEmpty, MakeEmpty n Constructors default (creates empty heap)default (creates empty heap) with vector (uses BuildHeap to make heap out of the vector)with vector (uses BuildHeap to make heap out of the vector)

Heap Sort n Turn array into a reverse heap bigger numbers at the frontbigger numbers at the front modified version of BuildHeapmodified version of BuildHeap n After you remove each item… n …put it at the back of the array which just became available!which just became available!

Heap Sort Example n Original array: n maxHeapify: n removeAll:

Questions?