IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture24.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

CMSC 341 Binary Heaps Priority Queues. 8/3/2007 UMBC CSMC 341 PQueue 2 Priority Queues Priority: some property of an object that allows it to be prioritized.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture27.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture.
Advanced Data Structures Chapter 16. Priority Queues Collection of elements each of which has a priority. Does not maintain a first-in, first-out discipline.
CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
CS 206 Introduction to Computer Science II 11 / 04 / 2009 Instructor: Michael Eckmann.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
Priority Queues  Queues: first-in first-out in printer schedule  Disadvantage: short job, important job need to wait  Priority queue is a data structure.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Dr. Andrew Wallace PhD BEng(hons) EurIng
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
CSE 373 Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
1 Priority Queues (Heaps)  Sections 6.1 to The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant.
Heapsort Based off slides by: David Matuszek
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
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.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture8.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
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.
data ordered along paths from root to leaf
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture17.
Sorting Dr. Yingwu Zhu. Heaps A heap is a binary tree with properties: 1. It is complete Each level of tree completely filled Except possibly bottom level.
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:
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
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.
Chapter 13 Priority Queues. 2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-in-first-out The “smallest”
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R3. Priority Queues.
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
Sorting Dr. Yingwu Zhu. Heaps A heap is a binary tree with properties: 1. It is complete Each level of tree completely filled Except possibly bottom level.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
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.
CSE 373: Data Structures and Algorithms Lecture 11: Priority Queues (Heaps) 1.
CS 367 Introduction to Data Structures Lecture 8.
Priority Queues and Heaps Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
1 Priority Queues (Heaps)  Sections 6.1 to Priority Queues  Regular queues which supports –First In, First Out –Enqueue(): add a new element.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Priority Queues A priority queue is an ADT where:
CS 201 Data Structures and Algorithms
Heaps (8.3) CSE 2011 Winter May 2018.
Priority Queues (Heaps)
Priority Queues and Heaps
Source: Muangsin / Weiss
Heap Sort Example Qamar Abbas.
Priority Queues (Heaps)
ADT Heap data structure
Priority Queue & Heap CSCI 3110 Nan Chen.
Priority Queues.
CSE 373: Data Structures and Algorithms
Tree Representation Heap.
CE 221 Data Structures and Algorithms
Sorting Dr. Yingwu Zhu.
Priority Queues (Heaps)
Heaps & Multi-way Search Trees
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Priority Queues (Heaps)
Presentation transcript:

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Binary Heap

2 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 A BC D E FG HIJ ABCDEFGHIJ Review Complete binary tree: the tree is completely filled, with possible exception of the bottom level, which is filled from left to right. Array Implementation:

3 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Priority Queue Definition Operations Binary Heap Operations Properties Representation Heap Sort Outline

4 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 A Priority Queue A special kind of queue that features slightly different rules: Enqueue operation doesn’t always add new items to the rear of the queue. Instead, it insert each new item into the queue as determined by its priority. Remember Assignment 1: Passport Processing? Essentially, PQ is an Abstract Data Type where access is restricted to the minimum item only.

5 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Binary Heap Tree-based data structure where access is restricted to the minimum item only. Basic operations: Find the minimum item in constant time Insert a new item in logarithmic worst-case time Delete the minimum item in logarithmic worst-case time

6 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Structural Property Data is stored in a complete binary tree The tree is balanced, so all operations are guaranteed O(log n) worst case Can be implemented as array or list Ordering Property Heap Order: Parent  Child Properties P X P  X

7 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr Which Of These Are Binary Heaps?

8 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Root at location 0 Left child of i is at location 2i + 1 Right child of i is at location 2i + 2 = 2(i + 1) Parent of i is at location (i - 1) / Heap Representation

9 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Find Minimum How to access the minimum element? Simple! Return root. Constant time operation

10 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Insertion Create a “hole” in the next available location. Where? Bottom level, rightmost leaf. Move it up towards root to maintain heap order This is called “percolating up”

11 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr Insertion Insert 2 (Percolate Up)

12 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr Insertion Insert 2 (Percolate Up)

13 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Insert 14 Insertion

