Presentation is loading. Please wait.

Presentation is loading. Please wait.

Priority Queues Chuan-Ming Liu

Similar presentations


Presentation on theme: "Priority Queues Chuan-Ming Liu"— Presentation transcript:

1 Priority Queues Chuan-Ming Liu
Computer Science & Information Engineering National Taipei University of Technology Taiwan

2 Contents Priority Queue ADT Implementing a Priority Queue with a List
Heaps

3 Example Consider that you are now a vendor who sells the service of a machine and each client pays a fixed amount per use. Your goal is to have the maximum profit. Solution: when the machine available, select the client with minimum time requirement

4 Knapsack Problem Given n objects
Each object has a value (profit), pi and a weight wi. A knapsack can only carry weight m Maximize the value of the stuff in the knapsack. Here we assume that the objects can be split into pieces and carry a portion xi of object i. (i.e., 0≦ xi ≦1) max subject to and

5 Knapsack Example 3 objects: 1 knapsack: P:25 W:18 P:24 W:15 P:15 W:10
Capacity: 20 1 knapsack:

6 Using Largest Profit Solution = 1 * 25 + 2/15 * 24 = 28.2 P:25; W:18
Capacity: 20 P:25; W:18 P:24; W:15 Solution = 1 * /15 * 24 = 28.2

7 Using Least Weight Solution = 1 * 15 + 10/15 * 24 = 31
P:25; W:18 P:24; W:15 P:15; W:10 Capacity: 20 P:15; W:10 P:24; W:15 Solution = 1 * /15 * 24 = 31 Better Select() …?

8 Using Largest p/w p/w = 1.39 p/w = 1.6 p/w = 1.5
Capacity: 20 P:24; W:15 P:15; W:10 Solution = 1 * /10 * 15 = 31.5

9 Solutions to Knapsack Problem
The framework for solving knapsack problem repeat Select the one which meets the criterion until the capacity of the knapsack is reached Three kinds of criteria: Largest profit Least weight Largest p/w ratio How to accomplish the repeated statement?

10 Introduction A priority queue is a data structure that manages objects with associated priority In particular, a priority queue will support insertion, deletion, and delete_highest_priority We can use a priority queue to help us accomplish the repeated statement in the knapsack problem. In general, we refer to the priority associated with the object as the key of that object.

11 Some Applications In a time-sharing system, a large number of tasks may be waiting for the CPU and some of the tasks have higher priority than the other. The set of tasks waiting for the CPU forms a priority queue The emergency room treats patients according to the urgency of their malady Selling stocks according to the capital gain

12 Keys A key is a “value” which can be used to identify, rank, or weight the managed objects. We focus on the key which has priority over another. In order to compare a pair of keys, it requires a total order relation among the keys

13 Total Order Relations total order relation () Such a relation
Reflexive property: x  x Antisymmetric property: x  y  y  x  x = y Transitive property: x  y  y  z  x  z Such a relation defines a linear ordering relation among the keys provides a comparison rule For a finite collection of elements having a total order relation, a smallest (largest) key kmin (kmax)is well defined.

14 Entries An entry in a priority queue is simply a key-element pair
Priority queues store entries to allow for efficient insertion and removal based on keys Methods: key(): returns the key for this entry element(): returns the element associated with this entry

15 Priority Queue ADT A priority queue is a container of objects (elements), each having an associated key that is provided at the time the object is insert Having the composition and comparator patterns, a priority queue P supports insert(k, x): inserts an entry with key k and object x removeMin() (deleteMin or extractMin): removes and returns the entry with smallest key min(): returns, but do not remove, an entry with smallest key size(), isEmpty()

16 Priority Queues Ways to represent a priority queue Unsorted lists
insert:(1) removeMin and min: (n) Sorted lists insert:(n) removeMin and min : (1) Heaps insert, removeMin and min : (log n)

17 Sorting with a Priority Queue
We can use a priority queue to sort a set of comparable elements Insert the elements one by one with a series of insert operations Remove the elements in sorted order with a series of removeMin operations The running time of this sorting method ? depends on the priority queue implementation

18 Contents Priority Queue ADT Implementing a Priority Queue with a List
Heaps We use list to mean a collection of records stored in a certain order and each record having one or more fields.

19 Using an Unsorted List Using an unsorted list S to implement a priority queue P, where S is a doubly linked list Constant time insertion: simply add the new entry at the end of S Linear time search and removal: The operation min or removeMin on P needs to scan through all the elements in S

20 Using a Sorted List Representing the priority queue P by using a list S of entries sorted Constant time search and removal: the operation min or removeMin on P can be done by simply accessing the first element of S Linear time insertion: the insertion needs to scan through the list to find a place to insert the entry

21 Selection-sort Recall the sorting scheme using a priority queue
Such a sorting using a priority queue with an unsorted list is better known as selection-sort The running time of selection-sort: Inserting the elements into the priority queue with n insert operations takes O(n) time Removing the elements in sorted order from the priority queue with n removeMin operations takes time proportional to ( …+ n) total running time is O(n2).

