Priority Queues and Binary Heaps: Implementation and Operations, Study notes of Computer Science

Class notes on priority queues and binary heaps from cop 3503 – computer science ii. Topics covered include the structure and ordering properties of binary heaps, basic operations like insertion and deletion, and sorting from a binary heap. The document also discusses an alternative technique for handling insertion and its associated problems.

Typology: Study notes

Pre 2010

Uploaded on 11/08/2009

koofers-user-6t5
koofers-user-6t5 🇺🇸

10 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COP 3503 – Computer Science II – CLASS NOTES - DAY #25
Priority Queues
A priority queue is a structure where the highest priority item is the next item
that will be dequeued.
Implementation typically sets the highest priority item to have the lowest
integer value as its priority.
Thus, the node with the lowest integer value should always be at the head of the
queue.
If implemented as a binary heap, insertion and deletion can be done in
logarithmic time in the worst case.
Binary Heaps
Can be implemented as an array (just like the queue)
Combines the best properties of binary search trees and queues.
Insertion: average case – O(1), worst case – O(log2 N).
DeleteMin: worst case – O(log2 N).
FindMin: worst case – O(1).
Like a binary search tree, the binary heap has two properties: a structure property
and an ordering property. Any operation has the potential for destroying both the
structure and ordering properties of the binary heap (hereafter called “the heap”).
Therefore, operations on the heap should not be allowed to terminate until both
properties are preserved.
Structure Property
A tree is the only structure that gives logarithmic time bounds, therefore it
makes sense to organize the heap as a tree.
To ensure a logarithmic worst case time bound, we will need to ensure that the
tree is balanced.
Note: a complete tree requires that every internal node has the maximum
number of children. For a binary tree this means that each internal node
must have two children.
A complete binary tree has several useful properties that make it well suited to
the application of heaps.
Day 25 - 1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Priority Queues and Binary Heaps: Implementation and Operations and more Study notes Computer Science in PDF only on Docsity!

COP 3503 – Computer Science II – CLASS NOTES - DAY #

Priority Queues

 A priority queue is a structure where the highest priority item is the next item

that will be dequeued.

 Implementation typically sets the highest priority item to have the lowest

integer value as its priority.

 Thus, the node with the lowest integer value should always be at the head of the

queue.

 If implemented as a binary heap, insertion and deletion can be done in

logarithmic time in the worst case.

Binary Heaps

 Can be implemented as an array (just like the queue)

 Combines the best properties of binary search trees and queues.

 Insertion: average case – O(1), worst case – O(log 2 N).

 DeleteMin: worst case – O(log 2

N).

 FindMin: worst case – O(1).

Like a binary search tree, the binary heap has two properties: a structure property

and an ordering property. Any operation has the potential for destroying both the

structure and ordering properties of the binary heap (hereafter called “the heap”).

Therefore, operations on the heap should not be allowed to terminate until both

properties are preserved.

Structure Property

 A tree is the only structure that gives logarithmic time bounds, therefore it

makes sense to organize the heap as a tree.

 To ensure a logarithmic worst case time bound, we will need to ensure that the

tree is balanced.

Note: a complete tree requires that every internal node has the maximum

number of children. For a binary tree this means that each internal node

must have two children.

 A complete binary tree has several useful properties that make it well suited to

the application of heaps.

  1. The height (length of longest path) is at most log 2 N (where the tree

contains N nodes). A complete tree of height H has between 2

H and 2

H+  1

nodes.

  1. In a complete binary tree, left and right references are not needed. Instead

the level-order traversal of the tree can be stored in single-dimension array.

The root node is stored in array position 1 (position 0 is utilized later by the

priority queue). For any element in array position i , its left child will be

found in position 2i and its right child will be found in position 2i+1. To

determine it a node at index i has children test to see if 2i > number of

elements. To determine the parent of a node at index i , check at index  i/2 .

Using an array to represent a tree is called an implicit tree representation. This

method is very fast on most systems and the traversal operation become trivial

and extremely fast.

Example:

A B C D E F G H I J

Left child of node “A” ( index 1) is located at index (2*1) = 2 or node “B”

Right child of node “A” (index 1) is located at index (2*1) +1 = 3 or node “C”

Parent of node “F” (index 6) is located at 6/2 = 3 or node “C”

Parent of node “I” (index 9) is located at 9/2 = 4 or node “D”

Child of node “H” (index 8) is located at (2*8) = 16 out of range so no child

exists.

A

B C

D E F G

H I J

Final tree maintaining structure and ordering properties.

DeleteMin:

  1. Get the key value from the root node.
  2. Locate the bottom, rightmost child and interchange it with the root.
  3. Compare the key values of the two children of the root and the compare

the smaller of them with the key value of the root. Interchange if the root

key value is greater than that of the smaller child key value.

  1. Percolate down by treating each current node as a “root” and recursively

applying step #3.

Example: return node “1”

1 0 7 4 1 `

Example: return 1

Sorting From A Binary Heap

 Recall that the binary heap has both an ordering property and a structure

property. The ordering property ensures that the items in the heap are basically

sorted from minimum (root) value to maximum values (leafs).

 Removing (or reading) everything results in a sort.

 If this can be done in O(N log 2 N) time, then we have found a “non-

comparison” based sorting algorithm.

The insertion technique that we used for inserting items into the binary heap,

inserted N items and required O(log 2 N) time to find the right spot at which to do

the insertion. Thus the time required to insert N items is: O(N log 2

N).

Example: Illustration of linear time fixHeap( ) using percolateDown( )

initial tree: has structure property, violates ordering property

  1. call percolateDown(7)

no changes

  1. call percolateDown(6)

swap 25, 45

  1. call percolateDown(5)

no changes

  1. call percolateDown(4)

swap 17, 20

  1. call percolateDown(3)

no changes

This technique is not without its problems, which include:

 Requires 2N space as 2 arrays are needed, one for the unordered heap and one

created in heap order by fixHeap( ).

 Fix: This problem can be solved by using a “sliding heap” and altering the

method fixHeap( ) to return the largest item. As maximal items are returned

we remove them from the heap and decrease the size of the heap by one.

The removed item is then placed into the cell of the array which has been

“freed” from the heap.

Example:

  1. call fixHeap( )
  1. call fixHeap( )
  1. call fixHeap( )
  1. call fixHeap( )

heap

end of

heap

heap

end of

heap heap

end of

heap

heap

end of

heap

heap

end of

heap

  1. call fixHeap( )
  1. call fixHeap( )
  1. call fixHeap( )
  1. call fixHeap( )
  1. call fixHeap( )

When the index counter (the sliding part of the heap) gets to 0, the resulting

array is in heap order.

heap

end of

heap

heap

end of

heap

heap

end of

heap

heap

end of

heap

heap

all items in

the heap