1 Priority Queues A priority queue is an ADT where: –Each element has an associated priority –Efficient extraction of the highest-priority element is supported.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

Analysis of Algorithms
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Sorting Algorithms and Average Case Time Complexity
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
CS 253: Algorithms Chapter 6 Heapsort Appendix B.5 Credit: Dr. George Bebis.
Analysis of Algorithms CS 477/677
More sorting algorithms: Heap sort & Radix sort. Heap Data Structure and Heap Sort (Chapter 7.6)
Comp 122, Spring 2004 Heapsort. heapsort - 2 Lin / Devi Comp 122 Heapsort  Combines the better attributes of merge sort and insertion sort. »Like merge.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 7 Heapsort and priority queues Motivation Heaps Building and maintaining heaps.
3-Sorting-Intro-Heapsort1 Sorting Dan Barrish-Flood.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
David Luebke 1 7/2/2015 Merge Sort Solving Recurrences The Master Theorem.
PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University.
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 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
1 Priority Queues (Heaps)  Sections 6.1 to The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant.
Sorting Algorithms (Part II) Slightly modified definition of the sorting problem: input: A collection of n data items where data item a i has a key, k.
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.
2IL50 Data Structures Spring 2015 Lecture 3: Heaps.
David Luebke 1 10/3/2015 CS 332: Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
Heaps, Heapsort, Priority Queues. Sorting So Far Heap: Data structure and associated algorithms, Not garbage collection context.
Binary Heap.
2IL50 Data Structures Fall 2015 Lecture 3: Heaps.
Chapter 21 Binary Heap.
Computer Sciences Department1. Sorting algorithm 3 Chapter 6 3Computer Sciences Department Sorting algorithm 1  insertion sort Sorting algorithm 2.
David Luebke 1 6/3/2016 CS 332: Algorithms Heapsort Priority Queues Quicksort.
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.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
Data Structure & Algorithm Lecture 5 Heap Sort & Binary Tree JJCAO.
CS 2133: Data Structures Quicksort. Review: Heaps l A heap is a “complete” binary tree, usually represented as an array:
1 Analysis of Algorithms Chapter - 03 Sorting Algorithms.
1 Algorithms CSCI 235, Fall 2015 Lecture 14 Analysis of Heap Sort.
David Luebke 1 12/23/2015 Heaps & Priority Queues.
Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 1 Adam Smith L ECTURES Priority Queues and Binary Heaps Algorithms.
CPSC 311 Section 502 Analysis of Algorithm Fall 2002 Department of Computer Science Texas A&M University.
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.
ΔΟΜΕΣ ΔΕΔΟΜΕΝΩΝ & ΑΡΧΕΙΩΝ
CSC 413/513: Intro to Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
Chapter 6: Heapsort Combines the good qualities of insertion sort (sort in place) and merge sort (speed) Based on a data structure called a “binary heap”
Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
CSE 5392 Fall 2005 Week 4 Devendra Patel Subhesh Pradhan.
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:
David Luebke 1 2/5/2016 CS 332: Algorithms Introduction to heapsort.
Heapsort A minimalist's approach Jeff Chastine. Heapsort Like M ERGE S ORT, it runs in O(n lg n) Unlike M ERGE S ORT, it sorts in place Based off of a.
Priority Queues and Heaps Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
Sept Heapsort What is a heap? Max-heap? Min-heap? Maintenance of Max-heaps -MaxHeapify -BuildMaxHeap Heapsort -Heapsort -Analysis Priority queues.
Heapsort Lecture 4 Asst. Prof. Dr. İlker Kocabaş.
6.Heapsort. Computer Theory Lab. Chapter 6P.2 Why sorting 1. Sometimes the need to sort information is inherent in a application. 2. Algorithms often.
1 Algorithms CSCI 235, Fall 2015 Lecture 13 Heap Sort.
Sorting Cont. Quick Sort As the name implies quicksort is the fastest known sorting algorithm in practice. Quick-sort is a randomized sorting algorithm.
Priority Queues A priority queue is an ADT where:
"Teachers open the door, but you must enter by yourself. "
Heaps, Heapsort, and Priority Queues
Heaps, Heap Sort and Priority Queues
Heap Sort Example Qamar Abbas.
Introduction to Algorithms
Heaps, Heapsort, and Priority Queues
CS200: Algorithm Analysis
Ch 6: Heapsort Ming-Te Chi
"Teachers open the door, but you must enter by yourself. "
Priority Queues & Heaps
Topic 5: Heap data structure heap sort Priority queue
HEAPS.
Heapsort Sorting in place
Algorithms CSCI 235, Spring 2019 Lecture 14 Heap Sort Read: Ch. 6
Algorithms CSCI 235, Spring 2019 Lecture 15 Analysis of Heap Sort
Presentation transcript:

1 Priority Queues A priority queue is an ADT where: –Each element has an associated priority –Efficient extraction of the highest-priority element is supported. Typical operations of a priority queue are: –Enqueue (insert an element) –Dequeue (remove the highest-priority element) –Max (return the highest-priority element) –Meld (join two priority queues into one)

