Design & Analysis of Algorithm Priority Queue

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
D ESIGN & A NALYSIS OF A LGORITHM 08 – P RIORITY Q UEUE Informatics Department Parahyangan Catholic University.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
The List ADT A sequence of zero or more elements A 1, A 2, A 3, … A N-1 N: length of the list A 1 : first element A N-1 : last element A i : position i.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
Internal and External Sorting External Searching
Priority Queues and Heaps Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
UNIT-V ABSTRACT DATA TYPE 1.LIST 2.STACK 3.QUEUE EC6301-II-ECE-C.
Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.
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 and Heaps Tom Przybylinski. Maps ● We have (key,value) pairs, called entries ● We want to store and find/remove arbitrary entries (random.
Chapter 16: Linked Lists.
Sorting With Priority Queue In-place Extra O(N) space
Priority Queues A priority queue is an ADT where:
"Teachers open the door, but you must enter by yourself. "
Unit – I Lists.
C++ Programming:. Program Design Including
Queues.
Understanding Algorithms and Data Structures
Priority Queue A Priority Queue Set S is made up of n elements: x0 x1 x2 x3 … xn-1 Functions: createEmptySet() returns a newly created empty priority.
Heaps (8.3) CSE 2011 Winter May 2018.
Vectors 5/31/2018 9:25 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Top 50 Data Structures Interview Questions
CSCI-255 LinkedList.
Lectures linked lists Chapter 6 of textbook
CE 221 Data Structures and Algorithms
Priority Queues and Heaps
Data Structures Interview / VIVA Questions and Answers
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,
Priority Queues Chuan-Ming Liu
October 30th – Priority QUeues
Programming Abstractions
Hashing Exercises.
Bohyung Han CSE, POSTECH
Heap Sort Example Qamar Abbas.
O(lg n) Search Tree Tree T is a search tree made up of n elements: x0 x1 x2 x3 … xn-1 No function (except transverse) takes more than O(lg n) in the.
ADT Heap data structure
Linear and Binary Search
Selection Sort – an array sorting algorithm
Part-D1 Priority Queues
Data Structures & Algorithms Priority Queues & HeapSort
Tree Representation Heap.
Ch. 8 Priority Queues And Heaps
Hassan Khosravi / Geoffrey Tien
"Teachers open the door, but you must enter by yourself. "
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Data Structures and Analysis (COMP 410)
Tables and Priority Queues
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Chapter 4 Unordered List.
Topic 6: Binary Search Tree Data structure Operations
Topic 5: Heap data structure heap sort Priority queue
CSE 12 – Basic Data Structures
Data Structures & Algorithms
CSE 373 Priority queue implementation; Intro to heaps
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Priority Queues & Heaps
B-Trees.
Data Structures and Algorithm Analysis Priority Queues (Heaps)
Priority Queue and Heap
Heaps & Multi-way Search Trees
Applications of Arrays
CMPT 225 Lecture 16 – Heap Sort.
Presentation transcript:

Design & Analysis of Algorithm Priority Queue Informatics Department Parahyangan Catholic University

Priority Queue A priority queue is a data structure that supports two basic operations: inserting a new item and removing element with the largest (or smallest) key Example : queue in hospital prioritize patient with emergency issue

Priority Queue A few more operations: Finding element with the largest key i.e., which patient has the most severe issue ? Removing and arbitrary element i.e., maybe someone decides to leave the hospital Changing the priority of an arbitrary element i.e., someone suddenly needs an immediate help ! Joining two priority queues i.e., received several transfer patients from other hospital

Priority Queue Operations More specifically, a priority queue maintains a set S of elements. Each element has a key. A max priority queue supports the following basic operations : INSERT(x) inserts element x to S MAXIMUM() returns the element in S that has the largest key EXTRACT-MAX() removes and returns the element in S that has the largest key INCREASE-KEY(x, k) increase the key of element x to k (assume the current key of x is at least as large as k) Removing an element is the same as increasing its key to ∞, then do extract-max

Priority Queue Operations Alternatively, a min priority queue supports the following operations: INSERT(x) inserts element x to S MINIMUM() returns the element in S that has the largest key EXTRACT-MIN() removes and returns the element in S that has the smallest key DECREASE-KEY(x,k) decrease the key of element x to k (assume the current key of x is at least as small as k) Removing an element is the same as decreasing its key to -∞, then do extract-min

Various Implementation Priority Queue is an ADT, means that we can implement it with various technique: Array Ordered Array Unordered Array Linked List Ordered Linked List Unordered Linked List Heap (covered later) Binomial Heap Fibonacci Heap etc. Now we discuss how to implement a max priority queue that only stores keys. Implementation of min priority queue is very similar, and left as an exercise  Later we shall see how to “link” a priority queue’s element to an outside object.

Implementation #1 Ordered Array Since an array has a fixed capacity, priority queue implemented using an array is also limited. length = array’s length (=capacity) size = the number of elements in priority queue Example: priority queue of length 10 with size=7 MAXIMUM and EXTRACT-MAX are O(1) 2 4 5 7 9 12 13 size = position of max element

Implementation #1 Ordered Array INSERT Similar to inserting an element on insertion sort INCREASE-KEY is similar to INSERT, only to a different swap direction 2 4 5 7 9 12 13 INSERT(10) 2 4 5 7 9 12 13 10 10<13  swap 2 4 5 7 9 12 10 13 10<12  swap 2 4 5 7 9 10 12 13 10≮9  stop Time complexity is O(n)

Implementation #2 Unordered Array Example: priority queue of length 10 with size=7 INSERT is simply adding the new element on an empty spot  O(1) INCREASE-KEY is simply changing the key’s value  O(1) EXTRACT-MAX and MAXIMUM requires sequential through the entire array  O(n) 5 4 2 1 11 6 7 size ≠ position of max element !!

Exercise Write the pseudocode for the following operations on an unordered array priority queue ! Global variables are: A = array of integers length = maximum capacity of A size = # of elements in priority queue, initially 0 INSERT(x) MAXIMUM() EXTRACT-MAX() INCREASE-KEY(i, k) increase the key of the element stored at index i to the new value k. Assume the old key is ≤ k

Implementation #3 Ordered Linked List Node of linked list can be ordered in ascending manner, and we keep track of “tail” pointer as the maximum element of the priority queue. Alternatively, order the linked list in descending manner, thus the “head” is the maximum element of the priority queue. 3 head tail = max 6 8 8 head = max 6 3

Implementation #3 Ordered Linked List Since a linked list can have unlimited* nodes, keeping track of length variable is not necessary. *of course they are limited by computer’s memory space  INSERT is similar to inserting on an ordered array 5 < 8 5 < 6 5 ≮ 3 5 8 6 3 What is the time complexity ? head = max

Implementation #3 Ordered Linked List How do we INCREASE-KEY ? Need a doubly linked-list to be able to move an element backward ! 8 6 5 3 head = max increase this element’s key to 7 ! 8 6 5 3 head = max

Exercise Write the pseudocode of INCREASE-KEY(Node x, int newkey) Assume that a Node has the following attribute: key next prev

Implementation #4 Unordered Linked List In an unordered linked-list, head element is not necessarily the maximum of the priority queue. New element can be inserted anywhere in the list, thus we can use the O(1) INSERT-FIRST method. However, finding the maximum element requires sequential search through the entire list  O(n) 4 7 5 2 head ≠ max

Comparison Array Linked-List Heap unordered ordered INSERT O(1) O(n) O(lgn) MAXIMUM EXTRACT-MAX INCREASE-KEY