0% found this document useful (0 votes)
24 views

Lecture 6 - Online - Algorithms

This document provides an overview of online algorithms and their analysis using competitive analysis. It discusses several motivating examples for online algorithms, including the ski rental problem, 2-lane line search problem, and paging problem. For each problem, it describes the online algorithm used, analyzes its competitive ratio compared to an optimal offline algorithm, and summarizes the results.

Uploaded by

minhtuelt10a4
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Lecture 6 - Online - Algorithms

This document provides an overview of online algorithms and their analysis using competitive analysis. It discusses several motivating examples for online algorithms, including the ski rental problem, 2-lane line search problem, and paging problem. For each problem, it describes the online algorithm used, analyzes its competitive ratio compared to an optimal offline algorithm, and summarizes the results.

Uploaded by

minhtuelt10a4
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Lecture 6.

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

¢ An online algorithm is one that can process its input


piece-by-piece, without having the entire input
available from the beginning
u For example, insertion sort is an online algorithm because
ê It considers one input element per iteration
ê It produces a partial solution without considering future
elements

¢ In contrast, an offline algorithm is given the whole


problem data from the beginning
u For example, selection sort is an offline algorithm because
ê It requires access to the entire input to sort an array by
repeatedly finding the minimum element

3
Introduction

¢ Example: Computing the maximum of an array x[1..n]


u Algorithm 1
max = x[1]
for i = 2..n do
if max < x[i] then
max = x[i]
endif
endfor

u Algorithm 2
ê Divide and conquer approach (presented in the previous
session)
u Are these algorithms online or offline?

4
Online algorithms analysis

¢ Note that the final result of an insertion sort is


optimum, i.e., a correctly sorted list
¢ However, for many problems, online algorithms
cannot match the performance of offline algorithms

¢ If the ratio between the performance of an online


algorithm and an optimal offline algorithm is
bounded, the online algorithm is called competitive

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)

¢ Problem description: We will go skiing. We have two options


for the equipment: (1) Buying it costs $500; (2) Renting it for a
day costs $50. Should we buy or rent it?
¢ Our goal is to minimize the cost
¢ Observations: The cost depends on how many times we will go
skiing. Specifically,
u If we will go skiing 11 times or more, then it is better to buy the
equipment
u If we will go skiing 9 times or fewer, then it is better to rent the
equipment
¢ Why should this problem be solved by an online algorithm?
u Skying may depend on the weather

7
Ski rental problem (2/4)

¢ An online algorithm called better-late-than-never


will be a number k such that after renting k-1 times,
we will buy the equipment (just before your kth time)
¢ Competitive analysis
u For every k, the cost for online algorithm is 50(k-1) + 500
u For k ≤ 10, we should rent -> the cost for optimal offline
algorithm is 50k, so the competitive ratio is
500 + 50(𝑘 − 1) 9
=1+
50𝑘 𝑘
u If k ≥ 10, we should buy -> the cost for optimal offline
algorithm is 500, so the competitive ratio is
500 + 50(𝑘 − 1) 𝑘
= 0.9 +
500 10

8
Ski rental problem (3/4)

1.9

Figure 1. Cost of online (blue) Figure 2. Competitive ratio between


and offline (red) algorithms online and offline algorithms

¢ From Figure 2, the best choice of k is 10, which


gives the competitive ratio 1.9 (the smallest ratio)

9
Ski rental problem (4/4)

¢ In general, we suppose that the purchase cost is p and the


rental cost is 1, the number of times going skiing is t
¢ The cost of the offline algorithm is min(p, t)
¢ If we set k = p, we never pay more than twice the cost of the
offline algorithm
u We buy the equipment after renting k-1 times, so the total payment is k-1
+ p = 2p–1
u If t >= k, then the cost of the offline algorithm is min(p, t) = p, and the
competitive ratio is (2p–1)/p = 2–1/p
u If t < k, then the cost of the offline algorithm is t and the competitive
ratio is (2p–1)/t > (2p-1)/p = 2–1/p

¢ So the better-late-than-never algorithm is (2–1/p)-


competitive

10
2-lane line search problem (1/5)

¢ Problem description: A robot starts at the origin of


