Binary and Binomial Heaps

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
Priority Queues  MakeQueuecreate new empty queue  Insert(Q,k,p)insert key k with priority p  Delete(Q,k)delete key k (given a pointer)  DeleteMin(Q)delete.
Priority Queues And the amazing binary heap Chapter 20 in DS&PS Chapter 6 in DS&AA.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
1 COSC 2P03 Lecture #5 – Trees Part III, Heaps. 2 Today Take up the quiz Assignment Questions Red-Black Trees Binary Heaps Heap sort D-Heaps, Leftist.
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.
Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals of Data Structures in C" and some supplement.
3-Sorting-Intro-Heapsort1 Sorting Dan Barrish-Flood.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
Binomial Heaps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Binary and Binomial Heaps These lecture slides are adapted from CLRS, Chapters.
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
Christopher Moh 2005 Application of Data Structures.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Chapter 21 Binary Heap.
1 Heaps (Priority Queues) You are given a set of items A[1..N] We want to find only the smallest or largest (highest priority) item quickly. Examples:
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
1 Binomial Tree Binomial tree. n Recursive definition: B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4.
Binomial Tree B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4 Adapted from: Kevin Wayne B k : a binomial tree B k-1 with the addition of a left child with another.
Advanced Data Structures By: Nikhil Bhargava. Outline Introduction to ADT. Introduction to ADT. Binary Search Tree (BST) Binary Search Tree (BST) –Definition.
1 Heap Sort. A Heap is a Binary Tree Height of tree = longest path from root to leaf =  (lgn) A heap is a binary tree satisfying the heap condition:
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
1 Fibonacci heaps: idea List of multiway trees which are all heap-ordered. Definition: A tree is called heap-ordered if the key of each node is greater.
Heaps and Priority Queues What is a heap? A heap is a binary tree storing keys at its internal nodes and satisfying the following properties:
Fibonacci Heaps. Fibonacci Binary insert O(1) O(log(n)) find O(1) N/A union O(1) N/A minimum O(1) O(1) decrease key O(1) O(log(n)) delete O(log(n) O(log(n))
Analysis of Algorithms
"Teachers open the door, but you must enter by yourself. "
School of Computing Clemson University Fall, 2012
Heaps (8.3) CSE 2011 Winter May 2018.
Heaps, Heap Sort and Priority Queues
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Hashing Exercises.
Source: Muangsin / Weiss
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Bohyung Han CSE, POSTECH
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Binomial Heaps Chapter 19.
Binomial Tree Adapted from: Kevin Wayne Bk-1 B0 Bk
7/23/2009 Many thanks to David Sun for some of the included slides!
Part-D1 Priority Queues
Heapsort Heap & Priority Queue.
Ch 6: Heapsort Ming-Te Chi
Algorithms Sorting-part2.
i206: Lecture 14: Heaps, Graphs intro.
Lecture 29 Heaps Chapter 12 of textbook Concept of heaps Binary heaps
Tree Representation Heap.
© 2013 Goodrich, Tamassia, Goldwasser
Ch. 8 Priority Queues And Heaps
Chapter 20 Binomial Heaps
ערמות בינומיות ופיבונצ'י
"Teachers open the door, but you must enter by yourself. "
CS 583 Analysis of Algorithms
Binary Heaps What if we’re mostly concerned with finding the most relevant data? A binary heap is a binary tree (2 or fewer subtrees for each node) A heap.
(2,4) Trees 2/15/2019 (2,4) Trees (2,4) Trees.
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
HEAPS.
Heapsort Sorting in place
Algorithms: Design and Analysis
Priority Queues CSE 373 Data Structures.
Binomial heaps, Fibonacci heaps, and applications
Priority Queues Supports the following operations. Insert element x.
Hash Maps: The point of a hash map is to FIND DATA QUICKLY.
Fibonacci Heaps & Doubled-Ended Heap Structures
Heaps & Multi-way Search Trees
Heaps 9/29/2019 5:43 PM Heaps Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

Binary and Binomial Heaps Event driven simulation: generate random inter-arrival times between customers, generate random processing times per customer, Advance clock to next event, skipping intervening ticks

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. . . . Event driven simulation: generate random inter-arrival times between customers, generate random processing times per customer, Advance clock to next event, skipping intervening ticks

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

Priority Queues Heaps Operation Linked List Binary Binomial Fibonacci * Relaxed make-heap 1 1 1 1 1 insert 1 log N log N 1 1 find-min N 1 log N 1 1 delete-min N log N log N log N log N union 1 N log N 1 1 not possible to get all ops O(1) if only use comparisons because of N log N sorting lower bound Brodal gives variant of relaxed heap that achieves worst-case bounds for all operations relaxed heap: relaxes heap ordering property decrease-key 1 log N log N 1 1 delete N log N log N log N log N is-empty 1 1 1 1 1 O(|V|2) O(|E| log |V|) O(|E| + |V| log |V|) Dijkstra/Prim 1 make-heap |V| insert |V| delete-min |E| decrease-key

Binary Heap: Definition 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 83 91 81 77 84 99 64

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

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 parent and children formula require only simple bit twiddling operations 1 14 45 2 3 78 18 47 53 4 5 6 7 83 91 81 77 84 99 64 8 9 10 11 12 13 14

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 45 78 18 47 53 42 next free slot 83 91 81 77 84 99 64

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 swap with parent 06 14 45 78 18 47 53 42 42 83 91 81 77 84 99 64

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 swap with parent 06 14 45 78 18 47 42 42 83 91 81 77 84 99 64 53

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 O(log N) operations. stop: heap ordered 06 14 42 78 18 47 45 83 91 81 77 84 99 64 53

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 83 91 81 77 84 99 64 53

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 83 91 81 77 84 99 64 53

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 83 91 81 77 84 99 64 06

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 83 91 81 77 84 99 64

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 83 91 81 77 84 99 64

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 83 91 81 77 84 99 64

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

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. H1 H2 14 11 78 18 53 62 41 91 81 77 84 99 64

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

Binomial Tree Binomial tree. Recursive definition: Bk-1 B0 Bk B0 B1 B2 not binary tree B0 B1 B2 B3 B4

Binomial Tree Useful properties of order k binomial tree Bk. Number of nodes = 2k. Height = k. Degree of root = k. Deleting root yields binomial trees Bk-1, … , B0. Proof. By induction on k. B1 Bk Bk+1 B2 B0 B0 B1 B2 B3 B4

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

Binomial Tree A property useful for naming the data structure. Bk has nodes at depth i. (k-1 choose i-1) + (k-2 choose i-1) + … + (i-1 choose i-1) = (k choose i) since (k choose i) = (k-1 choose i-1) + (k-1 choose i) depth 0 depth 1 depth 2 depth 3 depth 4 B4

Binomial Heap 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 55 45 32 30 24 23 22 50 48 31 17 44 8 29 10 6 3 18 sequence -> ordered 37 B4 B1 B0

Binomial Heap: 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 coding with a variable number of links per node would be more painful 6 3 18 6 3 18 Parent 37 29 10 44 37 29 Right Left 48 31 17 48 10 50 50 31 17 44 Binomial Heap Leftist Power-of-2 Heap

Binomial Heap: Properties 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. 55 45 32 30 24 23 22 50 48 31 17 44 8 29 10 6 3 18 37 N = 19 # trees = 3 height = 4 binary = 10011 B4 B1 B0

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''

+ Binomial Heap: Union 1 1 1 1 1 1 + 1 1 1 19 + 7 = 26 1 1 1 3 18 8 29 10 44 37 30 23 22 48 31 17 15 7 12 45 amusing to consider "multiplying" binomial heaps or other arithmetic ops. Any uses??? 32 24 50 28 33 25 + 55 41 1 1 1 1 1 1 + 1 1 1 19 + 7 = 26 1 1 1

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

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

3 12 7 37 18 25 6 3 18 8 29 10 44 37 30 23 22 48 31 17 15 7 12 45 32 24 50 28 33 25 + 55 41 12 18

3 3 12 15 7 37 7 37 18 28 33 25 25 41 6 3 18 8 29 10 44 37 30 23 22 48 31 17 15 7 12 45 32 24 50 28 33 25 + 55 41 12 18

3 3 12 15 7 37 7 37 18 28 33 25 25 41 6 3 18 8 29 10 44 37 30 23 22 48 31 17 15 7 12 45 32 24 50 28 33 25 + 55 41 3 12 15 7 37 18 28 33 25 41

3 3 12 15 7 37 7 37 18 28 33 25 25 41 6 3 18 8 29 10 44 37 30 23 22 48 31 17 15 7 12 45 32 24 50 28 33 25 + 55 41 6 3 12 8 29 10 44 15 7 37 18 30 23 22 48 31 17 28 33 25 45 32 24 50 41 55

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). 1 1 1 1 1 1 + 1 1 1 19 + 7 = 26 1 1 1

