How to update elements within a heap? priority queue
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Updating priorities inside a heap based priority queue is a common requirement in schedulers, shortest path algorithms, and event systems. The challenge is that heaps provide fast access to top priority but do not support fast random lookup by value. Efficient updates require both heap reordering logic and a way to locate elements quickly.
Understand the Two Update Directions
When an element priority changes, heap repair direction depends on the new value.
For a min heap:
- Lower key means move up with sift up.
- Higher key means move down with sift down.
For a max heap, directions are reversed.
If you always run both operations blindly, updates remain correct but waste time. Choosing direction explicitly keeps each update near O(log n).
Keep an Index Map for Fast Element Lookup
A plain heap array cannot locate arbitrary elements quickly. Add a map from item ID to heap index so updates avoid linear search.
Python example using min heap semantics:
This design supports insert, pop, and update without scanning the full heap.
Alternative Strategy: Lazy Updates
Some standard library priority queues do not support in place updates. A practical workaround is lazy updates:
- Push new pair with updated priority.
- Mark old entry stale in a map.
- Skip stale entries when popping.
This approach is simple and common in Dijkstra implementations where decrease key is needed but native heap update is unavailable.
Lazy updates trade some memory and stale entries for implementation simplicity.
Choose the Right Approach
Use indexed heap when:
- Frequent in place updates are required.
- Memory overhead of map is acceptable.
- Deterministic update cost matters.
Use lazy updates when:
- Update frequency is moderate.
- Simpler code is preferred.
- Occasional stale entries are acceptable.
Benchmark both approaches on realistic workload shape before locking in one design.
Common Pitfalls
- Searching heap linearly for element updates, which turns operations into
O(n). - Forgetting to update index map during swaps, causing corrupted state.
- Using wrong repair direction after priority change.
- Assuming language built in priority queue supports direct key update.
- Ignoring stale entry accumulation in lazy update patterns.
Summary
- Heap updates require both element lookup and heap repair.
- Maintain an index map for efficient in place key changes.
- Use sift up for higher priority in min heap, sift down for lower priority.
- Lazy update is a practical fallback when direct update is unavailable.
- Validate with benchmarks to match design to workload behavior.
Related reading
- How to use a custom SVM kernel?
- How to use a Java8 lambda to sort a stream in reverse order?
- How to use Comparator in Java to sort
- How to use Disjoint Sets in Connected Component labeling?
- How to use a dot . to access members of dictionary?
- How to use Array.prototype.some with an async function?
- How to use lower_boundupper_bound to find position of any number in array?
- How to use recursion in creating a binary search algorithm

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.