Chapter 9 Heap Structures

Slides:



Advertisements
Similar presentations
Fibonacci Heaps Especially desirable when the number of calls to Extract-Min & Delete is small (note that all other operations run in O(1) This arises.
Advertisements

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.
Advanced Data structure
Analysis Of Binomial Heaps. Operations Insert  Add a new min tree to top-level circular list. Meld  Combine two circular lists. Remove min  Pairwise.
Pairing Heaps. Experimental results suggest that pairing heaps are actually faster than Fibonacci heaps.  Simpler to implement.  Smaller runtime overheads.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Min-Max Heaps A double-ended priority queue is a data structure that supports the following operations: inserting an element with an arbitrary key deleting.
A Binary Tree root leaf. A Binary Tree root leaf descendent of root parent of leaf.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
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.
DEAPS By: Michael Gresenz Austin Forrest. Deaps A deap is a double-ended heap that supports the double-ended priority operations of insert, delete-min,
Binomial Heaps. Min Binomial Heap Collection of min trees
Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Review of Chapter 9 張啟中. Inheritance Hierarchy Min PQ Max PQ Min Heap Mergeable Min PQ DeapMin-Max Min-LeftistMin-SkewMinFHeap MinBHeap DEPQMax Heap Symmetric.
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.
Fibonacci Heaps. Single Source All Destinations Shortest Paths
Chapter 10 Search Structures Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals of Data Structures.
5.9 Heaps of optimal complexity
CS Data Structures Chapter 5 Trees. Additional Binary Tree Operations (1/7)  Copying Binary Trees  we can modify the postorder traversal algorithm.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Fibonacci Heaps These lecture slides are adapted from CLRS, Chapter 20.
Heapsort CIS 606 Spring Overview Heapsort – O(n lg n) worst case—like merge sort. – Sorts in place—like insertion sort. – Combines the best of both.
Priority Queues, Heaps & Leftist Trees
1 Binomial heaps, Fibonacci heaps, and applications.
Leftist Trees Linked binary tree. Can do everything a heap can do and in the same asymptotic complexity.  insert  remove min (or max)  initialize Can.
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
§8 Binomial Queues Haven’t we had enough about queues? What is a binomial queue for? Well, what is the average time for insertions with leftist or skew.
CS 1031 Tree Traversal Techniques; Heaps Tree Traversal Concept Tree Traversal Techniques: Preorder, Inorder, Postorder Full Trees Almost Complete Trees.
單元7: Heap 定義 for priority queues Leftist trees Binomial heap
Chap 9 Priority Queues. Operations supported by priority queue The functions that have been supported: –SP1: Return an element with minmum priority –SP2:
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
Foundations of Data Structures Practical Session #8 Heaps.
Double-Ended Priority Queues Primary operations  Insert  Remove Max  Remove Min Note that a single-ended priority queue supports just one of the above.
CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010.
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.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
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:
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
CSI 312 Dr. Yousef Qawqzeh Heaps and Priority Queue.
Fibonacci Heaps. Analysis FibonacciAnalysis.ppt Video  iew/cop5536sahni
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.
2 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)
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
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))
Leftist Trees Linked binary tree.
Data Structures Binomial Heaps Fibonacci Heaps Haim Kaplan & Uri Zwick
Splay Trees Binary search trees.
Binomial Heaps On the surface it looks like Binomial Heaps are great if you have no remove mins. But, in this case you need only keep track of the current.
CSCE 3100 Data Structures and Algorithm Analysis
Bohyung Han CSE, POSTECH
CMSC 341 Lecture 13 Leftist Heaps
Splay Trees Binary search trees.
Fibonacci Heaps Remove arbitrary is useful in (for example) correspondence structures and may also be used to do an increase key in a min structure (remove.
CSCI2100 Data Structures Tutorial 7
Pairing Heaps Actual Complexity.
A simpler implementation and analysis of Chazelle’s
ערמות בינומיות ופיבונצ'י
CS 583 Analysis of Algorithms
Fibonacci Heaps Remove arbitrary is useful in (for example) correspondence structures.
Fibonacci Heaps.
Heaps By JJ Shepherd.
CS 6310 Advanced Data Structure Wei-Shian Wang
Pairing Heaps Actual Complexity
Priority Queue and Heap
Heaps & Multi-way Search Trees
A Heap Is Efficiently Represented As An Array
Splay Trees Binary search trees.
Presentation transcript:

Chapter 9 Heap Structures CS235102 Data Structures Chapter 9 Heap Structures

Min-Max Heap Deaps Leftist Trees Binomial Heaps Fibonacci Heaps

MIN-MAX Heaps (1/10) Definition A double-ended priority queue is a data structure that supports the following operations: Insert an element with arbitrary key Delete an element with the largest key Delete an element with the smallest key Min heap or Max heap: Only insertion and one of the two deletion operations are supported Min-Max heap: Supports all of the operations just described.

MIN-MAX Heaps (2/10) Definition: A mix-max heap is a complete binary tree such that if it is not empty, each element has a field called key. Alternating levels of this tree are min levels and max levels, respectively. Let x be any node in a min-max heap. If x is on a min (max) level then the element in x has the minimum (maximum) key from among all elements in the subtree with root x. We call this node a min (max) node.

MIN-MAX Heaps (3/10) Insertion into a min-max heap (at a “max” level) If it is smaller/greater than its father (a “min”), then it must be smaller/greater than all “max”/“min” above. So simply check the “min”/“max” ancestors There exists a similar approach at a “min” level

MIN-MAX Heaps (4/10) verify_max Following the nodes the max node i to the root and insert into its proper place MIN-MAX Heaps (4/10) item = 80 i = 3 13 grandparent = 3 #define MAX_SIZE 100 #define FALSE 0 #define TRUE 1 #define SWAP(x,y,t) ((t)=(x), (x)=(y), (y)=(t)) typedef struct { int key; /* other fields */ }element; element heap[MAX_SIZE]; [1] [2] [3] 80 [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] 40

MIN-MAX Heaps (5/10) min_max_insert: Insert item into the min-max heap item.key = 80 5 *n = 13 14 12 complexity: O(log n) parent = 6 7 [1] 7 5 min [2] 70 [3] 80 40 max [4] [5] [6] [7] 30 9 10 7 15 min 45 50 30 max 20 12 10 40 [8] [9] [10] [11] [12] [13] [14]

MIN-MAX Heaps (6/10) Deletion of min element If we wish to delete the element with the smallest key, then this element is in the root. In general situation, we are to reinsert an element item into a min-max-heap, heap, whose root is empty. We consider the two cases: The root has no children Item is to be inserted into the root. The root has at least one child. The smallest key in the min-max-heap is in one of the children or grandchildren of the root. We determine the node k has the smallest key. The following possibilities need to be considered:

MIN-MAX Heaps (7/10) item.key  heap[k].key No element in heap with key smaller than item.key Item may be inserted into the root. item.key  heap[k].key, k is a child of the root Since k is a max node, it has no descendants with key larger than heap[k].key. Hence, node k has no descendants with key larger than item.key. heap[k] may be moved to the root and item inserted into node k.

MIN-MAX Heaps (8/10) item.key  heap[k].key, k is a grandchild of the root In this case, heap[k] may be moved to the root, now heap[k] is seen as presently empty. Let parent be the parent of k. If item.key  heap[parent].key, then interchange them. This ensures that the max node parent contains the largest key in the sub-heap with root parent. At this point, we are faced with the problem of inserting item into the sub-heap with root k. Therefore, we repeat the above process.

delete_min: Delete the minimum element from the min-max heap complexity: O(log n) *n = 11 12 i = 5 1 last = 5 k = 11 5 parent = 2 temp.key = x.key = 12 [1] 9 7 [0] 7 [2] 70 [3] 40 [4] [5] [6] [7] 30 12 9 10 15 45 50 30 20 12 [8] [9] [10] [11] [12]

MIN-MAX Heaps (10/10) Deletion of max element Determine the children of the root which are located on max-level, and find the larger one (node) which is the largest one on the min-max heap We would consider the node as the root of a max-min heap There exist a similar approach (deletion of max element) as we mentioned above max-min heap

Deaps(1/8) Definition The root contains no element The left subtree is a min-heap The right subtree is a max-heap Constraint between the two trees: let i be any node in left subtree, j be the corresponding node in the right subtree. if j not exists, let j corresponds to parent of i i.key <= j.key

Deaps(2/8) i = min_partner(n) = j = max_partner(n) = if j > heapsize j /= 2

Deaps Insert(3/8) } else { public void insert(int x) { i = maxPartner(n); if (x > deap[i]) { deap[n] = deap[i]; maxInsert(i, x); } else minInsert(n, x); } public void insert(int x) { int i; if (++n == 2) { deap[2] = x; return; } if (inMaxHeap(n)) { i = minPartner(n); if (x < deap[i]) { deap[n] = deap[i]; minInsert(i, x); } else maxInsert(n, x);

Deaps(4/8) Insertion Into A Deap

Deaps(5/8)

Deaps(6/8)

Deaps delete min(7/8) public int deleteMin() { int i, j, key = deap[2], x = deap[n--]; // move smaller child to i for (i = 2; 2*i <= n; deap[i] = deap[j], i = j) { j = i * 2; if (j+1 <= n && (deap[j] > deap[j+1]) j++; } // try to put x at leaf i j = maxPartner(i); if (x > deap[j]) { deap[i] = deap[j]; maxInsert(j, x); } else { minInsert(i, x); } return key;

Deaps(8/8)

Leftist Trees(1/7) Support combine (two trees to one)

Leftist Trees(2/7) shortest(x) = 0 if x is an external node, otherwise 1+min(shortest(left(x)),shortest(right(x))}

Leftist Trees(3/7) Definition: shortest(left(x)) >= shortest(right(x))

Leftist Trees(4/7) Algorithm for combine(a, b) assume a.data <= b.data if (a.right is null) then make b be right child of a else combine (a.right, b) if shortest (a.right) > shortest (a.left) then exchange

Leftist Trees(5/7)

Leftist Trees(6/7)

Leftist Trees(7/7)

Binomial Heaps(1/10) Cost Amortization(分期還款) every operation in leftist trees costs O(logn) actual cost of delete in Binomial Heap could be O(n), but insert and combine are O(1) cost amortization charge some cost of a heavy operation to lightweight operations amortized Binomial Heap delete is O(log2n) A tighter bound could be achieved for a sequence of operations actual cost of any sequence of i inserts, c combines, and dm delete in Binomial Heaps is O(i+c+dmlogi)

Binomial Heaps(2/10) Definition of Binomial Heap Node: degree, child ,left_link, right_link, data, parent roots are doubly linked a points to smallest root

Binomial Heaps(3/10)

Binomial Heaps(4/10) Insertion Into A Binomial Heaps make a new node into doubly linked circular list pointed at by a set a to the root with smallest key Combine two B-heaps a and b combine two doubly linked circular lists to one

Binomial Heaps(5/10) Deletion Of Min Element

Binomial Heaps(6/10)

Binomial Heaps(7/10)

Binomial Heaps(8/10)

Binomial Heaps(9/10)

Binomial Heaps(10/10) Trees in B-Heaps is Binomial tree B0 has exactly one node Bk, k > 0, consists of a root with degree k and whose subtrees are B0, B1, …, Bk-1 Bk has exactly 2k nodes actual cost of a delete is O(logn + s) s = number of min-trees in a (original roots - 1) and y (children of the removed node)

Fibonacci Heaps(1/8) Definition delete, delete the element in a specified node decrease key This two operations are followed by cascading cut

Fibonacci Heaps(2/8) Deletion From An F-heap min or not min

Fibonacci Heaps(3/8) Decrease Key if not min, and smaller than parent, then delete

Fibonacci Heap(4/8) To prevent the amortized cost of delete min becomes O(n), each node can have only one child be deleted. If two children of x were deleted, then x must be cut and moved to the ring of roots. Using a flag (true of false) to indicate whether one of x’s child has been cut

Fibonacci Heaps(5/8) Cascading Cut

Fibonacci Heaps(6/8) Lemma Corollary the ith child of any node x in a F-Heap has a degree of at least i – 2, except when i=1 the degree is 0 Corollary Let Sk be the minimum possible number of descendants of a node of degree k, then S0=1, S1=2. From the lemma above, we got (2 comes from 1st child and root)

Fibonacci Heaps(7/8) That’s why the data structure is called Fibonacci Heap

Fibonacci Heaps(8/8) Application Of F-heaps