Skip to Content
CSE347Analysis of Algorithms (Lecture 11)

CSE347 Analysis of Algorithms (Lecture 11)

More randomized algorithms

Caching problem: You have a cache with kk blocks and a sequence of accesses, called σ\sigma. The cost of a randomized caching algorithm is the expected number of cache misses on σ\sigma.

Randomized Marking Algorithm

A phase ii has nin_i new pages.

Our goal is to optimize m(σ)12i=1nnjm^*(\sigma)\geq \frac{1}{2}\sum_{i=1}^{n} n_j where njn_j is the number of new pages in phase jj.

Marking algorithm:

  • at a cache miss, evict an unmarked page uniformly at random
  • at the beginning of the algorithm, all the entries are unmarked
  • after kk unique accesses and one miss, all the entries are unmarked
  • old pages: pages in cache at the end of the previous phase
  • new pages: pages accessed in this phase that are not old.
  • new pages always cause a miss.
  • old pages can cause a miss if a new page was accessed and replaced that old page and then the old page was accessed again. This can also be caused by old pages replacing other old pages and creating this cascading effect.

Reminder: Competitive ratio for our randomized algorithm is

maxσ{E[m(σ)]m(σ)}max_\sigma \{\frac{E[m(\sigma)]}{m^*(\sigma)}\}
def randomized_marking_algorithm(sigma, k): cache = set() marked = set() misses = 0 for page in sigma: if page not in cache: # once all the blocks are marked, unmark all the blocks if len(marked) == k: marked.clear() # if the cache is full, randomly remove a page that is not marked if len(cache) == k: for page in cache: if page not in marked: cache.remove(page) misses += 1 # add the new page to the cache and mark it cache.add(page) marked.add(page) return misses

Example:

A cache on phase ii has kk blocks and miss on page xx:

[nin_i new pages] [oio_i old pages] [xx] [\ldots]

P[x causes a miss]=P[x was evicted earlier]njkoiP[x \text{ causes a miss}] = P[x\text{ was evicted earlier}] \leq \frac{n_j}{k-o_i}

Proof:

Warning: the first few line of the equation might be wrong.

P[x was evicted earliernj new pages,oi old pages,k unmarked blocks]=P[x was unmarked]+P[x was marked]=P[x was unmarked (new page)]+P[x was old page]+P[x was in the remaining cache blocks]=1k+oikP[x was evicted earliernj1 new pages,oi1 old pages,k1 unmarked blocks]+k1oikP[x was evicted earliernj1 new pages,oi old pages,k1 unmarked blocks]\begin{aligned} P\left[x \text{ was evicted earlier}\bigg\vert\begin{array}{c} n_j\text{ new pages}, \\ o_i\text{ old pages}, \\ k \text{ unmarked blocks} \end{array}\right] &=P[x\text{ was unmarked}]+P[x\text{ was marked}] \\ &=P[x\text{ was unmarked (new page)}]+P[x\text{ was old page}]+P[x\text{ was in the remaining cache blocks}] \\ &= \frac{1}{k}+\frac{o_i}{k} P\left[x \text{ was evicted earlier}\bigg\vert\begin{array}{c} n_j-1\text{ new pages}, \\ o_i-1\text{ old pages}, \\ k-1 \text{ unmarked blocks} \end{array}\right] +\frac{k-1-o_i}{k} P\left[x \text{ was evicted earlier}\bigg\vert\begin{array}{c} n_j-1\text{ new pages}, \\ o_i\text{ old pages}, \\ k-1 \text{ unmarked blocks} \end{array}\right] \\ \end{aligned}

Let P(nj,oi,k)P(n_j, o_i, k) be the probability that page xx causes a miss when the cache has njn_j new pages, oio_i old pages, and kk unmarked blocks.

Using P(nj,oi,k)njkoiP(n_j, o_i, k)\leq \frac{n_j}{k-o_i}, we have

