ICS 353: Design and Analysis of Algorithms Heaps and the Disjoint Sets Data Structures King Fahd University of Petroleum & Minerals Information & Computer.

Slides:



Advertisements
Similar presentations
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R4. Disjoint Sets.
Advertisements

COL 106 Shweta Agrawal and Amit Kumar
1 Union-find. 2 Maintain a collection of disjoint sets under the following two operations S 3 = Union(S 1,S 2 ) Find(x) : returns the set containing x.
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)
BY Lecturer: Aisha Dawood. Heapsort  O(n log n) worst case like merge sort.  Sorts in place like insertion sort.  Combines the best of both algorithms.
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.
More sorting algorithms: Heap sort & Radix sort. Heap Data Structure and Heap Sort (Chapter 7.6)
Heaps Heaps are used to efficiently implement two operations:
Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees,
Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on Binary Heaps: enqueue dequeue deleting an.
Lec 6 Feb 17, 2011  Section 2.5 of text (review of heap)  Chapter 3.
© 2004 Goodrich, Tamassia Heaps © 2004 Goodrich, Tamassia Heaps2 Priority Queue Sorting (§ 8.1.4) We can use a priority queue to sort a set.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
Priority Queues1 Part-D1 Priority Queues. Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is.
Lecture 11 Binary Heap King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
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.
1 Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on Binary Heaps: enqueue dequeue deleting.
Heapsort Based off slides by: David Matuszek
Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees.
Computer Science and Software Engineering University of Wisconsin - Platteville 12. Heap Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++ Plus.
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.
Priority Queues and Disjoint Sets CSCI 2720 Spring 2005.
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.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Chapter 21 Binary Heap.
Computer Sciences Department1. Sorting algorithm 3 Chapter 6 3Computer Sciences Department Sorting algorithm 1  insertion sort Sorting algorithm 2.
CMSC 341 Disjoint Sets. 8/3/2007 UMBC CMSC 341 DisjointSets 2 Disjoint Set Definition Suppose we have an application involving N distinct items. We will.
CMSC 341 Disjoint Sets Textbook Chapter 8. Equivalence Relations A relation R is defined on a set S if for every pair of elements (a, b) with a,b  S,
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues.
CHAPTER 8 THE DISJOINT SET ADT §1 Equivalence Relations 【 Definition 】 A relation R is defined on a set S if for every pair of elements (a, b), a, b 
Heapsort. What is a “heap”? Definitions of heap: 1.A large area of memory from which the programmer can allocate blocks as needed, and deallocate them.
8 January Heap Sort CSE 2011 Winter Heap Sort Consider a priority queue with n items implemented by means of a heap  the space used is.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Heaps & Priority Queues
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
1Computer Sciences. 2 HEAP SORT TUTORIAL 4 Objective O(n lg n) worst case like merge sort. Sorts in place like insertion sort. A heap can be stored as.
Priority Queues and Heaps Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
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)
Sorting Cont. Quick Sort As the name implies quicksort is the fastest known sorting algorithm in practice. Quick-sort is a randomized sorting algorithm.
Heaps, Heap Sort, and Priority Queues. Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two children * A node that.
Heaps (8.3) CSE 2011 Winter May 2018.
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
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,
CSCE 3100 Data Structures and Algorithm Analysis
Bohyung Han CSE, POSTECH
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Heap Sort Example Qamar Abbas.
The Heap Data Structure
CSCI2100 Data Structures Tutorial 7
Heaps 11/27/ :05 PM Heaps Heaps.
Binary Tree Application Operations in Heaps
Tree Representation Heap.
Heaps A heap is a binary tree.
© 2013 Goodrich, Tamassia, Goldwasser
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
ICS 353: Design and Analysis of Algorithms
Heap Sort The Heap Data Structure
ICS 353: Design and Analysis of Algorithms
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Heaps & Multi-way Search Trees
Disjoint Sets Textbook Chapter 8
Heaps 9/29/2019 5:43 PM Heaps Heaps.
Presentation transcript:

ICS 353: Design and Analysis of Algorithms Heaps and the Disjoint Sets Data Structures King Fahd University of Petroleum & Minerals Information & Computer Science Department 1

ICS 353: Design and Analysis of AlgorithmsHeaps and Disjoint Sets Data Structures Reading Assignment Chapter 4 from the text book. 2

ICS 353: Design and Analysis of AlgorithmsHeaps and Disjoint Sets Data Structures Heaps Heaps are used to efficiently implement two operations: Insert Find Max Definition: A Heap is a complete binary tree with each node satisfying the heap property: Max-heap: The key stored in the node is greater than or equal to both keys stored in its children, if any. Min-heap: The key stored in the node is less than or equal to both keys stored in its children, if any. 3

