Вы находитесь на странице: 1из 3

Outline Heapsort, Priority Queue

Heap data structure Extract min Insert Priority queue Heapsort

Recitation 3: Heapsort, Priority Queue

Recitation 3: Heapsort, Priority Queue

Binary Min-heap
Nearly complete binary tree that satisfies the heap property
4 2

Array Representation
1

1 2 7
9 10 5 6 3

5
7

tree completely filled on all levels except lowest level, which is filled from the left

3 8 11

8 1 2 5 3 7 6 8 9 8 11

Heap property:
A[parent(x)] A[x]
Left(i) = 2i Right(i) = 2i+1 Parent(i) = i/2
Recitation 3: Heapsort, Priority Queue 3

Recitation 3: Heapsort, Priority Queue

Extract Min
EXTRACT - MIN(A) 1 2 3 4 5 min A[1] A[1] A[heap - size[ A]] heap - size[ A] heap - size[ A] 1 MIN - HEAPIFY( A,1) maintain heap property return min

Remove the max item


12 8 9

Extract the min element for priority queue Simple way to sort - repeatedly extract-min.
Recitation 3: Heapsort, Priority Queue 5

Recitation 3: Heapsort, Priority Queue

Re-establish heap property


1 8 9

Re-establish heap property


9 8 7

bubble down
Recitation 3: Heapsort, Priority Queue 7 Recitation 3: Heapsort, Priority Queue 8

Min-Heapify
MIN - HEAPIFY( A, i ) 1 if 2i size[ A] and A[2i ] < A[i ] left child 2 then smallest 2i 3 4 5 6 7 8 else smallest i if 2i + 1 size[ A] and A[2i + 1] < A[ smallest ] right child then smallest 2i + 1 if smallest i then swap A[i ] A[ smallest ] MIN - HEAPIFY( A, smallest )

Insert
INSERT(key, A) 1 2 3 4 5 6 A[i ] key heap size( A) heap size( A) + 1 i heap size( A) while i > 1 and A[ i / 2] > key do A[i ] A[ i / 2] i i / 2

What is the running time of MIN-HEAPIFY?


Recitation 3: Heapsort, Priority Queue 9

Start at new leaf, move parent down until we meet a parent smaller than key. Running time: O(lg n)
Recitation 3: Heapsort, Priority Queue 10

Insert an item
9
0 1

Re-establish heap property


12 8
9
2 3 4

5 6 7

15

8 9

9 4 3 5 7 2 1 15
11

15

Recitation 3: Heapsort, Priority Queue

Recitation 3: Heapsort, Priority Queue

12

Re-establish heap property


15 9 7

Priority Queue
Data structure for maintaining a set of elements, each with an associated value called a key. Example: scheduling jobs on a shared computer

When job finished, highest priority job is selected to be executed.

bubble up
Recitation 3: Heapsort, Priority Queue 13

One of the most popular application of heap Insert new element in O(lg n) Extract element with smallest (largest) key in time O(lg n).
Recitation 3: Heapsort, Priority Queue 14

Heapsort
HEAPSORT ( A) 1 2 3 4 5 BUILD MAX HEAP( A) for i heap size( A) downto 2 do swap A[1] A[i ] heap size( A) heap size( A) 1 MAX HEAPIFY( A,1)
1 2

Build-Min-Heap
BUILD MIN HEAP( A) for i heap size( A) / 2 downto 1 do MIN HEAPIFY( A, i )

Correctness:
Invariant: All trees rooted at m>i are heaps

Use min-heap to get decreasing seq Sorts in place Running time = O(n log n) + Build-maxheap time.
Recitation 3: Heapsort, Priority Queue 15

Running time:
Height of tree is lg n Number of nodes at height h is at most n/2h+1 MIN-HEAPIFY runs in time O(h) when the node is of height h.
Recitation 3: Heapsort, Priority Queue 16

Running time:
lg n

2
i

lg n h n O ( h) = O n h = O ( n) h +1 h=0 h =0 2
= 1 1 x | x |< 1 differentiation

Summary
Heap data structure
nearly complete binary tree A[parent(x)] A[x]

because

x
i =0 i =0

Extract min, insert


O(lg n)

ix i 1 =
i

1 (1 x) 2

Heapsort
O(n lg n) in place sort

x mult both sides by x ix = (1 x)2 i =0 =2 when x = 1 / 2


Recitation 3: Heapsort, Priority Queue 17

Priority queue
popular application of heap - O(lg n) insert and extract min
Recitation 3: Heapsort, Priority Queue 18

Вам также может понравиться