Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Like this presentation? Why not share!

No Downloads

Total views

692

On SlideShare

0

From Embeds

0

Number of Embeds

1

Shares

0

Downloads

68

Comments

0

Likes

1

No embeds

No notes for slide

- 1. Binary Heaps Delete-min Building a heap in O(n) time Heap Sort 1
- 2. Delete-min The minimum element is the one at the top of the heap. We can delete this and move one of its children up to fill the space. Empty location moves down the tree. Might end up at any position on last level. Resulting tree would not be left filled. 2
- 3. Delete-min in a Heap 8 10 11 18 21 13 12 43 23 26 29 31 19 17 This is not a heap 3
- 4. Delete-min in a Heap (2) Replace top element with last element of heap. Heapify(1) 8 10 11 16 21 13 12 43 23 26 29 31 19 17 4
- 5. Building a heap We start from the bottom and move up All leaves are heaps to begin with 23 43 26 10 21 13 31 16 12 8 29 11 19 17 5
- 6. Building a Heap: Analysis Correctness: induction on i, all trees rooted at m > i are heaps Running time: n calls to Heapify = n O(lg n) = O(n lg n) We can provide a better O(n) bound. Intuition: for most of the time Heapify works on smaller than n element heaps 6
- 7. Building a Heap: Analysis (2) height of node: length of longest path from node to leaf height of tree: height of root time for Heapify(i) = O(height of subtree rooted at i) assume n = 2k – 1 (a complete binary tree) 7
- 8. Building a heap: Analysis (3) For the n/2 nodes of height 1, heapify() requires at most 1 swap each. For the n/4 nodes of height 2, heapify() requires at most 2 swaps each. For the n/2i nodes of height i, heapify() requires at most i swaps each. So total number of swaps required is n 1 n 1 n 1 T ( n) O 2 3 ... 1 k 2 4 8 lg n lg n i i 1/ 2 O n 1 since 2 2 i 1 2i i 1 2i 1 1/ 2 O ( n) 8
- 9. Building a Heap: Analysis (4) How? By using the following "trick" i 1 x if x 1 //differentiate i 0 1 x i 1 1 i x 2 //multiply by x i 1 1 x i x 1 i x 2 //plug in x i 1 1 x 2 i 1/ 2 2 i 1 2i 1/ 4 Therefore Build-Heap time is O(n) 9
- 10. Heap Sort Create a heap. Do delete-min 8 repeatedly till heap becomes 10 11 empty. To do an in place 12 21 13 17 sort, we move deleted element 16 23 43 29 26 19 31 to end of heap. 10
- 11. Running times of heap operations Insert:O(log n) Heapify: O(log n) Find minimum: O(1) Delete-min: O(log n) Building a heap: O(n) Heap Sort: O(nlog n) 11

No public clipboards found for this slide

×
### Save the most important slides with Clipping

Clipping is a handy way to collect and organize the most important slides from a presentation. You can keep your great finds in clipboards organized around topics.

Be the first to comment