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.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
- Implementation of Least Recently Used (LRU) page replacement algorithm using Counters
- Amazon’s most frequently asked interview questions | Set 2
- LRU Cache Implementation
- Program for Least Recently Used (LRU) Page Replacement algorithm
- Count pairs (p, q) such that p occurs in array at least q times and q occurs at least p times
- Cache Memory in Computer Organization
- Design a data structure for LRU Cache
- Difference between Virtual memory and Cache memory
- Cache Memory Design
- LRU Cache in Python using OrderedDict
- Concept of Cache Memory Design
- Difference between RAM and Cache
- Difference between Buffer and Cache
- Where is Heap Sort used practically?
- Introduction to Data Structures | 10 most commonly used Data Structures
- Not Recently Used (NRU) page replacement algorithm
- Check if characters of a given string can be used to form any N equal strings
- Implementation of Binomial Heap
- Queue - Linked List Implementation
- Implementation of Binomial Heap | Set - 2 (delete() and decreseKey())
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : vaibhav29498