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.

Slides:



Advertisements
Similar presentations
Binomial Heaps. Heap Under most circumstances you would use a “normal” binary heap Except some algorithms that may use heaps might require a “Union” operation.
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)
Advanced Data structure
Pairing Heaps. Experimental results suggest that pairing heaps are actually faster than Fibonacci heaps.  Simpler to implement.  Smaller runtime overheads.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
1 Pertemuan 19 Leftist Tree Heap Matakuliah: T0026/Struktur Data Tahun: 2005 Versi: 1/1.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
CS 253: Algorithms Chapter 6 Heapsort Appendix B.5 Credit: Dr. George Bebis.
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.
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.
Binomial Heaps. Min Binomial Heap Collection of min trees
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.
Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
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.
Binomial Heaps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lecture 11 Tuesday, 12/4/01 Advanced Data Structures Chapters.
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 & Leftist Trees
Fundamental Structures of Computer Science March 02, 2006 Ananda Guna Binomial Heaps.
Heapsort Based off slides by: David Matuszek
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.
Chapter 9 Heap Structures
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
單元7: Heap 定義 for priority queues Leftist trees Binomial heap
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Chapter 21 Binary 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.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
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.
Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue. Nov 4,
Double-Ended Priority Queues Primary operations  Insert  Remove Max  Remove Min Note that a single-ended priority queue supports just one of the above.
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.
Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
CS 367 Introduction to Data Structures Lecture 8.
Fibonacci Heaps. Analysis FibonacciAnalysis.ppt Video  iew/cop5536sahni
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
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.
Chapter 11: Multiway Search Trees
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,
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.
Double-Ended Priority Queues
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
CMSC 341 Lecture 13 Leftist Heaps
Binomial Heaps Chapter 19.
Binary 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.
Interval Heaps Complete binary tree.
Pairing Heaps Actual Complexity.
Initializing A Max Heap
Fibonacci Heaps Remove arbitrary is useful in (for example) correspondence structures.
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Binary and Binomial Heaps
Heaps By JJ Shepherd.
Priority Queues Supports the following operations. Insert element x.
Pairing Heaps Actual Complexity
Heaps & Multi-way Search Trees
A Heap Is Efficiently Represented As An Array
Presentation transcript:

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 from the slides of Prof. Hsin-Hsi Chen (NTU). Chapter 9 Heap Structures

Outline MIN-MAX Heaps Deaps Leftist Trees Binomial Heaps Fibonacci Heaps

MIN-MAX Heaps Definition

MIN-MAX Heaps Complete binary tree. Set root on a min level. A node x in min level would have smaller key value than all its descendents. (x is a min node.)

MIN-MAX Heaps min max min max

MIN-MAX Heaps Insertion into a min-max heap

min max min max 5

Insertion into a min-max heap min max min max 10

Insertion into a min-max heap min max min max 10

Insertion into a min-max heap Check if it satisfies min heap. (Compare with its parent.) If no, move the key of current parent to current position. If yes, skip.

Insertion into a min-max heap Initial Heap={7,70,40,30,9,10, 15,45,50,30,20,12} *n=13 item=80 parent=6 80>10 → verify_max(heap,13,80) min max min max 80

Insertion into a min-max heap Input verify_max(Heap,13,80) grandparent=3 80>40 → heap[13]=heap[3] i=3 grandparent= min max min max 80

Insertion into a min-max heap Input verify_max(Heap,3,80) → grandparent=null break; heap[3]=80; min max min max 40

Insertion into a min-max heap The time complexity of insertion into a min-max heap with n elements is O(log n). A min-max heap with n elements has O(log n) levels.

MIN-MAX Heaps Deletion of min element

The smallest element is in the root. We do the deletion as follows: 1. Remove the root node and the node x which is the end of the heap. 2. Reinsert the key of x into the heap.

Deletion of min element min max min max

Deletion of min element min max min max 12

Deletion of min element The reinsertion may have 2 cases: 1. No child. (Only one node in the heap) i i It means the item is the only one element, so it should be at root in heap.

Deletion of min element 2. The root has at least one child Find the min value. (Let this be node k.) a. A item.key ≦ heap[k].key i i It means the item is the min element, and the min element should be at root in heap.

Deletion of min element b. item.key> heap[k].key and k is child of root. i Since k is in max level, it has no descendants. k k i

Deletion of min element c. item.key> heap[k].key and k is grandchild of root. i Example (p>i,): We should make sure the node in max level contains the largest key. Then redo insertion to the subtree with the root in red line. p k k p i

Deletion of min element Get the min value of the heap. Move k to the root of this heap. Swap i and p in case 2c.

Deletion of min element Deletion of min element of a min-max heap with n elements need O(log n) time. In each iteration, i moves down two levels. Since a min-max heap is a complete binary tree, heap has O(log n) levels.

Deaps Definition

Deaps Complete binary tree. Either empty or satisfies the properties 1. The root contains no element. 2. The left subtree is a min-heap. 3. The right subtree is a max-heap. 4. If the right subtree is not empty. Let i be any node in left subtree, and j be the corresponding node in the right subtree. If no, choose the parent one. i_key ≦ j_key.

Deaps The relation between i and j Ex1: i=4; j=4+2^(2-1)=6; Ex2: i=9; j=9+2^(3-1)=13; j>12 j=6;

Deaps

Deaps Insertion into a deap