2 Priority Queues: Applications Scheduling –Examples: Printer scheduling. –Jobs sent to a printer by faculty have a higher priority than jobs sent by students Process scheduling. –Several processes require CPU time. Priority may be based on various factors: »Shortest jobs have higher priority »Jobs with a shorter remaining time have higher priority »Jobs that have waited the longest have higher priority »etc.

3 Priority Queues: Applications Discrete-event simulations –Simulations of systems modeled as collections of entities that interact with one another through events that occur at discrete times. The priority assigned to an event is its start time. –Example: PA1, bank simulation

4 Binary Heaps Binary heap = –a binary tree that is –complete every level except possibly the bottom one is completely filled and the leaves in the bottom level are as far left as possible –satisfies the heap property: the key stored in every node is greater than or equal to the keys stored in its children –this is called a max-heap. If the key at each node is smaller than or equal to the keys of its children, then we have a min-heap.

5 Binary Heaps Since heaps are complete trees, they can easily be stored in arrays: index given index i of a node, the index of its parent is  i / 2  the indices of its children are 2i and 2i+1

6 Binary Heaps Is every array a heap? –Not necessarily. But every sorted array is a max-heap, if sorted in descending order. min-heap, if sorted in ascending order. What is the height of a heap? –O(lgn) where n is the number of elements in the heap. Where in a max-heap is the smallest element? –It is always a leaf but we cannot know exactly where it is located. What are the maximum and minimum numbers of elements in a heap of height h? –max #elements: 2 h+1 -1 –min #elements: 2 h

7 Binary Heaps: Heapify In the discussion that follows, "heap" refers to a max-heap unless otherwise noted. Every time we extract or insert an element, the heap property may be violated. Heapify: Given node i of heap that potentially violates the heap property, fix it. Input: Array A storing a heap, index i Preconditions: The children of node i are heaps. Algorithm: while (i < heapsize) { max_index = max(A[i], A[2i], A[2i+1]) if (max_index == i) return; swap(A, i, max_index); // swap contents at indices i, max_index i = max_index; // Since we moved A[i] to A[max_index], the heap // property may now be violated there. }

8 Binary Heaps: Heapify Running time of Heapify: –O(lgn) because we may have to travel all the way from the root to a leaf, fixing the heap property. Can you come up with an array that causes Heapify to be called recursively on every node on a path from the root to a leaf? –Any array sorted in ascending order.

9 Binary Heaps: MakeHeap Given a random array, create a heap How? –Algorithm sketch: Elements A[  n/2  +1] through A[n] are leaves, so they can be considered one-element heaps. Call Heapify in a bottom-up manner starting at index  n/2 . –Running time : easy analysis: O(nlgn) more exact analysis (proof omitted): O(n)

10 Binary Heaps: MakeHeap Why does the loop run in decreasing order? If it ran in increasing order, would we still get a heap? MakeHeap: Given random array A, turn it into a heap Input: Array A Output: Array A as a heap Algorithm: heapsize(A) = length(A) for i =  length(A)/2  downto 1 Heapify(A, i) endfor

11 Binary Heaps: Insert New elements are always inserted at the end (last element of the array) This may violate the heap property: –Travel up the tree looking for an appropriate position for the new node (one that maintains the heap property). As you go, copy (shift) down the values of the nodes you pass, to make room HeapInsert (A, key) i = ++heapsize(A) while i >1 and A[parent(i)]<key A[i] = A[parent(i)] i = parent(i) A[i] = key Running time: O(lgn) since we may end up traveling from a leaf all the way to the root.

12 Binary Heaps: ExtractMax The maximum element is always at the root. Extract the maximum by replacing it with the last element of the array and then Heapifying. ExtractMax (A) max = A[1] A[1] = A[heapsize(A)] heapsize(A) = heapsize(A)-1 Heapify(A,1) return max Running time: O(lgn) since we may end up traveling from the root all the way to a leaf.

13 Binary Heaps: Sorting Idea: –Since the maximum element is at the root, we can move it to the last position in the array, and decrement the size of the heap. –The root will now contain the new maximum –Repeat... Heapsort(A) Input: Array A Output: Array A in ascending order. Algorithm: Build-heap(A) for i = length(A) downto 1 swap(A[i], A[1]) heapsize = heapsize – 1 Heapify(A, i) Running time: O(nlgn) since the loop is executed n times and Heapify takes O(lgn)

14 Priority queues in the STL #include int main() { priority_queue, greater > q; int nums[10] = {10,6,8,1,5,3,7,2,9,4}; for (int i =0; i < 10; ++i) q.push(nums[i]); std::cout << q.size() << "\n"; while (!q.empty() ) { std::cout << q.top() << "\n"; q.pop(); } return 0; } based on a vector higher values = lower priority

15 Heaps in the STL #include int main () { vector nums; for (int i=1; i<11; i++) nums.push_back(i); make_heap(nums.begin(), nums.end()) ; sort_heap(nums.begin(), nums.end()) ; nums.push_back(32); make_heap(nums.begin(), nums.end()); // remake heap pop_heap(nums.begin(), nums.end()); // extraxt max return 0; }