Lecture 6 - Online - Algorithms
Lecture 6 - Online - Algorithms
Online Algorithms
1
Contents
¢ Introduction
¢ Online algorithms analysis
u Competitive analysis
¢ Motivating examples
u Ski rental problem
u 2-lane line search problem
u Paging problem
u Bélády's anomaly
2
Introduction
3
Introduction
u Algorithm 2
ê Divide and conquer approach (presented in the previous
session)
u Are these algorithms online or offline?
4
Online algorithms analysis
5
Online algorithms analysis
¢ Competitive analysis
u An online algorithm is c-competitive if there is a constant
b for all input sequences s
CON(s) ≤ c COPT(s) + b
Where CON(s) is the cost of online algorithm on the sequence s
and COPT(s) is the optimal offline cost for the same sequence
u Competitive analysis compares the relative performance
of an online and offline algorithm for the same problem
u Competitive ratio: The ratio between the output
generated by an online algorithm and the output produced
by an optimal offline algorithm
ê The closer competitive ratio is to 1 the better
6
Ski rental problem (1/4)
7
Ski rental problem (2/4)
8
Ski rental problem (3/4)
1.9
9
Ski rental problem (4/4)
10
2-lane line search problem (1/5)
Left Right
x’ O x
11
2-lane line search problem (2/5)
Figure 3. The worst case when the robot finds the object
13
2-lane line search problem (4/5)
1 ∗ 1 − 2-02
= 2∗ + 2- + 𝜀
1−2
= 2 ∗ (2-02 − 1) + 2- + 𝜀
= (23+1) ∗ 2- + 𝜀 − 2
≈ 9 ∗ 2- + 𝜀
where j is the number of iterations and 𝜀 is a small distance
14
2-lane line search problem (5/5)
15
Paging problem (1/5)
16
Paging problem (2/5)
17
Paging problem (3/5)
u Continue…
18
Paging problem (4/5)
19
Paging problem (5/5)
20
Bélády's anomaly (1/5)
¢ Bélády's anomaly is a phenomenon where increasing the
size of cache memory results in an increase of page
faults
¢ Bélády's anomaly is commonly experienced with FIFO
algorithm but never occurs in LRU algorithm
¢ Reason behind Bélády's anomaly:
u LRU is a stack-based algorithm -> Do not suffer Bélády's
anomaly
u Stack property: the set of pages that were presented in cache
memory when the size of cache memory is k will be
compulsorily presented in cache memory if the size of cache
memory increases to k’>k
21
Bélády's anomaly (2/5)
¢ FIFO algorithm example:
u Given the request: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
¢ Case 1: If k = 3, the given request using FIFO
algorithm yields a total of 9 page faults (PF)
Stage 1 Stage 2 Stage 3 Stage 4 Stage 5 Stage 6 Stage 7 Stage 8 Stage 9 Stage 10 Stage 11 Stage 12
1 1 1 2 3 4 1 1 1 2 5 5
2 2 3 4 1 2 2 2 5 3 3
3 4 1 2 5 5 5 3 4 4
PF PF PF PF PF PF PF O O PF PF O
22
Bélády's anomaly (3/5)
¢ At stage 7 and stage 8 in Case 2, cache memory
does not contain the set of pages that are
presented in the corresponding stages in Case 1
¢ Thus, FIFO algorithm does not follow the stack
property
¢ FIFO algorithm suffers Bélády's anomaly
u The number of page faults increases from 9 to 10 when
increasing the size of cache memory from 3 to 4
¢ Note: It would be wrong to say that “FIFO algorithm
always suffers from Bélády's anomaly”
23
Bélády's anomaly (4/5)
¢ LRU algorithm example:
u Given the request: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
¢ If k = 3, the given request using LRU algorithm
yields a total of 10 page faults (PF)
Stage 1 Stage 2 Stage 3 Stage 4 Stage 5 Stage 6 Stage 7 Stage 8 Stage 9 Stage 10 Stage 11 Stage 12
1 2 3 4 1 2 5 1 2 3 4 5
1 2 3 4 1 2 5 1 2 3 4
1 2 3 4 1 2 5 1 2 3
PF PF PF PF PF PF PF O O PF PF PF
24
Bélády's anomaly (5/5)
¢ At stages in Case 2, cache memory contains the set
of pages that are presented in the corresponding
stages in Case 1
¢ LRU algorithm follows the stack property
¢ LRU does not suffer Bélády's anomaly:
u The number of page faults decreases from 10 to 8 when
increasing the size of cache memory from 3 to 4
25