Download presentation
Presentation is loading. Please wait.
Published byDavid Hoover Modified over 9 years ago
1
D ESIGN & A NALYSIS OF A LGORITHM 08 – P RIORITY Q UEUE Informatics Department Parahyangan Catholic University
2
P RIORITY Q UEUE 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
3
P RIORITY Q UEUE 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
4
P RIORITY Q UEUE O PERATIONS 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
5
P RIORITY Q UEUE O PERATIONS 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
6
V ARIOUS I MPLEMENTATION 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. 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.
7
I MPLEMENTATION #1 O RDERED A RRAY 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) 245791213 size = position of max element
8
I MPLEMENTATION #1 O RDERED A RRAY INSERT Similar to inserting an element on insertion sort INCREASE-KEY is similar to INSERT, only to a different swap direction 245791213 24579121310 24579121013 24579101213 INSERT(10) 10<13 swap 10<12 swap 10 ≮ 9 stop Time complexity is O(n)
9
I MPLEMENTATION #2 U NORDERED A RRAY 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) 54211167 size ≠ position of max element !!
10
E XERCISE 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
11
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. I MPLEMENTATION #3 O RDERED L INKED L IST 3 headtail = max 688 head = max 63
12
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 I MPLEMENTATION #3 O RDERED L INKED L IST 8 head = max 63 5 5 < 8 5 < 6 5 ≮ 3 What is the time complexity ?
13
How do we INCREASE-KEY ? Need a doubly linked-list to be able to move an element backward ! I MPLEMENTATION #3 O RDERED L INKED L IST 8 head = max 653 increase this element’s key to 7 ! 8 head = max 653
14
E XERCISE Write the pseudocode of INCREASE-KEY(Node x, int newkey) Assume that a Node has the following attribute: key next prev
15
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) I MPLEMENTATION #4 U NORDERED L INKED L IST 4 head ≠ max 752
16
C OMPARISON ArrayLinked-ListHeap unorderedorderedunorderedordered INSERTO(1)O(n)O(1)O(n)O(lgn) MAXIMUMO(n)O(1)O(n)O(1) EXTRACT-MAXO(n)O(1)O(n)O(1)O(lgn) INCREASE-KEYO(1)O(n)O(1)O(n)O(lgn)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.