Bonus1
LRU-K
王子轩
Date: 2024-6-8
Chapter 1: Introduction
Least Recently Used (LRU) cache scheme is to remove the least recently used frame (the one hasn’t been used for the longest amount of time) when the cache is full and a new page is referenced which is not there in cache.
LRU-K is a variant of the LRU algorithm, where K represents the number of recent uses. LRU can be considered as LRU-1. Unlike LRU, the LRU-K requires the maintenance of two different queues (for historical access and cache). The data in the historical access queue is not moved to the cache queue until the data is hit K times.
Chapter 2: Algorithm Specification
Pseudo code:
1 | function processIntegers(k, n, m, integerList): |
Chapter 3: Testing Results
input | expect | aim | actual output |
---|---|---|---|
2 5 17 9 5 6 7 8 3 8 9 5 9 8 3 4 7 5 6 9 |
4 9 8 3 7 5 6 |
Experimenting with two lines of output | 4 9 8 3 7 5 6 |
3 5 10 9 5 6 7 8 3 8 9 5 9 |
7 3 8 5 9 - |
Experimenting with one line of output | 7 3 8 5 9 - |
Chapter 4: Analysis and Comments
Time Complexity Derivation:
Initialization operations.
- The time complexity of initializing the queues
cacQueue
andhisQueue
is O(1). - The time complexity of initializing the hash tablehashTable
depends on how the hash table is implemented and is usually O(m), where m is the length of the list of integers.
- The time complexity of initializing the queues
Processing integer lists.
For each integer in the integer list
integerList
, do the following:- Check if the integer is in
cacQueue
orhisQueue
: O(1) (assuming O(1) for hash table operations). - If in queue, remove and rejoin tail of queue: O(1). - If not in queue, check ifhisQueue
is full, if so, remove head element and add new element: O(1). - Update the hash table: O(1). - Thus, for each integer, the total time complexity of the above operations is O(1).
- Check if the integer is in
Output results after processing is complete.
- Remove elements from
hisQueue
andcacQueue
in turn and add them to the result list: O(n + k), where n is the maximum capacity ofhisQueue
and k is the number of elements incacQueue
.
- Remove elements from
In summary, the time complexity of the whole algorithm is O(m + n + k), where m is the length of the list of integers, n is the capacity of hisQueue
, and k is the number of elements in cacQueue
.
Derivation of space complexity:
Queues and hash tables.
- The space complexity of
cacQueue
andhisQueue
is O(k) and O(n) respectively. - The space complexity of the hash tablehashTable
is O(m) because it stores the number of occurrences of each integer.
- The space complexity of
ResultList.
- The space complexity of the result list is O(n + k) because it contains at most all the elements in
hisQueue
andcacQueue
.
- The space complexity of the result list is O(n + k) because it contains at most all the elements in
Therefore, the space complexity of the whole algorithm is O(m + n + k), where m is the length of the integer list, n is the capacity of hisQueue
, and k is the number of elements in cacQueue
.
Directions for improvement:
Time complexity optimization
Optimizing the use of hash tables.
- If the hash table implementation is not optimal, consider using more efficient data structures such as balanced binary search trees (e.g., red-black trees) or jump tables, which provide more stable query and update time complexity when there are more hash conflicts.
Reducing unnecessary operations.
- If there are duplicate or unnecessary computations in the algorithm, try to reduce these operations by caching the results or simplifying the logic.
Batch processing.
- If the algorithm allows it, consider batch processing some operations to reduce the number of accesses to the data structure, thus reducing the time complexity.
Space complexity optimization
Space reuse.
- If the data stored in
hisQueue
andcacQueue
are duplicated or can be shared in some way, you can try to reuse the space to reduce the memory usage.
- If the data stored in
Data structure selection.
- Choose an appropriate data structure to store the data. For example, if the range of elements is limited, you can use a bit array or Bloom filter to reduce the space usage.
Algorithm logic optimization
Reduce queue operations.
- If queue operations are frequent and have an impact on performance, consider whether they can be reduced through algorithm logic tuning.
Avoid duplicate calculations.
- If there are repeated calculations in the algorithm, consider avoiding repeated calculations by caching or preprocessing.
Code level optimization
Loop Expansion.
- For loop structure, if the number of loops is fixed and small, loop unrolling can be used to reduce the loop overhead.
Inline function calls.
- For short functions that are called frequently, consider inlining them to reduce the function call overhead.
Algorithm parallelization.
- If algorithms can be executed in parallel, consider using multi-threading or multi-processing to speed up the computation process.
Appendix: Source Code (in C)
1 |
|
Declaration
I hereby declare that all the work done in this project titled “LRU-K” is of my independent effort.