22 Selection-Sort Example
Sequence S Priority Queue P Input: (7,4,8,2,5,3,9) () Phase 1 (a) (4,8,2,5,3,9) (7) (b) (8,2,5,3,9) (7,4) . . . (g) () (7,4,8,2,5,3,9) Phase 2 (a) (2) (7,4,8,5,3,9) (b) (2,3) (7,4,8,5,9) (c) (2,3,4) (7,8,5,9) (d) (2,3,4,5) (7,8,9) (e) (2,3,4,5,7) (8,9) (f) (2,3,4,5,7,8) (9) (g) (2,3,4,5,7,8,9) () Insertion removeMin

23 Insertion-sort Insertion-sort is a sorting scheme using a priority queue with a sorted list The running time of insertion-sort: Inserting the elements into the priority queue with n insert operations takes time proportional to ( …+ n) Removing the elements in sorted order from the priority queue with n removeMin operations takes O(n) time total running time is O(n2)

24 Insertion-Sort Example
Sequence S Priority queue P Input: (7,4,8,2,5,3,9) () Phase 1 (a) (4,8,2,5,3,9) (7) (b) (8,2,5,3,9) (4,7) (c) (2,5,3,9) (4,7,8) (d) (5,3,9) (2,4,7,8) (e) (3,9) (2,4,5,7,8) (f) (9) (2,3,4,5,7,8) (g) () (2,3,4,5,7,8,9) Phase 2 (a) (2) (3,4,5,7,8,9) (b) (2,3) (4,5,7,8,9) (g) (2,3,4,5,7,8,9) () Insertion removeMin

25 Contents Priority Queue ADT Implementing a Priority Queue with a List
Heaps

26 Heaps A min (max) heap is a complete binary tree with the property that the value at each node is at least as small as (as large as) the values at its children (if they exist). The above property is called as heap property Smallest (largest) element is at the root Min (max) heap is a complete binary tree and one can use an array to represent it

27 Example – Heaps 10 30 15 35 40 44 25 50 38 57 61

28 Complete Binary Trees A complete binary tree with height h is a binary tree where for i = 0, … , h - 2, there are 2i nodes of depth i at depth h - 1, the internal nodes are to the left of the external nodes the last node of a complete binary tree is the right-most node of depth h A heap T storing n entries has height

29 A Complete Binary Tree h = 3 last node Level 0 Level 1 Level 2 Level 3
10 30 15 35 40 44 25 50 38 57 61 h = 3 last node

30 Complete Binary Tree ADT
A complete binary tree T supports all the methods of binary tree ADT, plus add(o): add a new node v which contains the element o to T such that the resulting tree is a complete binary tree with last node v remove(): remove the last node and return its element Operation add may increase the height of T by 1

31 Array List Representation
A complete binary tree T can be implemented by a vector V where each node v is kept with rank equal to the level number p(v) If v is the root, then p(v)=1 If v is the left child of node u, then p(v)=2p(u) If v is the right child of node u, then p(v)=2p(u)+1 Methods add and remove can be done in O(1) time.

32 Priority Queue with a Heap
Heap-based representation T for a priority queue P consists of the following: Heap, T Comparator Should support the following operations min (max) insert removeMin(removeMax)

33  Insertion insert (v) // for Min_heap increase the size of the heap
add v at the end of the vector while (v.key() < v.parent.key()) do exchange v and v.parent O(h), h is the height of the heap and generally is log n up-heap bubbling

34 Example – Insertion (Min-Heap)
10 15 30 35 40 44 25 50 38 57 61 8

35 RemoveMin removeMin ( ) Downheap (p, T) O(log n) operations
remove the item at the root r and output it replace the last element in the vector with r decrease the size of the heap downheap (r, T) Downheap (p, T) //rearrage the tree into a heap a in top-down fashion push the element at position p down to the right position in the heap O(log n) operations

36 Example – removeMin removeMin Downheap output Minimum 8 10 30 35 40 15
25 50 38 57 61 44

37 Heap Sort Recall the sorting scheme using a priority queue again
The heap-sort algorithm sorts a sequence S of n elements in O(n log n) time

38 Constructing Heaps To form a heap for n elements
Insert each element one by one Each insertion: O(log n) Total time: O(n log n) Build a heap by an input vector Work on the vector (a complete binary tree) Total time: O(n) Called heapify for a vector Bottom-up fashion

39 Heap Construction by Insertion
Compute the time tightly, we count it level by level in worst case for inserting one by one log n i i-1 Note: A heap T storing n entries has height log n

40 Computing the Bound

41 Heapfy – An Example 1 35 2 3 20 40 4 5 6 7 30 25 15 28

42 Heapify – Analysis Compute the time tightly, we count it level by level The worst case number of iterations for a node at level i is h-i, where h is the height of the heap i log n log n-i ?

43 The Bound Changing variables i=log n - j

44 More Priority Queues There are more priority queues discussed in Chapter 9 of the textbook We so far look at some basic priority queues like, sorted lists, unsorted lists, and heap


Download ppt "Priority Queues Chuan-Ming Liu"

Similar presentations


Ads by Google