| Start |
|---|
Paging (Online Caching) PrefaceInstead of starting with a formal definition, the goal is to approach these topic via a row of examples, introducing definitions along the way. The remark section Theory will consist of all definitions, theorems and propositions to give you all informations to faster look up specific aspects. The remark section sources consists of the basis material used for this topic and additional information for further reading. In addition you will find the full source codes for the examples there. Please pay attention that to make the source code for the examples more readable and shorter it refrains from things like error handling etc. It also passes on some specific language features which would obscure the clarity of the example like extensive use of advanced libraries etc. PagingThe paging problem arises from the limitation of finite space. Let's assume our cache We say a request is a cache hit, when the page is already in cache, otherwise, its called a cache miss. In that case, we must bring the requested page into the cache and evict another, assuming the cache is full. The Goal is an eviction schedule that minimizes the number of evictions. There are numerous strategies for this problem, let's look at some:
There are two ways to approach this problem:
Offline ApproachFor the first approach look at the topic Applications of Greedy technique (opens new window). It's third Example Offline Caching considers the first five strategies from above and gives you a good entry point for the following. The example program was extended with the FWF strategy: The full sourcecode is available here (opens new window). If we reuse the example from the topic, we get the following output: Even though LFD is optimal, FWF has fewer cache misses. But the main goal was to minimize the number of evictions and for FWF five misses mean 15 evictions, which makes it the poorest choice for this example. Online ApproachNow we want to approach the online problem of paging. But first we need an understanding how to do it. Obviously an online algorithm cannot be better than the optimal offline algorithm. But how much worse it is? We need formal definitions to answer that question: Definition 1.1: An optimization problem Π consists of a set of instances ΣΠ. For every instance σ∈ΣΠ there is a set Ζσ of solutions and a objective function fσ : Ζσ → ℜ≥0 which assigns apositive real value to every solution. Definition 1.2: An online algorithm A for a minimization problem Π has a competetive ratio of r ≥ 1 if there is a constant τ∈ℜ with wA(σ) = fσ(A(σ)) ≤ r ⋅ OPT(σ) + τ for all instances σ∈ΣΠ. A is called a r-competitive online algorithm. Is even wA(σ) ≤ r ⋅ OPT(σ) for all instances σ∈ΣΠ then A is called a strictly r-competitive online algorithm. So the question is how competitive is our online algorithm compared to an optimal offline algorithm. In their famous book (opens new window) Allan Borodin and Ran El-Yaniv used another scenario to describe the online paging situation: There is an evil adversary who knows your algorithm and the optimal offline algorithm. In every step, he tries to request a page which is worst for you and simultaneously best for the offline algorithm. the competitive factor of your algorithm is the factor on how badly your algorithm did against the adversary's optimal offline algorithm. If you want to try to be the adversary, you can try the Adversary Game (opens new window) (try to beat the paging strategies). Marking AlgorithmsInstead of analysing every algorithm separately, let's look at a special online algorithm family for the paging problem called marking algorithms. Let σ=(σ1,...,σp) an instance for our problem and k our cache size, than σ can be divided into phases:
For example with k = 3: A marking algorithm (implicitly or explicitly) maintains whether a page is marked or not. At the beginning of each phase are all pages unmarked. Is a page requested during a phase it gets marked. An algorithm is a marking algorithm iff it never evicts a marked page from cache. That means pages which are used during a phase will not be evicted. Proposition 1.3: LRU and FWF are marking algorithm. Proof: At the beginning of each phase (except for the first one) FWF has a cache miss and cleared the cache. that means we have k empty pages. In every phase are maximal k different pages requested, so there will be now eviction during the phase. So FWF is a marking algorithm. Proposition 1.4: Every marking algorithm is strictly k-competitive. Proof: Let σ be an instance for the paging problem and l the number of phases for σ. Is l = 1 then is every marking algorithm optimal and the optimal offline algorithm cannot be better. wA(σ) ≤ l⋅k ≤ (k+l-2)k ≤ OPT(σ) ⋅ k Corollary 1.5: LRU and FWF are strictly k-competitive. Excercise: Show that FIFO is no marking algorithm, but strictly k-competitive. Is there no constant r for which an online algorithm A is r-competitive, we call A not competitive Proposition 1.6: LFU and LIFO are not competitive. Proof: Let l ≥ 2 a constant, k ≥ 2 the cache size. The different cache pages are nubered 1,...,k+1. We look at the following sequence: The first page 1 is requested l times than page 2 and so one. At the end, there are (l-1) alternating requests for page k and k+1. which is equal to To satisfy this inequality you just have to choose l sufficient big. So LFU and LIFO are not competitive. Proposition 1.7: There is no r-competetive deterministic online algorithm for paging with r < k. The proof for this last proposition is rather long and based of the statement that LFD is an optimal offline algorithm. The interested reader can look it up in the book of Borodin and El-Yaniv (see sources below). The Question is whether we could do better. For that, we have to leave the deterministic approach behind us and start to randomize our algorithm. Clearly, its much harder for the adversary to punish your algorithm if it's randomized. Randomized paging will be discussed in one of next examples... RemarksTheoryDefinition 1: An optimization problem Π consists of a set of instances ΣΠ. For every instance σ∈ΣΠ there is a set Ζσ of solutions and a objective function fσ : Ζσ → ℜ≥0 which assigns apositive real value to every solution. Definition 2: An online algorithm A for a minimization problem Π has a competetive ratio of r ≥ 1 if there is a constant τ∈ℜ with wA(σ) = fσ(A(σ)) ≤ r ⋅ OPT(&sigma) + τ for all instances σ∈ΣΠ. A is called a r-competitive online algorithm. Is even wA(σ) ≤ r ⋅ OPT(&sigma) for all instances σ∈ΣΠ then A is called a strictly r-competitive online algorithm. Proposition 1.3: LRU and FWF are marking algorithm. Proof: At the beginning of each phase (except for the first one) FWF has a cache miss and cleared the cache. that means we have k empty pages. In every phase are maximal k different pages requested, so there will be now eviction during the phase. So FWF is a marking algorithm. Proposition 1.4: Every marking algorithm is strictly k-competitive. Proof: Let σ be an instance for the paging problem and l the number of phases for σ. Is l = 1 then is every marking algorithm optimal and the optimal offline algorithm cannot be better. wA(σ) ≤ l⋅k ≤ (k+l-2)k ≤ OPT(σ) ⋅ k Corollary 1.5: LRU and FWF are strictly k-competitive. Is there no constant r for which an online algorithm A is r-competitive, we call A not competitive. Proposition 1.6: LFU and LIFO are not competitive. Proof: Let l ≥ 2 a constant, k ≥ 2 the cache size. The different cache pages are nubered 1,...,k+1. We look at the following sequence: First page 1 is requested l times than page 2 and so one. At the end there are (l-1) alternating requests for page k and k+1. which is equal to To satisfy this inequality you just have to choose l sufficient big. So LFU and LIFO are not competetive. Proposition 1.7: There is no r-competetive deterministic online algorithm for paging with r < k. SourcesBasic Material
Further Reading
Source Code
|
Read Article
https://develop.horje.com/learn/1434/reference