the x-axis. An object has been placed somewhere
on the x-axis. The robot can switch direction of
travel immediately, but in order for the robot to
determine that there is an object at location x’,
the robot has to be physically present at x’. How
should the robot explore the x-axis in order to find
the object as soon as possible?
Left or Right?
How should I go?

Left Right
x’ O x

11
2-lane line search problem (2/5)

¢ We investigate an online algorithm called doubling


strategy to find the object
Input:
1) Current direction:= Left
2) Current position:= 0
3) d:= 1
Repeat:
1) Travel d unit on the current direction
2) If found the object then finish
3) Else return to starting point
4) d:= 2d
5) Flip the current direction
12
2-lane line search problem (3/5)

¢ The robot follows an online algorithm named


doubling strategy to find the object

x’ Flip direction O Start Flip direction x


Found object 1
1 2
4 2
4 8
𝜀 4 8

Figure 3. The worst case when the robot finds the object

13
2-lane line search problem (4/5)

¢ We derive the competitive ratio of the doubling


strategy
¢ If the object locates at position x′ = 2- + 𝜀.
¢ We have the total distance of this algorithm:
𝐶𝑜𝑠𝑡./ = 2 1 + 2 + 4 + … + 2-01 + 2- + 𝜀

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)

¢ The cost of optimal offline algorithm when the


robot knows where the object locates at x′ = 2- + 𝜀
𝐶𝑜𝑠𝑡.45 = 𝑥′ = 2- + 𝜀
¢ Finally, combine 𝐶𝑜𝑠𝑡𝑂𝑁 and 𝐶𝑜𝑠𝑡𝑂𝑃𝑇, we obtain the
competitive ratio as following
6789!" :∗2% 0<
Competitive ratio= 6789!#$
= 2% 0<
≈9

¢ Conclusion: The doubling strategy is 9-competitive


for the 2-lane line search problem

15
Paging problem (1/5)

¢ Problem description: We have a disk with N pages, and


cache with space k < N. When a memory request is
made, if the page isn’t in the cache -> page fault. We
then need to bring the page into the cache and throw
something else out if our cache is full. Our goal is to
minimize the number of page faults. The algorithmic
question is: which page should we throw out?
¢ There are some online algorithms for this problem:
u Least-Recently-Used (LRU) (We will investigate this
algorithm)
u First-In-First-Out (FIFO)
u Flush-When-Full (FWF)

16
Paging problem (2/5)

¢ Least-Recently-Used (LRU) algorithm evicts the


page that has been unused for the longest time

¢ Competitive ratio is used to analyze LRU algorithm


# "# $%&'(&")* "# ")+&)$ ,+-".&(/0(2)
Competitive ratio=
# "# $%&'(&")* "# "4(&0,+ "##+&)$ ,+-".&(/0(2)

where σ is the input sequence of requests

17
Paging problem (3/5)

¢ We will derive the competitive ratio of LRU algorithm


¢ Consider the cache has k slots, which are initially
empty and the input sequence
σ = 1, 2, 3, …, k, k+1, 1, 2, 3, …, k
u After arriving of first k inputs, cache looks like

u When k+1 arrives, 1 will be evicted because 1 is unused for


the longest time, cache then looks like

u Continue…

18
Paging problem (4/5)

u When 1 arrives again, 2 will be evicted, cache looks like

u Similarly, all the k pages will be evicted one by one

u And, when the last input k arrives, k+1 will be evicted

19
Paging problem (5/5)

¢ So, the total number of evictions using LRU is k+1


¢ Now, if we use the optimal algorithm for the same
input sequence σ, the cache will look like

u Hence, the number of evictions for optimal algorithm is 2


=01
Competitive ratio = 2

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

¢ Case 2: If k = 4, the given request using FIFO


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 1 1 1 1 1 2 3 4 5 1 2
2 2 2 2 2 3 4 5 1 2 3
3 3 3 3 4 5 1 2 3 4
4 4 4 5 1 2 3 4 5
PF PF PF PF O O PF PF PF PF PF PF

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

¢ If k = 4, the given request using LRU algorithm


yields a total of 8 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
1 2 3 4 4 4 5 1 2
PF PF PF PF O O 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

You might also like