14 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Insert 14 Insertion

15 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Insert 14 Insertion

16 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Delete Minimum Only the root can be deleted. Why? Access is restricted to minimum item! Replace “hole” in the root with former last item. “Percolate down” towards leaf to maintain heap order Compare with smallest child, recurse.

17 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr Delete Minimum Percolate Down

18 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr Delete Minimum

19 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr Delete Minimum: Completed

20 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr Delete Min (Alternative)

21 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr Delete Min (Alternative) Percolate Down

22 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Delete Min (Alternative)

23 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr Delete Min (Alternative)

24 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Delete Min (Alternative)

25 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Delete Min (Alternative) Instead of lots of swapping, simply store value at the beginning, move “hole” around, and plug value in at the end.

26 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Heap Operation sequence: Insert: 40, 20, 5, 55, 76, 31, 3 Delete Min Insert: 10, 22 Delete Min

27 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Heap Class Definition & Constructor public class MyHeap implements PriorityQueue { // Array implementation private int[] data; int currentSize; // Constructor public MyHeap(int maxSize) { data = new int[maxSize]; currentSize = 0; }

28 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Heap Indexing Methods // Returns index of an element’s parent private int parentOf(int i) {return (i - 1) / 2;} // Returns index of an element’s left child private int leftChildOf(int i) {return 2 * i + 1;} // Returns index of an element’s right child private int rightChildOf(int i) {return 2 * i + 2;} // Returns minimum value (i.e. heap root), a.k.a. findMin public int peek() {return data[0];}

29 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Removal & Insertion public int poll() // a.k.a. remove, dequeue { int minVal = peek(); data[0] = data[--currentSize]; if(currentSize > 1) percolateDown(0); return minVal; } public void add(int value) // a.k.a. offer { data[currentSize++]=value; percolateUp(currentSize-1); } (Actually, still need to check if polling empty array, adding to full array…) Implement?

30 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Percolate Up protected void percolateUp (int leaf) { int parent = parentOf(leaf); int value = data[leaf]; while(leaf>0 && value<data[parent]) { data[leaf]=data[parent]; leaf = parent; parent = parentOf(leaf); } data[leaf]=value; }

31 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Percolate Down private void percolateDown (int hole) { int value = data[hole]; while(leftChildOf(hole) < size()) { int child = leftChildOf(hole); if(rightChildOf(hole) < size() && data[rightChildOf(hole)] < data[child]) child = rightChildOf(hole); if(data[child] < value) { data[hole] = data[child]; hole = child; } data[hole] = value; }

32 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Fix Heap / Heapify The fixHeap operation takes a complete tree that does not have heap order and reinstates it. N insertions could be done in O(n log n) But we can make a fixHeap that takes O(n) time!

33 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 fixHeap Algorithm Inefficient version: fixHeap left subtree fixHeap right subtree percolateDown root However, we don’t need the recursive calls! Why? If we call percolateDown in reverse level order from the bottom (non- leaves) upwards, by the time we call percolateDown on node n, it’s children are already guaranteed to be heaps! n

34 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Fix Heap / Heapify Call percolateDown(6)

35 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Fix Heap / Heapify Call percolateDown(5)

36 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Fix Heap / Heapify Call percolateDown(4)

37 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Fix Heap / Heapify Call percolateDown(3)

38 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Fix Heap / Heapify Call percolateDown(2)

39 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Fix Heap / Heapify Call percolateDown(1)

40 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Fix Heap / Heapify Call percolateDown(0)

41 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr Max Heap Heaps can also store maximum item (instead of min).

42 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr Heap after the first deleteMax

43 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr Heap after second deleteMax

44 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Heap Sort 1. Create a heap tree 2. Remove the root elements from the heap one at a time, recomposing the heap

45 Ruli Manurung (Fasilkom UI)IKI10100: Lecture24 th Apr 2007 Priority queue can be implemented using binary heap Properties of binary heap structural property: complete binary tree ordering property: Parent  Child Operations of binary heap insertion: O(log n) find min: O(1) delete min: O(log n) heapify: O(n) Binary heap can be used for sorting Summary