P(nj,oi,k)=1k+oikP(nj1,oi1,k1)+k1oikP(nj1,oi,k1)1k+oiknj1k1oi1+k1oiknj1k1oi=1k+(1+oinkoi+nj1koi)=1k(koi+oin+(nj1)(koi)koi)=njkoi\begin{aligned} P(n_j, o_i, k) &= \frac{1}{k}+\frac{o_i}{k} P(n_j-1, o_i-1, k-1)+\frac{k-1-o_i}{k} P(n_j-1, o_i, k-1) \\ &\leq \frac{1}{k}+\frac{o_i}{k} \frac{n_j-1}{k-1-o_i-1}+\frac{k-1-o_i}{k} \frac{n_j-1}{k-1-o_i} \\ &= \frac{1}{k}+\left(1+\frac{o_in}{k-o_i}+\frac{n_j-1}{k-o_i}\right)\\ &=\frac{1}{k}\left(\frac{k-o_i+o_in+(n_j-1)(k-o_i)}{k-o_i}\right)\\ &= \frac{n_j}{k-o_i} \end{aligned}

Fix a phase jj, let xix_i be an indicator random variable

xi={1if page ith old page causes a miss0otherwisex_i=\begin{cases} 1 & \text{if page } i \text{th old page causes a miss} \\ 0 & \text{otherwise} \end{cases} P[xi=1]=P[ith old page causes a miss]njk(i1)\begin{aligned} P[x_i=1]&=P[i\text{th old page causes a miss}]\\ &\leq \frac{n_j}{k-(i-1)}\\ \end{aligned} E[xi]=E[i=1oiP[xi=1]]=E[nj+i=1knjxi]=nj+i=1knjE[xi]nj+i=1knjnjk(i1)=nj+(1+1k+1k1++1nj)njHk\begin{aligned} E[x_i]&=E[\sum_{i=1}^{o_i} P[x_i=1]]\\ &= E[n_j+\sum_{i=1}^{k-n_j}x_i]\\ &=n_j+\sum_{i=1}^{k-n_j} E[x_i]\\ &\leq n_j+\sum_{i=1}^{k-n_j} \frac{n_j}{k-(i-1)}\\ &=n_j+\left(1+\frac{1}{k}+\frac{1}{k-1}+\cdots+\frac{1}{n_j}\right)\\ &\leq n_j H_k\\ \end{aligned}

Let NN be the total number of phases.

So the expected total number of misses is

E[i=1Nxi]i=1NE[xi]j=1NnjHkE[\sum_{i=1}^{N} x_i]\leq \sum_{i=1}^{N} E[x_i]\leq\sum_{j=1}^{N} n_j H_k

So the competitive ratio is

E[i=1Nxi]12j=1Nnj2Hk=O(logk)\frac{E[\sum_{i=1}^{N} x_i]}{\frac{1}{2}\sum_{j=1}^{N} n_j}\leq 2H_k=O(\log k)

Probabilistic boosting for decision problems

Assume that you have a randomized algorithm that gives you the correct answer with probability 12+ϵ\frac{1}{2}+\epsilon. for some ϵ>0\epsilon>0.

I want to boost the probability of the correct decision to be 1δ\geq 1-\delta.

What we can do is to run the algorithm xx times independently with probability 12+ϵ\frac{1}{2}+\epsilon and take the majority vote.

The probability of the wrong decision is

(xx/2)(12ϵ)x/2\binom{x}{\lceil x/2\rceil} \left(\frac{1}{2}-\epsilon\right)^{\lceil x/2\rceil}

I want to choose xx such that this is δ\leq \delta.

(1p)1pe1(1-p)^{\frac{1}{p}}\leq e^{-1}

So

(xx/2)(12ϵ)x/2(xex/2)x/2(12ϵ)x/2ϵ\begin{aligned} \binom{x}{\lceil x/2\rceil}\left(\frac{1}{2}-\epsilon\right)^{\lceil x/2\rceil}&\leq \left(\frac{xe}{x/2}\right)^{\lceil x/2\rceil}\left(\frac{1}{2}-\epsilon\right)^{-\lceil x/2\rceil\epsilon} \end{aligned}

We use this to solve for xx.

Last updated on