Priority Queues - PowerPoint PPT Presentation

1 / 48
About This Presentation
Title:

Priority Queues

Description:

F2.2AO2 Data Structures and Algorithms I. The Priority Queue ... the user who needs least time gets the machine; the average delay is minimised ... – PowerPoint PPT presentation

Number of Views:112
Avg rating:3.0/5.0
Slides: 49
Provided by: stuart154
Category:

less

Transcript and Presenter's Notes

Title: Priority Queues


1
Priority Queues Pauline Wilcox and Brian
Palmer Department of Computer Science
2
Aims
  • Summary of rest of term 2
  • Introduce priority queue structure
  • The priority queue as an ADT
  • Example applications
  • Sorting
  • Implementation Issues

3
The Priority Queue
P5
P2
P1
P9
P25
  • We call it a priority queue - but its not FIFO
  • Items in queue have PRIORITY
  • Elements are removed from priority queue in
    either increasing or decreasing priority
  • Min Priority Queue
  • Max Priority Queue

4
The Priority Queue
P5
P2
P1
P9
P25
  • Consider situation where we have a computer whose
    services we are selling
  • Users need different amounts of time
  • Maximise earnings by min priority queue of users
  • i.e. when machine becomes free, the user who
    needs least time gets the machine the average
    delay is minimised

5
The Priority Queue
P5
P2
P1
P9
P25
  • Consider situation where users are willing to pay
    more to secure access - they are in effect
    bidding against each other
  • Maximise earnings by max priority queue of users
  • i.e. when machine becomes free, the user who is
    willing to pay most gets the machine

6
The Priority Queue
  • Common data structure in computer science
  • Responsible for scheduling jobs
  • Unix (linux) can allocate processes a priority
  • Time allocated to process is based on priority of
    job
  • Priority of jobs in print scheduler

7
Max Priority Queue ADT
  • AbstractDataType MaxPriorityQueue
  • instances
  • finite collection of zero or more elements each
    has a priority
  • operations
  • isEmpty() Return true if the queue is empty
    else false
  • size() Return number of elements in the queue
  • getMax() Return the element with the maximum
    priority
  • put(x) Add element x into the priority queue
  • removeMax() Remove the element with the
    maximum priority from the priority and return it

8
Max Priority Queue Interface
  • public interface MaxPriorityQueue
  • public boolean isEmpty()
  • public int size()
  • public Comparable getMax()
  • public void put(Comparable theObject)
  • public Comparable remove()

It may be desirable to include a method to allow
priority of elements to be changed (increased or
decreased) If we dont do this some items may
never be accessed!
9
Application
  • Sorting
  • Use element key as priority
  • Put elements to be sorted into a priority queue
  • Extract elements in priority order
  • if a min priority queue is used, elements are
    extracted in ascending order of priority (or key)
  • if a max priority queue is used, elements are
    extracted in descending order of priority (or
    key)

10
Sorting Example
  • Sort five elements whose keys are 6, 8, 2, 4, 1
    using a max priority queue
  • Step 1
  • Put the five elements into a max priority queue
  • Step 2
  • Do five remove max operations placing removed
    elements into the sorted array from right to left

11
After Putting Into Max Priority Queue
  • Max Priority Queue
  • Sorted Array

6
8
4
1
2
12
After First Remove Max Operation
  • Max Priority Queue
  • Sorted Array

4
6
1
2
13
After Second Remove Max Operation
  • Max Priority Queue
  • Sorted Array

4
1
2
6
14
After Third Remove Max Operation
  • Max Priority Queue
  • Sorted Array

1
2
6
4
15
After Fourth Remove Max Operation
  • Max Priority Queue
  • Sorted Array

1
6
4
2
16
After Fifth Remove Max Operation
  • Max Priority Queue
  • Sorted Array

6
4
2
1
17
Complexity Of Sorting
  • Sort n elements
  • n put operations gt O(n log n) time
  • n remove max operations gt O(n log n) time
  • total time is O(n log n)
  • Compare
  • with O(n2) for sort methods like the bubblesort
  • Heap Sort
  • Uses a max priority queue that is implemented as
    a heap
  • Initial put operations are replaced by a heap
    initialization step that takes O(n) time

