Lecture 7 Heaps and Priority Queues. Motivating Example 3 jobs have been submitted to a printer, the jobs have sizes 100, 10, 1 page. Average waiting.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

Trees Types and Operations
Heaps, Heap Sort, and Priority Queues. Sorting III / Slide 2 Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Heaps, Heap Sort, and Priority Queues
Priority Queue (Heap) & Heapsort COMP171 Fall 2006 Lecture 11 & 12.
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.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
Heaps and heapsort COMP171 Fall Sorting III / Slide 2 Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. Sizes: Job.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
Lec 6 Feb 17, 2011  Section 2.5 of text (review of heap)  Chapter 3.
Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Priority Queues  Queues: first-in first-out in printer schedule  Disadvantage: short job, important job need to wait  Priority queue is a data structure.
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.
1 Binary heaps binary tree that satisfy two properties –structural property (is a complete tree) –heap-ordering property (minimum item on top) Can have.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Lecture 7 Heaps and Priority Queues. Motivating Example 3 jobs have been submitted to a printer, the jobs have sizes 100, 10, 1 page. Average waiting.
PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University.
Heaps and heapsort COMP171 Fall 2005 Part 2. Sorting III / Slide 2 Heap: array implementation Is it a good idea to store arbitrary.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
Priority Queues, Heaps & Leftist Trees
Fundamental Structures of Computer Science March 02, 2006 Ananda Guna Binomial Heaps.
1 Priority Queues (Heaps)  Sections 6.1 to The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant.
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.
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 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Chapter 21 Binary Heap.
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
WEEK 3 Leftist Heaps CE222 Dr. Senem Kumova Metin CE222_Dr. Senem Kumova Metin.
CSC 213 – Large Scale Programming Lecture 15: Heap-based Priority Queue.
Data Structure & Algorithm II.  In a multiuser computer system, multiple users submit jobs to run on a single processor.  We assume that the time required.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Trees CS 105. L9: Trees Slide 2 Definition The Tree Data Structure stores objects (nodes) hierarchically nodes have parent-child relationships operations.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Dan Grossman Fall 2013.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2014W1 1.
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.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Kevin Quinn Fall 2015.
CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
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.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
CS 367 Introduction to Data Structures Lecture 8.
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)
Nov 2, 2001CSE 373, Autumn Hash Table example marking deleted items + choice of table size.
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 and Heap Sort. Sorting III / Slide 2 Background: Complete Binary Trees * A complete binary tree is the tree n Where a node can have 0 (for the leaves)
CSE373: Data Structures & Algorithms Priority Queues
CSE373: Data Structures & Algorithms
Heap Chapter 9 Objectives Define and implement heap structures
CS 201 Data Structures and Algorithms
Heaps (8.3) CSE 2011 Winter May 2018.
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
October 30th – Priority QUeues
Source: Muangsin / Weiss
Heaps, Heap Sort, and Priority Queues
Heaps, Heap Sort, and Priority Queues
Priority Queue and Binary Heap Neil Tang 02/12/2008
CE 221 Data Structures and Algorithms
Priority Queues (Chapter 6.6):
Priority Queues (Chapter 6):
Data Structures and Algorithm Analysis Priority Queues (Heaps)
Priority Queues Binary Heaps
Priority Queues (Heaps)
Presentation transcript:

Lecture 7 Heaps and Priority Queues

Motivating Example 3 jobs have been submitted to a printer, the jobs have sizes 100, 10, 1 page. Average waiting time for FIFO service, ( )/3 = 107 time units Average waiting time for shortest job first service, ( )/3 = time units Need to have a queue which does insert and deletemin Priority Queue

Common Implementation Linked list Insert in O(1) Find the minimum element in O(n), thus deletion is O(n) Search Tree (AVL tree) Insert in O(log n) Delete in O(log n) Search Tree is an overkill as it does many other operations

