| Start | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Ticket automat First simple Example: You have a ticket automat which gives exchange in coins with values 1, 2, 5, 10 and 20. The dispension of the exchange can be seen as a series of coin drops until the right value is dispensed. We say a dispension is optimal when its coin count is minimal for its value. Let The Greedy Technique for the exchange is the following pseudo algorithmic approach: Step 1: while Afterwards the sum of all coins clearly equals Now the ticket automat as program (in C++): Be aware there is now input checking to keep the example simple. One example output: As long as
But the algorithm has two pitfalls:
A simple counter example: the coins are |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Interval Scheduling We have a set of jobs
The question now is, which approach is really successfull. Early start time definetly not, here is a counter example
Or as C++ program: The output for this example is: The implementation of the algorithm is clearly in Θ(n^2). There is a Θ(n log n) implementation and the interested reader may continue reading below (Java Example). Now we have a greedy algorithm for the interval scheduling problem, but is it optimal? Proposition: The greedy algorithm earliest finish time is optimal. Proof:****(by contradiction) Assume greedy is not optimal and The job This second example demonstrates that there are usually many possible greedy strategies but only some or even none might find the optimal solution in every instance. Below is a Java program that runs in Θ(n log n) And the expected output is: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Minimizing Lateness There are numerous problems minimizing lateness, here we have a single resource which can only process one job at a time. Job
The solution
Its easy to see that shortest processing time first is not optimal a good counter example is
the smallest stack solution has simillar problems
the last strategy looks valid so we start with some pseudo code:
And as implementation in C++: And the output for this program is: The runtime of the algorithm is obviously Θ(n log n) because sorting is the dominating operation of this algorithm. Now we need to show that it is optimal. Clearly an optimal schedule has no idle time. the earliest deadline first schedule has also no idle time. Lets assume the jobs are numbered so that Proposition: Swapping two adjacent, inverted jobs reduces the number of inversions by one and does not increase the maximal lateness. Proof: Let Clearly it is That means the lateness after swap is less or equal than before. This concludes the proof. Proposition: The earliest deadline first schedule Proof:****(by contradiction) Lets assume The minimizing lateness problem and its near related minimum makespan problem, where the question for a minimal schedule is asked have lots of applications in the real world. But usually you don't have only one machine but many and they handle the same task at different rates. These problems get NP-complete really fast. Another interesting question arises if we don't look at the offline problem, where we have all tasks and data at hand but at the online variant, where tasks appear during execution. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Offline Caching The caching problem arises from the limitation of finite space. Lets assume our cache We say a request is a cache hit, when the item is already in cache, otherwise its called a cache miss. In that case we must bring the requested item into cache and evict another, assuming the cache is full. The Goal is a eviction schedule that minimizes the number of evictions. There are numerous greedy strategies for this problem, lets look at some:
Attention: For the following examples we evict the page with the smallest index, if more than one page could be evicted. Example (FIFO)Let the cache size be
Thirteen cache misses by sixteen requests does not sound very optimal, lets try the same example with another strategy: Example (LFD)Let the cache size be
Eight cache misses is a lot better. Selftest: Do the example for LIFO, LFU, RFU and look what happend. The following example programm (written in C++) consists of two parts: The skeleton is a application, which solves the problem dependent on the chosen greedy strategy: The basic idea is simple: for every request I have two calls two my strategy:
Now lets look of example implementations for our five strategies: FIFOFIFO just needs the information how long a page is in the cache (and of course only relative to the other pages). So the only thing to do is wait for a miss and then make the pages, which where not evicted older. For our example above the program solution is: Thats exact the solution from above. LIFOThe implementation of LIFO is more or less the same as by FIFO but we evict the youngest not the oldest page. The program results are: LRUIn case of LRU the strategy is independent from what is at the cache page, its only interest is the last usage. The programm results are: LFULFU evicts the page uses least often. So the update strategy is just to count every access. Of course after a miss the count resets. The program results are: LFDThe LFD strategy is different from everyone before. Its the only strategy that uses the future requests for its decission who to evict. The implementation uses the function The greedy strategy LFD is indeed the only optimal strategy of the five presented. The proof is rather long and can be found here (opens new window) or in the book by Jon Kleinberg and Eva Tardos (see sources in remarks down below). Algorithm vs RealityThe LFD strategy is optimal, but there is a big problem. Its an optimal offline solution. In praxis caching is usually an online problem, that means the strategy is useless because we cannot now the next time we need a particular item. The other four strategies are also online strategies. For online problems we need a general different approach. RemarksSources
![]() |
Read Article
https://develop.horje.com/learn/1434/reference