09-10_Applying_Guide - Princeton University • COS 423 •...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Princeton University • COS 423 • Theory of Algorithms • Spring 2002 • Kevin Wayne Binary and Binomial Heaps These lecture slides are adapted from CLRS, Chapters 6, 19. 2 Priority Queues Supports the following operations. ■ Insert element x. ■ Return min element. ■ Return and delete minimum element. ■ Decrease key of element x to k. Applications. ■ Dijkstra’s shortest path algorithm. ■ Prim’s MST algorithm. ■ Event-driven simulation. ■ Huffman encoding. ■ Heapsort. ■ . . . 3 Priority Queues in Action PQinit () for each v ∈ V key(v) ← ∞ PQinsert (v) key(s) ← while (! PQisempty ()) v = PQdelmin () for each w ∈ Q s.t (v,w) ∈ E if π (w) > π (v) + c(v,w) PQdecrease (w, π (v) + c(v,w)) Dijkstra’s Shortest Path Algorithm 4 Dijkstra/Prim 1 make-heap |V| insert |V| delete-min |E| decrease-key Priority Queues make-heap Operation insert find-min delete-min union decrease-key delete 1 Binary log N 1 log N N log N log N 1 Binomial log N log N log N log N log N log N 1 Fibonacci * 1 1 log N 1 1 log N 1 Relaxed 1 1 log N 1 1 log N 1 Linked List 1 N N 1 1 N is-empty 1 1 1 1 1 Heaps O(|E| + |V| log |V|) O(|E| log |V|) O(|V| 2 ) 5 Binary Heap: Definition Binary heap. ■ Almost complete binary tree. – filled on all levels, except last, where filled from left to right ■ Min-heap ordered. – every child greater than (or equal to) parent 06 14 78 18 81 77 91 45 53 47 64 84 99 83 6 Binary Heap: Properties Properties. ■ Min element is in root. ■ Heap with N elements has height = log 2 N . 06 14 78 18 81 77 91 45 53 47 64 84 99 83 N = 14 Height = 3 7 Binary Heaps: Array Implementation Implementing binary heaps. ■ Use an array: no need for explicit parent or child pointers. – Parent(i) = i/2 – Left(i) = 2i – Right(i) = 2i + 1 06 14 78 18 81 77 91 45 53 47 64 84 99 83 1 2 3 4 5 6 7 8 9 1 11 12 13 14 8 Binary Heap: Insertion Insert element x into heap. ■ Insert into next available slot. ■ Bubble up until it’s heap ordered. – Peter principle: nodes rise to level of incompetence 06 14 78 18 81 77 91 45 53 47 64 84 99 83 42 next free slot 9 Binary Heap: Insertion Insert element x into heap....
View Full Document

Page1 / 11

09-10_Applying_Guide - Princeton University • COS 423 •...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online