Next: Chapter 7: Sorting
Up: CS 3345: Algorithm Analysis
Previous: Chapter 5: Hashing
Priority Queues (Heaps)
Minimum set of required functions:
- Insert
- : equivalent to Enqueue
- Delete_Min
- : returns and removes minimum element
Use binary search trees?
- O(log n) time for both operations
- repeated deletions
tree unbalanced
- overkill: several search tree operations not required
Implementation: Binary Heap
Structure Property
- complete binary tree
- can be represented in an array
- pointers not needed
-
-
-
- position 0 not used
Implementation (contd.)
Heap Order Property
-
value(node) < value of its children
- root = minimum element
- Find_Min = constant time operation
Basic Heap Operations
- Insert(x)
- : percolate up
- create a slot in next available location
- can x be placed in the slot?
- Yes: done
- No: slide slot's parent down to slot, repeat.
- termination: place sentinel in location 0
- Time = O(log n) comparisons
- Average Time = 2.607 comparisons
Operations (contd.)
- Delete_Min
- : percolate down
- remove element from root (create empty slot)
- let last element of heap be X
- move smaller of slot's children up
- repeat until slot reaches a node where X can be inserted
- place X in slot
- Time = O(log n) comparisons
Other Heap Operations
- Decrease_Key(P, d)
- : decrease value of pth element by d
- fix heap using percolate up
- Increase_Key(P, d)
- : fix heap using
percolate down
- Remove(I)
- : remove node at position I
- Decrease_Key(I, )
- Delete_Min
- Build_Heap
- : build a heap containing n
elements
Build_Heap
Simple Implementation
-
n inserts
- worst case =
O(n log n)
- average case = O(n)
Better Implementation
- place n keys in unordered complete tree
- for
(i=n/2; i>0; i-) percolate_down(i)
Running Time: (# of comparisons) + (# of assignments)
# of comparisons = O(heights of nodes)
= O(n)
Applications: Selection Problem
Find the kth largest/smallest element
Solution 1
- read n elements into array
- Build_Heap for n elements
- perform k Delete_Min operations
- last element returned = kth smallest
- change heap order property to get kth largest
- Time complexity =
O(n log n)
Applications: Selection Problem
Solution 2
- Build_Heap using first k elements
- compare subsequent elements with
minimum of heap (top)
- replace heap minimum if new element is larger
- heap minimum at the end = kth largest
- Time complexity =
O(k + (n-k) log k) =
O(n log k)
Leftist Heaps
- binary tree
- has heap order property
- not perfectly balanced
Null path length(X): length of shortest path from node X to a node
without two children
Npl(NULL) = -1
Npl(X) = min(Npl(right child), Npl(left child)) + 1
Leftist heap property: for every node X,
Npl(left child) Npl(right child)
Leftist heap operations
Merge: recursively merge heap with larger root with the
right subheap of heap with smaller root.
- base case: one tree is empty
- make new tree right child of smaller root
- if result not leftist: swap root's left and right subtrees
Time = O(sum of lengths of right paths)
= O(log n)
Leftist heap operations (contd.)
Insert: treat item as a one node heap, and merge
Delete_Min: remove root, merge two subtrees
Skew Heaps
- binary trees with heap order
- no structural constraint
- right path can be arbitrarily long
- no extra space needed to store path lengths
- always swap subtrees during merge
- worst case running time of all operations = O(n)
For any m consecutive operations, time =
O(m log n)
Next: Chapter 7: Sorting
Up: CS 3345: Algorithm Analysis
Previous: Chapter 5: Hashing
Ravi Prakash
1999-11-17