ICS 353: Design and Analysis of AlgorithmsHeaps and Disjoint Sets Data Structures Example of a Min-Heap and Its Implementation

ICS 353: Design and Analysis of AlgorithmsHeaps and Disjoint Sets Data Structures Heap Operations The heap data structure supports the following operations delete_max[H] (or delete_min[H]): insert[H,x]: delete[H,i]: makeheap[A]: Implementation of the above operations necessitates the introduction of two subprograms, viz. sift-up and sift-down. We restrict our discussion to max-heaps. 5

ICS 353: Design and Analysis of AlgorithmsHeaps and Disjoint Sets Data Structures Sift Up In a max-heap, if the value at a node, other than the root, becomes greater than its parent, the heap property can be restored by swapping the current node and its parent, repeating this process for the parent if necessary, until the key at the node is less than or equal to that of the parent. we reach the root. 6

ICS 353: Design and Analysis of AlgorithmsHeaps and Disjoint Sets Data Structures Sift Up Algorithm Procedure Sift-Up Input: H[1..n], i where 1  i  n. Output: H, where no node is greater than its parent on the path from node i to the root. done := false; if i = 1 then exit; /* Node i is the root */ repeat if key(H[i]) > key(H[  i/2  ]) then swap(H[i],H[  i/2  ]); else done := true; end if; i :=  i/2  ; until i=1 or done; 7

ICS 353: Design and Analysis of AlgorithmsHeaps and Disjoint Sets Data Structures Example and Time Complexity of Sift Up What is the cost of Sift Up in the worst case? 8

ICS 353: Design and Analysis of AlgorithmsHeaps and Disjoint Sets Data Structures Sift-Down In a max-heap, if the value at a node becomes less than the key of any of its children, the heap property can be restored by swapping the current node and the child with maximum key value, repeating this process if necessary until the key at the node is greater than or equal to the keys of both children. we reach a leaf. 9

ICS 353: Design and Analysis of AlgorithmsHeaps and Disjoint Sets Data Structures Sift-Down Algorithm Procedure Sift-Down Input: H[1..n], i where 1  i  n. Output: H[i] is percolated down, if needed, so that it’s not smaller than its children. done := false; if (2*i) > n then exit; { is a leaf node } repeat i = 2*i; if (i+1)  n and key(H[i+1]) > key(H[i]) then i := i+1; if key(H[  i/2  ]) < key(H[i]) then swap(H[i],H[  i/2  ]); else done := true; end if; until 2*i > n or done; 10

ICS 353: Design and Analysis of AlgorithmsHeaps and Disjoint Sets Data Structures Example and Time Complexity of Sift Down What is the cost of Sift Down in the worst case? 11

ICS 353: Design and Analysis of AlgorithmsHeaps and Disjoint Sets Data Structures Insertion into a Heap To insert an element x into a heap: Increase the size of the heap by 1 Append x to the end of the heap Run the Sift-Up algorithm on x. Algorithm insert Input: A heap H[1..n] & a heap element x. Output: A new heap H[1..n+1] with x being one of its elements. 1. n := n + 1; 2. H[n] := x; 3. Sift-Up(H, n); The cost of this operation in the worst case is: 12

ICS 353: Design and Analysis of AlgorithmsHeaps and Disjoint Sets Data Structures Deletion from a Heap Algorithm Delete Input: A nonempty heap H[1..n] and i where 1  i  n. Output: H[1..n-1] after H[i] is removed. 1. x := H[i]; y := H[n]; 2. n := n – 1; 3. if i = n+1 then exit 4. H[i] := y; 5. if key(y)  key(x) then 6 Sift-Up(H, i) 7. else Sift-Down(H, i) end if The cost of deletion, in the worst case, is: 13

ICS 353: Design and Analysis of AlgorithmsHeaps and Disjoint Sets Data Structures Delete-Max What is the algorithm for deleting the maximum element? 14

ICS 353: Design and Analysis of AlgorithmsHeaps and Disjoint Sets Data Structures Make-Heap Algorithm Work from high end of array to low end. Call SiftDown for each item. Don’t need to call SiftDown on leaf nodes. Algorithm Make-Heap Input: Array A[1..n] of n elements Output: Max-heap A[1..n] for i=  n/2  downto 1 SiftDown(A,i); end for; 15

ICS 353: Design and Analysis of AlgorithmsHeaps and Disjoint Sets Data Structures Make-Heap Cost Cost for heap construction: 16