The insertion steps 1. max_heap(n): Check iff n is a position in the max-heap of the deap. 2. min_partner(n) or max_partner(n) : Compute the min-heap/max-heap node that corresponding to n. ( / ) 3. Compare key of i and j to satisfy deap. 4. min_insert or max_insert.

Insertion into a deap j

Insertion into a deap j i

Insertion into a deap

Insertion into a deap

Insertion into a deap Step 1. max_heap. Step 2. min_partner. Step 3. Compare key of i and j. Step 4. min_insert or max_insert.

Insertion into a deap The time complexity is O(log n) as the height of the deap is O(log n).

Deaps Deletion of min element

Insertion into a deap The insertion steps 1. Save the last element as temp and remove this node from deap. 2. Find the node with smaller key from the children of removed minimum element and loop down until reaching leaves. 3. Insert temp into the left subtree of deap.

Deletion of min element temp

Deletion of min element j temp:20 i

Deletion of min element j i temp:20

Deletion of min element i temp:20

Deletion of min element i temp:20

Deletion of min element

Deletion of min element Step 1. Save the min element. Step 2. Find the node with smaller key. Step 3. (Exercise 2).

Deletion of min element The time complexity is O(log n) as the height of the deap is O(log n).

Leftist Trees Definition

Leftist Trees Linked binary tree. Can do everything a heap can do and in the same asymptotic complexity. Can meld two leftist tree priority queues in O(log n) time. For any node x in an extended binary tree, let shortest(x) be the length of a shortest path from x to an external node in the subtree rooted at x.

Leftist Trees AG BHCI EDFJ Two binary trees

Extended binary trees Leftist Trees AG BHCI EDFJ

The number inside each internal node x is shortest(x). Leftist Trees

Definition A leftist tree is a binary tree such that if it is not empty, then for every internal node x:

Leftist Trees By the definition Let x be the root of a leftist tree that has n internal nodes. a) x shortest(x) A binary tree whose shortest height is shortest(x) means for every path from root to leaf has at least shortest(x) nodes. Such that it has at least 2 shortest(x) - 1 nodes.

Leftist Trees b) The rightmost root to external node path is the shortest root to external node path. x shortest(x) Base on the definition of leftist tree.

Leftist Trees Definition A min-leftist tree (max leftist tree) is a leftist tree in which the key value in each node is no larger (smaller) than the key values in its children (if any)

Leftist Trees Combination of leftist trees

The combination step (min-leftist trees) 1. Choose minimum root of the two trees, A and B. 2. Leave the left subtree of smaller root (suppose A) unchanged and combine the right subtree of A with B. Back to step 1, until no remaining vertices. 3. Compare shortest(x) and swap to make it satisfy the definition of leftist trees.

Combination of leftist trees Step 1. Choose smaller root and set as a. Step 2. If a has no right_chlid, set b as a ’ s right_child. Else, recursively combine a ’ s right_child and b. Step 3. Make the combined tree satisfied the leftist tree property.

Combination of leftist trees

Combination of leftist trees

Combination of leftist trees

Combination of leftist trees

Combination of leftist trees

Combination of leftist trees

Combination of leftist trees Both insert and delete min operations can be implemented by using the combine operation. Insert: Treat the inserting node as a single node binary tree. Combine with the original one. Delete: Remove the node can get two separate subtrees. Combine the two trees.

Binomial Heaps Definition

Binomial Trees [Definition] Binomial trees A binomial tree B k has 2 k nodes with height be k. BkBk B k-1 B0B0 B1B1 B2B2 B3B3

Binomial Trees It has nodes at depth i. The ith child of root is the root of subtree B i-1. B4B4 B2B2 B3B3 B1B1 B0B0 Depth 1 : 4 nodes. Depth 2 : 6 nodes. Depth 3 : 4 nodes.

Binomial Heaps Collection of min (max) trees. The min trees should be Binomial trees

Binomial Heaps The representation of B-heap: Degree: number of children a node has. Child: point to any one of its children. Left_link, Right_link: maintain doubly linked circular list of siblings. The position of pointer a is the min element.

Binomial Heaps Siblings parent child a pointer a

Binomial Heaps Combination of binomial heaps

a 40

Combination of binomial heaps a 40

Pairwise combine Combination of binomial heaps a 40

Pairwise combine Combination of binomial heaps a 40

Pairwise combine Combination of binomial heaps a 40

Pairwise combine Combination of binomial heaps a 40

Pairwise combine Combination of binomial heaps a 40

Pairwise combine Combination of binomial heaps a 40

Pairwise combine Combination of binomial heaps a 40

Pairwise combine Combination of binomial heaps a 40

Combination of binomial heaps a 40

Combination of binomial heaps The insertion is to combine a single vertex b-heap to original b-heap. After we delete the min element, we get several b-heaps that originally subtrees of the removed vertex. Then combine them together.

Time complexity

Fibonacci Heaps Definition

Fibonacci Heaps Collection of min (max) trees. The min trees need not be Binomial trees. B-heaps are a special case of F-heaps. So that what B-heaps can do can be done in F-heaps. More than that, F-heap may delete an arbitrary node and decrease key.

Fibonacci Heaps Deletion and Decrease key

Deletion

Deletion and Decrease key Deletion

Deletion and Decrease key Deletion

Deletion and Decrease key Decrease key

Deletion and Decrease key Decrease key

Deletion and Decrease key Decrease key

Time complexity