Heaps Almost complete binary tree All levels are complete except the lowest one (book says complete binary tree, I will define complete and almost complete whenever I use them) Note down example from the board If such a tree has height h, then it has between 2 h and 2 h + 1 nodes. Thus h is O(log n) Mistake in the expression in your book

Value of an element at a node is less than or equal to that of its descendants. Note down example from the board Mistake in your book Section 6.1, 6.2, 6.3 in book

Array implementaion We start from position 1 in the array. The first element contains the root Left child of element at position j is at position 2j, right child is at position 2j + 1 Parent of element at position j is at  j/2  Need to know the size of the heap. Would you recommend this implementation for any binary tree? Note down example from the board.

Insertion Find an empty position in the heap If some nodes have one child, then the empty position is the other child If all nodes have 0 or 2 children, then the empty position is the left child of a leaf. Insert the element there. Let the element be inserted at position j. If the parent of the element is more than the element, then interchange the parent and child Interchange elements at  j/2  and j.

If necessary, interchange elements at  j/2  with   j/2  /2  And so on till we reach the root. We are maintaining the heap property at every stage. Note down example from board Complexity? For (k = empty_pos; k >1; k =  k/2  ) If (Heap[k] < Heap[  k/2  ]) interchange(Heap[k], Heap[  k/2  ]) ;

DeleteMin Delete the root. Compare the two children of the root Make the lesser of the two root. An empty spot is created. Bring the lesser of the two children of the empty spot to the empty spot. A new empty spot is created. Continue

Note down example from the board. What is the complexity? Do we maintain the heap property in this procedure? Completeness is not preserved. Note down the example from the board. We can delete the last element in the heap, insert is at the empty spot at a leaf, and then move it upwards as necessary.

pseudocode Delete root; k =1; Do { If A[k] is empty, break; If A[k]  min(A[2k], A[2k + 1]), break; If A[2k] < A[2k + 1] j = 2k, else j = 2k+1; A[k] = A[j]; k = j; }Insert A[currsize] at A[k]; }

Heap Sort You want to sort n real numbers. Insert them in a heap; Deletemin n times; Complexity O(h), h is the depth of the tree

Increase Key Need to increase the value of an element. Increase the value of the element, Interchange it with the lesser of its children if it is greater than any of its children, Continue doing this till you reach a leaf

Decrease Key Need to decrease the value of an element. Decrease the value of the element, Interchange it with its parent if it is less than its parent, Continue doing this till you reach the root

Tighter Analysis of Complexity of Build Heap Suppose we have n elements. We want to build a heap of n elements. We present an O(n) algorithm to do so. Insert these elements in n positions of an array. Is an almost complete binary tree. But does not satisfy the heap order So we need to interchange these elements to obtain heap order

Parents are at position n/2 to 1. If we percolate these down suitably, (interchange with lesser of the two children, if it is less than either of the two children, and so on), then we will get heap order. For (j = n/2; j--; j >0) Percolate Down (j);

Percolate Down (j) { If (2j > n) break; If Heap[j]  min(Heap[2j], Heap(2j+1), break; If (Heap[2j] < Heap[2j + 1]) { interchange(Heap[j], Heap[2j]); Percolate Down (2j);} else { interchange(Heap[j], Heap[2j+1]); Percolate Down (2j+1);} }

Complexity Analysis Complexity of percolating down a single element is O(h) where h is the height of the element. Overall complexity is O(sum of the heights of all elements) We will show that sum of the heights of all elements is O(n). For an almost complete binary tree, There is 1 element at height h, 2 at height h-1 4 at height h-2…… 2 i at height h-i

The number at height 0 is between 1 and 2 h However, these elements do not contribute to the sum of the heights. Thus sum of the heights is  j=0 h-1 2 j (h-j) This is equal to 2 h (h+1) We have seen that the number of nodes n is greater than 2 h and 2 h is greater than (1/4) (2 h –h) Thus sum of heights is less than 4n Thus sum of heights is O(n)