1 / 52

Binary and Binomial Heaps

Binary and Binomial Heaps. Slides borrowed from Kevin Wayne - Princeton University. Pre-Requisites. Binomial Coeffiecients N-ary tree representation Left Child Right Sub-Tree. Slides borrowed from Kevin Wayne - Princeton University. Binomial Coefficients. Wikepedia:

keegan
Télécharger la présentation

Binary and Binomial Heaps

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Binary and Binomial Heaps Slides borrowed from Kevin Wayne - Princeton University

  2. Pre-Requisites • Binomial Coeffiecients • N-ary tree representation • Left Child Right Sub-Tree Slides borrowed from Kevin Wayne - Princeton University

  3. Binomial Coefficients Wikepedia: In mathematics, particularly in combinatorics, the binomial coefficient of the natural number n and the integer k is defined to be the natural number: Example:

  4. N-ary Tree’s 06 14 18 47 45 78 83 91 14 77 84 64 53 99 91 78 83

  5. N-ary Tree’s 06 14 18 47 45 78 83 91 14 77 84 64 53 99 91 78 83

  6. N-ary Tree’s 06 14 18 47 45 78 83 91 14 77 84 64 53 99 91 78 83 Traverse(Node *t){ if(t->child !=NULL) then Traverse(t->child) if(t->sibling != NULL) then Traverse(t->sibling) }

  7. 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. • . . .

  8. Priority Queues in Action PQinit() for each v  V key(v)  PQinsert(v) key(s)  0 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

  9. Priority Queues Heaps Operation Linked List Binary Binomial Fibonacci * make-heap 1 1 1 1 insert 1 log N log N 1 find-min N 1 log N 1 delete-min N log N log N log N union 1 N log N 1 decrease-key 1 log N log N 1 delete N log N log N log N is-empty 1 1 1 1 O(|V|2) O(|E| log |V|) O(|E| + |V| log |V|)

  10. 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 45 78 18 47 53 91 77 83 84 81 99 64

  11. Binary Heap: Properties • Properties. • Min element is in root. • Heap with N elements has height = log2 N. 06 N = 14Height = 3 14 45 78 18 47 53 91 77 83 84 81 99 64

  12. 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 1 14 45 2 3 78 18 47 53 4 5 6 7 91 77 83 84 81 99 64 8 9 10 12 14 11 13

  13. Binary Heap: Insertion next free slot 42 • Insert element x into heap. • Insert into next available slot. • Bubble up until it's heap ordered. 06 14 45 78 18 47 53 91 77 83 84 81 99 64

  14. Binary Heap: Insertion 42 42 • Insert element x into heap. • Insert into next available slot. • Bubble up until it's heap ordered. swap with parent 06 14 45 78 18 47 53 91 77 83 84 81 99 64

  15. Binary Heap: Insertion 42 • Insert element x into heap. • Insert into next available slot. • Bubble up until it's heap ordered. swap with parent 06 14 45 78 18 47 42 91 77 83 84 81 99 64 53

  16. Binary Heap: Insertion • Insert element x into heap. • Insert into next available slot. • Bubble up until it's heap ordered. • O(log N) operations. stop: heap ordered 06 14 42 78 18 47 45 91 77 83 84 81 99 64 53

  17. Binary Heap: Decrease Key • Decrease key of element x to k. • Bubble up until it's heap ordered. • O(log N) operations. 06 14 42 78 18 47 45 91 77 83 84 81 99 64 53

  18. Binary Heap: Delete Min • Delete minimum element from heap. • Exchange root with rightmost leaf. • Bubble root down until it's heap ordered. • power struggle principle: better subordinate is promoted 06 14 42 78 18 47 45 91 77 83 84 81 99 64 53

  19. Binary Heap: Delete Min • Delete minimum element from heap. • Exchange root with rightmost leaf. • Bubble root down until it's heap ordered. • power struggle principle: better subordinate is promoted 53 14 42 78 18 47 45 91 77 83 84 81 99 64 06

  20. Binary Heap: Delete Min • Delete minimum element from heap. • Exchange root with rightmost leaf. • Bubble root down until it's heap ordered. • power struggle principle: better subordinate is promoted exchange with left child 53 14 42 78 18 47 45 91 77 83 84 81 99 64

  21. Binary Heap: Delete Min • Delete minimum element from heap. • Exchange root with rightmost leaf. • Bubble root down until it's heap ordered. • power struggle principle: better subordinate is promoted exchange with right child 14 53 42 78 18 47 45 91 77 83 84 81 99 64

  22. Binary Heap: Delete Min • Delete minimum element from heap. • Exchange root with rightmost leaf. • Bubble root down until it's heap ordered. • power struggle principle: better subordinate is promoted • O(log N) operations. stop: heap ordered 14 18 42 78 53 47 45 91 77 83 84 81 99 64

  23. Binary Heap: Heapsort • Heapsort. • Insert N items into binary heap. • Perform N delete-min operations. • O(N log N) sort. • No extra storage.

  24. Binary Heap: Union • Union. • Combine two binary heaps H1 and H2 into a single heap. • No easy solution. • (N) operations apparently required • Can support fast union with fancier heaps. H2 H1 14 11 78 18 53 62 91 77 41 84 81 99 64

  25. Priority Queues Heaps Operation Linked List Binary Binomial Fibonacci * make-heap 1 1 1 1 insert 1 log N log N 1 find-min N 1 log N 1 delete-min N log N log N log N union 1 N log N 1 decrease-key 1 log N log N 1 delete N log N log N log N is-empty 1 1 1 1

  26. Binomial Tree Bk B0 Bk-1 Bk-1 • Binomial tree. • Recursive definition: B0 B1 B2 B3 B4

  27. Binomial Tree Bk+1 B1 B2 B0 Bk • Useful properties of order k binomial tree Bk. • Number of nodes = 2k. • Height = k. • Degree of root = k. • Deleting root yields binomialtrees Bk-1, … , B0. B0 B1 B2 B3 B4

  28. Binomial Tree • A property useful for naming the data structure. • Bk has nodes at depth i. depth 0 depth 1 depth 2 depth 3 depth 4 B4

  29. Binomial Heap 6 8 29 10 44 30 23 22 48 31 17 45 32 24 50 55 • Binomial heap. Vuillemin, 1978. • Sequence of binomial trees that satisfy binomial heap property. • each tree is min-heap ordered • 0 or 1 binomial tree of order k 3 18 37 B4 B1 B0

  30. Binomial Heap: Implementation • Implementation. • Represent trees using left-child, right sibling pointers. • three links per node (parent, left, right) • Roots of trees connected with singly linked list. • degrees of trees strictly decreasing from left to right heap 6 3 18 6 3 18 Parent 37 37 29 10 29 44 Right Left 48 31 17 48 10 50 50 31 17 44 Binomial Heap Leftist Power-of-2 Heap

  31. Binomial Heap: Properties 6 8 29 10 44 30 23 22 48 31 17 45 32 24 50 55 • Properties of N-node binomial heap. • Min key contained in root of B0, B1, . . . , Bk. • Contains binomial tree Bi iff bi = 1 where bn b2b1b0 is binary representation of N. • At most log2 N + 1 binomial trees. • Height  log2 N. 3 18 37 N = 19# trees = 3height = 4binary = 10011 B4 B1 B0

  32. Binomial Heap: Union • Create heap H that is union of heaps H' and H''. • "Mergeable heaps." • Easy if H' and H'' are each order k binomial trees. • connect roots of H' and H'' • choose smaller key to be root of H 6 8 29 10 44 30 23 22 48 31 17 45 32 24 50 55 H' H''

  33. Binomial Heap: Union

  34. Binomial Heap: Union 6 3 18 37 8 29 10 44 30 23 22 48 31 17 15 7 12 45 32 24 50 25 28 33 + 55 41 1 1 1 1 0 0 1 1 + 0 0 1 1 1 19 + 7 = 26 1 1 0 1 0

  35. Binomial Heap: Union 3 18 15 7 12 6 37 25 28 33 8 29 10 44 41 30 23 22 48 31 17 45 32 24 50 55

  36. Binomial Heap: Union 15 6 7 12 3 18 28 8 29 10 25 44 33 37 30 23 22 48 31 17 41 45 32 24 50 55

  37. Binomial Heap: Union x next 15 6 18 7 12 3 28 8 29 10 25 44 33 37 30 23 22 48 31 17 41 45 32 24 50 55

  38. Binomial Heap: Union x next 15 6 7 12 3 28 8 29 10 25 44 33 37 18 30 23 22 48 31 17 41 45 32 24 50 55

  39. Binomial Heap: Union x next prev 15 6 12 3 7 28 8 29 10 44 33 37 25 18 30 23 22 48 31 17 41 45 32 24 50 55

  40. Binomial Heap: Union x next prev 15 6 3 12 28 8 29 10 7 44 33 37 18 30 23 22 48 31 17 41 25 45 32 24 50 55 • Combine x and next

  41. Binomial Heap: Union x next prev 15 6 3 12 28 8 29 10 7 44 33 37 18 30 23 22 48 31 17 41 25 45 32 24 50 55 • Move pointers

  42. Binomial Heap: Union x next prev 15 6 3 12 28 8 29 10 7 44 33 37 18 30 23 22 48 31 17 41 25 45 32 24 50 55 • x and next are of same degree, so we combine them

  43. Binomial Heap: Union x next prev 6 3 12 8 29 10 7 44 37 15 18 30 23 22 48 31 17 25 28 33 45 32 24 50 41 55

  44. Binomial Heap: Union x next prev 6 3 12 8 29 10 7 44 37 15 18 30 23 22 48 31 17 25 28 33 45 32 24 50 41 55 Moved the pointers and see all node have a different degree, we are done.

  45. Binomial Heap: Union • Create heap H that is union of heaps H' and H''. • Analogous to binary addition. • Running time. O(log N) • Proportional to number of trees in root lists  2(log2 N + 1). 6 3 12 8 29 10 7 44 37 15 18 30 23 22 48 31 17 25 28 33 45 32 24 50 41 55 1 1 1 1 0 0 1 1 + 0 0 1 1 1 19 + 7 = 26 1 1 0 1 0

  46. Binomial Heap: Delete Min 3 6 18 44 8 29 10 37 30 23 22 48 31 17 H 45 32 24 50 55 • Delete node with minimum key in binomial heap H. • Find root x with min key in root list of H, and delete • H'  broken binomial trees • H Union(H', H) • Running time. O(log N)

  47. Binomial Heap: Delete Min H' • Delete node with minimum key in binomial heap H. • Find root x with min key in root list of H, and delete • H'  broken binomial trees • H Union(H', H) • Running time. O(log N) 6 18 44 8 29 10 37 30 23 22 48 31 17 H 45 32 24 50 55

  48. Binomial Heap: Decrease Key • Decrease key of node x in binomial heap H. • Suppose x is in binomial tree Bk. • Bubble node x up the tree if x is too small. • Running time. O(log N) • Proportional to depth of node x  log2 N . 3 6 18 depth = 3 44 8 29 10 37 30 23 22 48 31 17 H x 32 24 50 55

  49. Binomial Heap: Delete • Delete node x in binomial heap H. • Decrease key of x to -. • Delete min. • Running time. O(log N)

  50. Binomial Heap: Insert • Insert a new node x into binomial heap H. • H'  MakeHeap(x) • H Union(H', H) • Running time. O(log N) 3 6 18 x 44 8 29 10 37 30 23 22 48 31 17 H H' 45 32 24 50 55

More Related