Least Frequently Used (LFU) is a caching algorithm in which the least frequently used cache block is removed whenever the cache is overflowed. In LFU we check the old page as well as the frequency of that page and if the frequency of the page is larger than the old page we cannot remove it and if all the old pages are having same frequency then take last i.e FIFO method for that and remove that page.
Min-heap data structure is a good option to implement this algorithm, as it handles insertion, deletion, and update in logarithmic time complexity. A tie can be resolved by removing the least recently used cache block. The following two containers have been used to solve the problem:
- A vector of integer pairs has been used to represent the cache, where each pair consists of the block number and the number of times it has been used. The vector is ordered in the form of a min-heap, which allows us to access the least frequently used block in constant time.
- A hashmap has been used to store the indices of the cache blocks which allows searching in constant time.
Below is the implementation of the above approach:
Cache block 1 inserted. Cache block 2 inserted. Cache block 3 inserted. Cache block 4 inserted. Cache block 3 removed. Cache block 5 inserted.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- LRU Cache Implementation
- Amazon’s most frequently asked interview questions | Set 2
- Cache Memory Design
- LRU Cache in Python using OrderedDict
- Concept of Cache Memory Design
- Design a data structure for LRU Cache
- Cache Memory in Computer Organization
- Difference between Virtual memory and Cache memory
- Implementation of Binomial Heap
- Implementation of Access Matrix in Distributed OS
- Implementation of lower_bound and upper_bound on Set of Pairs in C++
- Implementation of lower_bound() and upper_bound() on Map of Pairs in C++
- Queue - Linked List Implementation
- Implementation of lower_bound() and upper_bound() in Vector of Pairs in C++
- Stable-Storage Implementation in Operating system
- Implementation of file allocation methods using vectors
- Implementation of Contiguous Memory Management Techniques
- Implementation of Binomial Heap | Set - 2 (delete() and decreseKey())
- Stack Implementation in Operating System uses by Processor
- Implementation of Non-Preemptive Shortest Job First using Priority Queue
Improved By : vaibhav29498