18
Implementation Options
  • Priority queue can be regarded as a linear list
  • Unordered linear list
  • Time complexities
  • Ordered linear list
  • Time complexities

put operation is O(1) removeMax is O(n)
put operation is O(n) removeMax is O(1)
19
Implementation Options
  • Priority queue can be regarded as a heap
  • isEmpty, size, and get gt O(1) time
  • put and remove gt O(log n) time
  • where n is the size of the priority queue
  • Were going to look at the heap
  • A complete binary tree with values at its nodes
    arranged in a particular way (the priority!)

i.e. this is better than linear list option on
average
20
Min Tree Definition
  • Each tree node has a value
  • Value in any node is the minimum value in the
    subtree for which that node is the root
  • Equivalently, no descendant has a smaller value

2
4
9
3
4
8
7
9
9
21
Max Tree Example
  • Root has maximum value

9
4
9
8
4
2
7
3
1
22
Heap Definitions
  • Complete binary tree
  • Min tree
  • Complete binary tree
  • Max tree

Min Heap
Max Heap
23
Min Heap With 9 Nodes
  • Complete binary tree with 9 nodes

24
Min Heap With 9 Nodes
  • Complete binary tree with 9 nodes that is also a
    min tree

25
Max Heap With 9 Nodes
  • Complete binary tree with 9 nodes that is also a
    max tree.

Since a heap is a complete binary tree, the
height of an n node heap is log2 (n1)
26
A Heap Is Efficiently Represented As An Array
27
Putting An Element Into A Max Heap
  • Complete binary tree with 10 nodes

28
Putting An Element Into A Max Heap
  • New element is 5

29
Putting An Element Into A Max Heap
  • New element is 20

9
8
7
6
2
6
7
7
5
1
7
30
Putting An Element Into A Max Heap
  • New element is 20

31
Putting An Element Into A Max Heap
  • New element is 20

9
7
8
6
2
6
7
5
1
7
7
32
Putting An Element Into A Max Heap
  • New element is 20

20
9
7
8
6
2
6
7
5
1
7
7
33
Putting An Element Into A Max Heap
  • Complete binary tree with 11 nodes

20
9
7
8
6
2
6

7
5
1
7
7
34
Putting An Element Into A Max Heap
  • New element is 15

20
9
7
8
6
2
6

7
5
1
7
7
35
Putting An Element Into A Max Heap
  • New element is 15

20
9
7
6
2
6
8
8
7
5
1
7
7
36
Putting An Element Into A Max Heap
  • New element is 15

20
7
15
6
2
6
9
8
8
7
5
1
7
7
Complexity is O(log n), where n is heap size
37
Removing The Max Element
  • Max element is in the root

38
Removing The Max Element
  • After max element is removed - root is left empty

7
15
6
2
6
9
8
8
7
5
1
7
7
39
Removing The Max Element
  • Heap will have 10 nodes, reinsert last leaf node
    (8) into the heap

7
15
6
2
6
9
8
8
7
5
1
7
7
40
Removing The Max Element
  • Reinsert 8 into the heap

7
15
6
2
6
9
7
5
1
7
7
41
Removing The Max Element
  • Reinsert 8 into the heap

15
7
6
2
6
9
7
5
1
7
7
42
Removing The Max Element
  • Reinsert 8 into the heap

15
7
9
6
2
6
8
7
5
1
7
7
43
Removing The Max Element
  • Max element is 15

15
7
9
6
2
6
8
7
5
1
7
7
44
Removing The Max Element
  • After max element is removed

7
9
6
2
6
8
7
5
1
7
7
45
Removing The Max Element
  • Heap with 9 nodes - reinsert last leaf node (7)
    into the heap

7
9
6
2
6
8
7
5
1
7
7
46
Removing The Max Element
  • Reinsert 7

7
9
6
2
6
8
5
1
47
Removing The Max Element
  • Reinsert 7

9
7
6
2
6
8
5
1
48
Removing The Max Element
  • Reinsert 7

9
8
7
6
2
6
7
5
1
Complexity is again O(log n)
Write a Comment
User Comments (0)
About PowerShow.com