# Priority Queues Priority Queue Operations Priority Queue

Document Sample

```					                                             Priority Queue Operations
Enqueue items with a priority
Priority Queues
Dequeue item with the highest priority
Highest - return item with the highest
priority

2/14/05 12:05          Priority Queues   1   2/14/05 12:05      Priority Queues            2

Priority Queue Operations                    Priority Queue Implementations
In class exercise - Evaluate and choose
Uses for a priority queue                    the best ADT implementation
!   Printers
!   Office hours
!   Emergency room

2/14/05 12:05          Priority Queues   3   2/14/05 12:05      Priority Queues            4
Priority Queue Implementations
Binary Heaps/Heaps
List                  Similar to a BST
Insert at front or end                        O(1)       O(1)     n/a
(unsorted)                                                                      Heap is sorted in a much weaker sense
Search for item to deQ                        O(n)       O(n)     n/a           however the sorting is sufficient for a
Search for highest                            O(n)       O(n)     n/a           priority queue
OR                                                          Tree is always a full tree
Insert sorted                                 O(n)       O(n)    O(lg n)          !    Bottom level may not be filled but fills from
left to right
deQ highest item                              O(1)       O(1)    O(lg n)
Return highest item                           O(1)       O(1)    O(lg n)

2/14/05 12:05                         Priority Queues                   5   2/14/05 12:05               Priority Queues            6

Max Heap                                                                    Heaps
10
9                    Full binary trees are easily stored in an
10
6                    array
3                      !   Must know max size but no wasted space
9                 6
2                           " Parent = (i-1)/2
" Left Child = 2i + 1
5
" Right Child = 2i + 2

3            2         5

2/14/05 12:05                         Priority Queues                   7   2/14/05 12:05               Priority Queues            8
Heap Ordering Properties                                            Min Heap
1
Min Heap                                                                                                             7
1
!   For each node X, X.item > (X.parent).item                                                                      2
" Smallest number is at the root                                                                              8
7               2
Max Heap                                                                                                             9
!   For each node X, X.item < (X.parent).item                                                                      5
" Largest number is at the root
8            9       5

2/14/05 12:05               Priority Queues                    9    2/14/05 12:05                       Priority Queues       10

Create a hole (empty node) in the next
HEAP class                                                             available complete tree location
int MAXNODES                set to current array size
!    Remember to fill bottom layer from left to
int numNodes           current #of items in heap                   right
If the item can be inserted into the hole
itemtype* Arr          dynamically allocated space
for MAXNODES items (can              without violation of the heap property,
realloc in needed)                   insert item
Otherwise, copy the holes parent item
into the hole then trickle up
2/14/05 12:05               Priority Queues                    11   2/14/05 12:05                       Priority Queues       12
Min Heap Enqueue                                                            Min Heap Enqueue
13, 16, 19, 14, 23, 17, 6                                                        13, 16, 19, 14, 23, 17, 6

Enqueue 13
Enqueue 19
13
13
Enqueue 16                                                                                 13

13                                  16
13                                                    16            19

16

2/14/05 12:05                   Priority Queues                        13   2/14/05 12:05                      Priority Queues                  14

Min Heap Enqueue                                                            Min Heap Enqueue
13, 16, 19, 14, 23, 17, 6                                                        13, 16, 19, 14, 23, 17, 6

Enqueue 14                                                                  Enqueue 23

13                                                                                13
13                       13                                                                  13

16            19                           19                                       14            19                     14        19
14        19

16
16                           16                                                             16             23

2/14/05 12:05                   Priority Queues                        15   2/14/05 12:05                      Priority Queues                  16
Min Heap Enqueue                                                                               Min Heap Enqueue
13, 16, 19, 14, 23, 17, 6                                                                      13, 16, 19, 14, 23, 17, 6

Enqueue 17                                                                                     Enqueue 6

13                            13                               13
13                                        13

14                   19        14                               14                   17
14                   17                        14

16     23                      16       23            19        16        23        19
16      23           19                       16     23        19   17

2/14/05 12:05                       Priority Queues                                  17        2/14/05 12:05                     Priority Queues                        18

Min Heap Enqueue                                                                               Max Heap Enqueue
13, 16, 19, 14, 23, 17, 6
In class exercise - Enqueue the
Enqueue 6                                                                                 following numbers into a max heap
(remember that the largest number is
6
at the root)

14                   13                           14                 13                                                  1, 2, 3, 4, 5, 6

16        23         19        17               16         23        19        17

2/14/05 12:05                       Priority Queues                                  19        2/14/05 12:05                     Priority Queues                        20
Max Heap Enqueue                                                               Max Heap Enqueue
1, 2, 3, 4, 5, 6                                                          1, 2, 3, 4, 5, 6

Enqueue 1
Enqueue 3
1

Enqueue 2                                                             2                                           3
1
2                   1                     1                 2       1           2

1              1
2/14/05 12:05                   Priority Queues                   21           2/14/05 12:05                Priority Queues                       22

Max Heap Enqueue                                                               Max Heap Enqueue
1, 2, 3, 4, 5, 6                                                          1, 2, 3, 4, 5, 6
4           2
Enqueue 4                                                                      Enqueue 5
3            2
1       3
3                         3                                                4
4
1       4                                                                           5
1            2                            2                                3           2
2
3            2                                                              4                2
1                                                 1
1           3
1                                                                           1       3
2/14/05 12:05                   Priority Queues                   23           2/14/05 12:05                Priority Queues                       24
Max Heap Enqueue                                                            Min Heap Enqueue
void enqueueHeap ( itemtype item )
1, 2, 3, 4, 5, 6
if heap not full
4               5                                             Running time = O(lg n)
if heap empty
Enqueue 6                                                                                  arr[numNodes] = item
1       3   2                                     numNodes++
else
5                       5                                                            x = numNodes
6
while (x > 0 && arr[(x-1)/2] > item)
4             2        4                                                                               arr[x] = arr[(x-1)/2]
4               5                                     x = (x-1)/2
arr[x] = item
1         3             1         3         2                                                        numNodes++
1       3       2
2/14/05 12:05                Priority Queues                       25   2/14/05 12:05               Priority Queues                 26

Heap                                                                        Heap Dequeue
Finding the node with the highest                                      Dequeue highest priority item
priority is easy - it is just at the root                                !   Makes a hole at the root
!   Running time = O(1)                                                 !   Want to remain a complete tree so place
last item in the heap into the hole
" If item can be placed in hole without violation
of the heap property, then done
" Otherwise, trickle down. Put the highest priority
of the holes children into the hole

2/14/05 12:05                Priority Queues                       27   2/14/05 12:05               Priority Queues                 28
Min Heap Dequeue                                                                      Min Heap Dequeue

1                Dequeue 1                                                           13              17

14                  13
14                                                  13
17

16       23         19         17                                                                                                      14                  17
14             13                16       23        19

16          23        19
16        23   19

2/14/05 12:05                        Priority Queues                             29   2/14/05 12:05                   Priority Queues                              30

Min Heap Dequeue                                                                      Min Heap Dequeue
19
14
13             Dequeue 13
19
14
16                    17
19
14                 17
17
23                    14
16        23        19                               14         17
16       23
16          17
16      23

19    23
2/14/05 12:05                        Priority Queues                             31   2/14/05 12:05                   Priority Queues                              32
Min Heap Dequeue                                                      16        23        Heap Notes
In class exercise                                                          17             Common error when writing code for
Dequeue 14                                                                dequeue
14                                     19                                          !   Check number of children before finding
smaller child
16              17                           23                       16
Searching for an item requires looking
16                 17                 19                  17        at all nodes since there is no particular
19     23
ordering
19                                    23                                     !   Search is not too common

2/14/05 12:05                   Priority Queues                                  33       2/14/05 12:05                            Priority Queues                               34

Building a Heap                                                                           Building a Min Heap
Naïve method                                                                                         10    7       14    13          9         6    12       2    19

!   Perform N enqueue operations - O(n lg n)                                                             10                                            10
Better solution
!   Given an array of numbers:                                                                  7                 14                          7                 14

for (x = (N/2)-1; x >= 0; x++ )                                         13           9         6        12                2         9        6        12
Trickle down

2        19                                    13          19
2/14/05 12:05                   Priority Queues                                  35       2/14/05 12:05                            Priority Queues                               36
Building a Min Heap                                                                             Building a Min Heap
10    7        14    13          9         6        12    2        19                              10    7        14    13          9         6        12    2        19

10                                                  10                                             10                                                  10

7                   14                               7                  6                           7                  6                               2                   6

2        9         6         12                     2        9         14        12             2           9         14        12                     7        9         14        12

13       19                                    13            19                                 13       19                                       13            19
2/14/05 12:05                         Priority Queues                                     37    2/14/05 12:05                            Priority Queues                                     38

Building a Min Heap                                                                             Building a Heap
10    7        14    13          9         6        12    2        19                      Build heap algorithm actually runs in
10
O(n) time
2
!   Beyond the scope of the class to prove
2                   6
7                  6

7        9         14        12
10        9        14        12

13       19                                      13           19
2/14/05 12:05                         Priority Queues                                     39    2/14/05 12:05                            Priority Queues                                     40
Heapsort - Sort in descending
Heapsort                                                                                   order
Build heap for the opposite of what you
want                                                                                                           2                                                     6
!   Max heap for ascending order
!   Min heap for descending order                                                            7                      6                               7                     12

Take root and place in last array position,
10         9            14        12                    10       9            14        19
then think of array as 1 smaller
Trickle down from root to rebuild heap
13        19                                        13            2
Continue until all items are moved
2/14/05 12:05                           Priority Queues                               41   2/14/05 12:05                              Priority Queues                                        42

Heapsort - Sort in descending                                                              Heapsort - Sort in descending
order                                                                                      order

6                                                                                                  7
7                                                                                                          9

7                12                                                                           9                     12
9                 12                                                                                   10                    12

10           9       14        19                                                              10          13           14         19
10       13       14        19                                                                      19          13           14        7

13        2                                                                                    6            2
6         2                                                                                        6             2

2/14/05 12:05                           Priority Queues                               43   2/14/05 12:05                              Priority Queues                                        44
Heapsort - Sort in descending                                                         Heapsort - Sort in descending
order                                                                                 order

9                                         10                                          10                                             12

10                  12                      13                12                       13                 12                          13                 14

19         13       14        7             19       14        9        7              19        14        9        7                 19       10         9        7

6         2                                 6         2                                6         2                                   6          2

2/14/05 12:05                      Priority Queues                               45   2/14/05 12:05                      Priority Queues                                46

Heapsort - Sort in descending                                                         Heapsort - Sort in descending
order                                                                                 order

12                                                                                    13
13                                                                                     14

13                  14                                                                19                  14
19                14                                                                  19                 13

19         10        9        7                                                       12         10        9        7
12       10        9        7                                                         12        10        9        7

6        2                                                                           6         2
6         2                                                                            6        2

2/14/05 12:05                      Priority Queues                               47   2/14/05 12:05                      Priority Queues                                48
Heapsort - Sort in descending
order                                                                                 Heapsort Run Time
Build step - O(n)
14                                            19                      Sorting step - O(n lg n)
Heapsort - O(n) + O(n lg n) = O(n lg n)
19                   13                          14                13

12          10        9        7                 12       10        9        7

6        2                                       6        2

2/14/05 12:05                      Priority Queues                               49   2/14/05 12:05      Priority Queues        50

```
DOCUMENT INFO
Shared By:
Categories:
Stats:
 views: 76 posted: 5/28/2011 language: English pages: 13