Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Binary heaps and Heapsort

2 Priority Queues n The smallest element is removed item = remove();item = remove(); 17 24 81 12 5 7 17 12 8 Priority Queue Minimum element is 5

3 Priority Queues n The smallest element is removed item = remove();item = remove(); 17 24 81 12 7 17 12 8 Priority Queue Minimum element is 7

4 Priority Queues n Additions made wherever add(15);add(15); Priority Queue Minimum element is 8 17 24 81 12 17 12 817 24 81 12 17 12 8 15

5 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?

6 List Representations n Sorted Rep Insert 14Insert 14 Remove-HighestRemove-Highest n Unsorted Rep 17 24 81 12 5 7 17 12 85 7 8 12 12 17 17 24 81 17 24 81 12 5 7 17 12 8 14 5 7 8 12 12 14 17 17 24 81 17 24 81 12 7 17 12 8 14 7 8 12 12 14 17 17 24 8117 24 81 12 17 12 8 148 12 12 14 17 17 24 81

7 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, …

8 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

9 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 5 12 7 8812417

10 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 1 5 1 18814 1514

11 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

12 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

13 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 2 9 4 1371120 231815 141216 8 8 20 9 88

14 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! 5 12 7 8812417 5 12 7 8812417 51278812417 12345671234567 Note: array index starts at 1

15 Exercise n Store the following heap into an array 1 5 2 381912 141520

16 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?

17 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

18 Insertion Example n Insert 6 into this heap n N = 8 5 12 7 8812417 51278812417 12345671234567

19 Insertion Example n Insert 6 into this heap n N  8 n A[N/2] = A[4] = 81 n 81 > 6  percolate up 6 5 12 7 8812417 51278812417 1234567812345678 ?

20 Insertion Example n Insert 6 into this heap n N = 4 n A[N/2] = A[2] = 7 n 7 > 6  percolate up 81 5 12 7 862417 51278812417 1234567812345678 81

21 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 6 81 5 12 6 872417 5127872417 1234567812345678 81 6 6

22 Exercise n Continuing the example: insert 14insert 14 insert 7insert 7 insert 4insert 4 81 5 12 6 872417 5126872417 1234567812345678 81

23 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

24 Deletion from Heap n Remove the root n Promote last item to root n Let it percolate down 81 4 12 5 672417 4125672417 1234567891011 81 1487 87 4

25 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 81 7 12 5 672417 7125672417 12345678910 81 148 87

26 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 81 5 12 7 672417 5127672417 12345678910 81 148 87

27 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) 81 5 12 6 772417 5126772417 12345678910 81 148 87

28 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) 81 5 12 6 772417 5126772417 12345678910 81 148 87

29 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

30 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

31 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

32 Exercise n Delete from this heap n Delete again from this heap n And again 81 5 12 6 17724 512617724 12345678910 81 1418 1418

33 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, …

34 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 5 127 8124 5121781824758717122481 8 12 7 17 24 8 81 1217 2481 2481

35 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 14 29 13 19 820922 291319820922 12345678910 14 1314 1314 have childrenhave no children

36 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 14 29 13 19 820922 291319820922 12345678910 14 1314 1314 have childrenhave no children

37 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 14 29 13 19 820922 291319820922 12345678910 14 1314 1314 have childrenhave no children 13 20 1320

38 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 14 29 13 19 813922 291319813922 12345678910 14 2014 2014 have childrenhave no children 9 13 8 19 14 9819 1413

39 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 14 29 9 8 1413 22 29981413 22 12345678910 14 2019 2019 have childrenhave no children 8 29 13 29 14 829 132914

40 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]

41 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 – –;

42 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;

43 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)

44 Maximum Number of Swaps kNformulamax swapsN – k – 1 0100 132*0 + 111 272*1 + 244 3152*4 + 31111 4312*11 + 42626 Equality can be proven by induction on k

45 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)

46 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!

47 Heap Sort Example n Original array: n maxHeapify: n removeAll: 1772295 97 175 972217522 5172217 97 9 5797 57 5

48 Questions?


Download ppt "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."

Similar presentations


Ads by Google