ICS 353: Design and Analysis of AlgorithmsHeaps and Disjoint Sets Data Structures Heap-Sort Using previous operations, one can develop a sorting algorithm for an array of n elements. What is the cost of this sorting technique? 17

ICS 353: Design and Analysis of AlgorithmsHeaps and Disjoint Sets Data Structures Disjoint Sets Data Structures Objective Study a data structure that can represent disjoint sets and support operations related to the manipulation of disjoint sets in an efficient manner. Disjoint-Sets Representation: Parent-Pointer Implementation Disjoint-Sets Operations: Union/Find 18

ICS 353: Design and Analysis of AlgorithmsHeaps and Disjoint Sets Data Structures Parent Pointer Implementation For Forests EFGKJIHLABCD Index Node ABCDEFGHIJKL Parent

ICS 353: Design and Analysis of AlgorithmsHeaps and Disjoint Sets Data Structures Equivalence Class Problem The parent pointer representation is good for: Answering Q: “Do these two elements belong to the same equivalence class?” Efficiently compute the union of two equivalence classes Hence, the parent pointer implementation supports the above two important functionalities for disjoint sets efficiently. Find Union 20

ICS 353: Design and Analysis of AlgorithmsHeaps and Disjoint Sets Data Structures Union/Find int FIND(int curr) { while (parent[curr]!= 0) curr = parent[curr]; return curr; // At root } void UNION(int a, int b) { int root1 = FIND(a); // Find root for a int root2 = FIND(b); // Find root for b if (root1 != root2) parent[root1] = root2; } 21

ICS 353: Design and Analysis of AlgorithmsHeaps and Disjoint Sets Data Structures Example 1 Carry out the Union-Find algorithm for the following set of equivalences assuming there are 8 objects indexed by the values 1 through 8. (1,2), (1,3), (2,4), (4,5), (6,7), (8,7) What do you notice regarding the number of comparisons for carrying out the Union-Find operations? 22

ICS 353: Design and Analysis of AlgorithmsHeaps and Disjoint Sets Data Structures Improving Union-Find Algorithms Union-By-Rank Heuristic Path Compression 23

ICS 353: Design and Analysis of AlgorithmsHeaps and Disjoint Sets Data Structures Union by Rank Heuristic Objective: Want to keep the depth small. Procedure: To carry out the union of the two trees rooted at x and y, respectively, make the root node of the tree with higher rank the root of the Union tree with one of its children being the root node of the other tree. The rank of a node is the height of that node in the tree When the ranks of the two root nodes are equal, make the root of the second tree the parent of the root of the first tree. 24

ICS 353: Design and Analysis of AlgorithmsHeaps and Disjoint Sets Data Structures Path Compression In order to reduce the height of the tree further, during the FIND(x) operation, we can make each node on the path from x up to the child of the root all point to the root. This is called path compression int FIND(int curr) { if (parent[curr] == 0) return curr; return parent[curr]=FIND(parent[curr]); } 25

ICS 353: Design and Analysis of AlgorithmsHeaps and Disjoint Sets Data Structures Example 2 Using the union-by-rank heuristic and path compression, show the result from the following series of equivalences on a set of objects indexed by the values 1 through 16, assuming initially that each element in the set is in an equivalence class containing it alone. When the ranks of the two trees are equal, make the root of the tree containing the second object to be the root of the tree: (1,3) (2,3) (4,5) (4,2) (10,12) (13,15) (13,10) (7,8) (9,11) (8,15) (11, 2) (4,7) (9,13) 26

ICS 353: Design and Analysis of AlgorithmsHeaps and Disjoint Sets Data Structures Analysis of Union and Find Using Union By Rank Heuristic Theorem: rank(parent(x))  rank(x) + 1. Proof: Theorem: The value of rank(x) is initially zero and increases in subsequent union operations until x is no longer a root. Once x becomes a child of another node, its rank never changes. Proof: Lemma: The number of nodes in a tree with root x is at least 2 rank(x). Proof: Theorem: A sequence of m interspersed union-by-rank and find operations costs O(m log n ) Proof: 27

ICS 353: Design and Analysis of AlgorithmsHeaps and Disjoint Sets Data Structures Time Complexity of Union and Find Using Union By Rank and Path Compression Heuristics Theorem: A sequence of m interspersed union-by-rank and find operations using path compression costs O(m log * n) operations, where log * n = min {i | log log …log n  1} for n > 1 = 0 for n=0,1 Proof: Out of the scope of an undergraduate course! i times 28