Priority Queue - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Priority Queue

Description:

Balanced POT heap. PQ Implementation with An Array. When an array is used to implement PQ ... Decrement the size of the heap. ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 16
Provided by: PEV4
Category:
Tags: heap | priority | queue

less

Transcript and Presenter's Notes

Title: Priority Queue


1
Priority Queue
  • Irena Pevac
  • CS501

2
Priority Queue
  • Priority Queue (PQ) is set of items with assigned
    priorities, where item with highest priority is
    going to be deleted first.
  • Basic PQ operations
  • Insert item
  • DeleteMax

3
Priority Queue Implementations
  • PQ can be implemented using
  • An array
  • A sorted array
  • Balanced POT heap

4
PQ Implementation with An Array
  • When an array is used to implement PQ
  • we can add an item to the collection just by
    adding it to the rear. Cost is O(1).
  • To delete an item we have to find the one with
    highest priority. Cost is O(n).

5
PQ Implementation with Sorted Array
  • When sorted array is used to implement PQ
  • We can add an item to the collection by inserting
    it in correct position so that array components
    remain sorted based on the key. Cost is O(n).
  • We delete item with highest priority. That one is
    either first (when items are sorted in descending
    order) or last (when items are sorted in
    ascending order). Cost is O(1).

6
PQ Implementation with POT
  • Partially Ordered Tree (POT) is labeled binary
    tree with following properties
  • Each node has label whose data specifies the
    priority.
  • Each nodes priority is higher than or equal to
    the priorities of the children.

7
Balanced Partially Ordered Tree
  • Balanced Partially Ordered Tree (balanced POT)
  • POT is balanced if all possible nodes exists at
    all levels except the lowest level. The leaves at
    lowest level are as far to the left as possible.

8
Balanced Partially Ordered Tree
  • Balanced Partially Ordered Tree can be
    implemented using an array data structure called
    a heap.
  • Let us number the nodes from balanced POT
    starting from the root and visiting them level by
    level from the left to right.

9
Heap
  • Array X is created by putting items from balanced
    POT into it.
  • Root item goes into x1.
  • Left child of the root goes into x2.
  • Right child of the root goes into x3.
  • And so on.
  • Node k has children at 2k and 2k1.
  • Node r has parent at position r/2.

10
PQ Operations on a Heap
  • Insert
  • DeleteMax

11
Insert
  • Insert
  • Let n be the number of elements in PQ.
  • Assume that A1..n already satisfy POT property.
  • Increment n.
  • Call bubbleUp(A, n) which swaps item with its
    parent if item is larger than its parent and
    calls bubbleUp(A, n/2) recursively

12
DeleteMax
  • DeleteMax
  • To implement deleteMax we need another operation
    bubbleDown to bubble down an element at root
    that may violate the POT property, in that it may
    be smaller than one or both of its children A2i
    and A2i1. We swap Ai with larger child.
  • Fig 5.48 and Fig 5.49 page 277

13
DeleteMax
  • To delete largest
  • We swap the element at the root, which is to be
    deleted, with the last element in An
  • Decrement the size of the heap.
  • bubbleDown to push item from the root down until
    it reaches a point where it is larger of both
    children or becomes a leaf. That restores POT
    property.

14
Running Time of PQ Operations
  • Insert
  • deleteMax
  • Both work in O(lg n) since height of balanced POT
    is lg n

15
Heapsort
  • Sorting with Balanced POTs
  • Heapsort - informally
  • Cost O(n lg n)
Write a Comment
User Comments (0)
About PowerShow.com