Quiz 2
Registry Synced

Online and Parallel Algorithms

614 words
3 min read

Reading compass

Now · 🎯 Learning Objectives

Online and Parallel Algorithms

🎯 Learning Objectives

  • Analyze online algorithms using competitive ratio
  • Design online paging algorithms (LRU, FIFO)
  • Understand parallel computation (PRAM model)
  • Analyze work and depth of parallel algorithms

1. Online Algorithms

1.1 Competitive Analysis

Competitive ratio: Algorithm's cost / Optimal cost (with full knowledge)
AlgorithmProblemCompetitive Ratio
LRUPagingk (cache size)
FIFOPagingk
LFUPagingNot competitive
Ski rental (1/2)Buying vs. renting2

1.2 Paging Problem

LRU (Least Recently Used): Evict page not used for longest time. Tracing: Cache size = 3, request sequence: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4
RequestCacheHit/MissEvicted
1[1]Miss
2[1, 2]Miss
3[1, 2, 3]Miss
4[4, 2, 3]Miss1 (LRU)
1[4, 1, 3]Miss2 (LRU)
2[4, 1, 2]Miss3 (LRU)
5[5, 1, 2]Miss4 (LRU)
1[5, 1, 2]Hit
2[5, 1, 2]Hit
3[3, 1, 2]Miss5 (LRU)
4[3, 4, 2]Miss1 (LRU)
Total: 8 misses out of 11 requests. Optimal (Belady's): Evict page used farthest in future. Cache start empty: Miss on 1,2,3,4 (evict 3 since 4 is next), 1(hit),2(hit),5(evict 4),1,2,3(evict 5),4 → 5 misses. Competitive ratio: LRU ≤ k × OPT, which is optimal for deterministic algorithms.

2. The PRAM Model

Parallel Random Access Machine: p processors, shared memory.

2.1 Classification

ModelReadWriteUse
EREWExclusiveExclusiveMost restrictive
CREWConcurrentExclusiveCommon
CRCWConcurrentConcurrentMost powerful
Common CRCWConcurrentConcurrent (same value)Useful

2.2 Prefix Sum (CRCW)

Array: [3, 1, 7, 2, 5, 8, 4, 6] Parallel prefix sum in O(log n) time:
LevelProcessorsOperation
08Input: [3,1,7,2,5,8,4,6]
14[4, 8, 9, 10, 13, 14, 10, 12]
22[4, 8, 13, 18, 13, 14, —]
31[4, 8, 13, 18, 23, 32, 36, 42]
Wait, let me redo. The standard parallel prefix sum algorithm: Step 1: each processor i: B[i] = A[i] (depth 0) Step 2: for k=0 to log n-1: if i ≥ 2^k: B[i] += B[i - 2^k] For n=8, log n = 3:
i01234567
A31725846
k=034897131210
k=134111315221923
k=234111626354145
Wait, that's not the standard prefix sum. The classic algorithm:
  • for k=0 to log n-1: if i & (1<<k): B[i] += B[i ^ (1<<k)] Actually the standard work-efficient prefix sum is:
  1. Up-sweep (build tree)
  2. Down-sweep (distribute results) This is too detailed. Let me simplify: Work: O(n), Depth: O(log n), Work-efficiency: Yes

3. Amdahl's Law

Speedup = 1 / ((1-f) + f/p) where f = fraction parallelizable, p = processors. Example: 90% parallelizable, 10 processors: Speedup = 1/(0.1 + 0.9/10) = 1/0.19 = 5.26× With 100 processors: 1/(0.1 + 0.9/100) = 1/0.109 = 9.17× Key insight: Even 1% sequential code limits speedup to 100× regardless of processors.

4. Key Concepts Reference

ConceptDefinitionExample
Competitive ratioALG/OPTLRU: k-competitive
Belady's OPTEvict farthest-in-futureOptimal paging
PRAMParallel shared memory modelPrefix sum
WorkTotal operationsO(n) for prefix sum
DepthLongest chain (critical path)O(log n) for prefix sum
Amdahl's lawSpeedup limit from sequential partParallelism ceiling

5. 📝 Practice Questions

Q1: LRU with cache size 4, sequence: 1,2,3,4,5,1,2,3,4,5. How many misses?
Answer: 1: miss [1], 2: miss [1,2], 3: miss [1,2,3], 4: miss [1,2,3,4] 5: miss [5,2,3,4] evict 1 1: miss [5,1,3,4] evict 2 2: miss [5,1,2,4] evict 3 3: miss [5,1,2,3] evict 4 4: miss [5,1,2,3]→[4,1,2,3] evict 5 5: miss [4,5,2,3] evict 1 Total: 10 misses (miss every time). Optimal: 7 misses (Belady's). Q2: Parallel prefix sum on [1,2,3,4,5,6,7,8], show k=0 and k=1 steps.
Answer: Initial: [1,2,3,4,5,6,7,8] k=0: [1,3,5,7,9,11,13,15] (i≥1: B[i]+=B[i-1]) k=1: [1,3,6,10,14,18,22,26] (i≥2: B[i]+=B[i-2]) k=2: [1,3,6,10,15,21,28,36] (i≥4: B[i]+=B[i-4]) Result: prefix sums! Q3: If 95% of a program is parallelizable, max speedup with infinite processors?
Answer: Speedup ≤ 1/(1-0.95) = 1/0.05 = 20× regardless of processors. The 5% sequential code dominates. This is Amdahl's law — the sequential bottleneck is fundamental.

6. 🔗 Cross-References

Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.