Binomial Heap: Delete Min 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) 3 37 6 18 55 45 32 30 24 23 22 50 48 31 17 44 8 29 10 H

Binomial Heap: Delete Min 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 8 29 10 44 37 H' 30 23 22 48 31 17 H 45 32 24 50 55

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 . Bk has height k and 2k nodes 3 6 18 depth = 3 8 29 10 44 37 30 23 22 48 31 17 H x 32 24 50 55

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

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 8 29 10 44 37 30 23 22 48 31 17 H H' 45 32 24 50 55

Binomial Heap: Sequence of Inserts Insert a new node x into binomial heap H. If N = .......0, then only 1 steps. If N = ......01, then only 2 steps. If N = .....011, then only 3 steps. If N = ....0111, then only 4 steps. Inserting 1 item can take (log N) time. If N = 11...111, then log2 N steps. But, inserting sequence of N items takes O(N) time! (N/2)(1) + (N/4)(2) + (N/8)(3) + . . .  2N Amortized analysis. Basis for getting most operations down to constant time. 3 6 x 29 10 44 37 48 31 17 50

Priority Queues Heaps Operation Linked List Binary Binomial Fibonacci * Relaxed make-heap 1 1 1 1 1 insert 1 log N log N 1 1 find-min What about search? Idea 1 = use a hash table to keep track of item's position in the heap. Idea 2 = use a (balanced) search tree. N 1 log N 1 1 delete-min N log N log N log N log N union 1 N log N 1 1 decrease-key 1 log N log N 1 1 delete N log N log N log N log N is-empty 1 1 1 1 1 just did this