Cache State
Hash Map (Key -> Node Address)
Empty
Cache is empty β use PUT to add items
How LRU Cache Works
MRUA
ββ
B
ββ
C
ββ
LRUD
Items are in a doubly linked list. MRU at left, LRU at right β evicted first when cache is full.
GET
Hit β found β move to MRU, return value.
Miss β not found β return
Miss β not found β return
-1.
PUT
New key β full? evict LRU β insert at MRU.
Existing β update value β move to MRU.
Existing β update value β move to MRU.
π Walkthrough β capacity = 3
PUT 1,Aβ[1]PUT 2,Bβ[2β1]PUT 3,Cβ[3β2β1]GET 1β HIT β[1β3β2]PUT 4,Dβ evict 2 β[4β1β3]Activity Log
liveGraph
Queue / Stack
empty
Visited Order
none yet
Pseudo-code & Log
liveprocedure BFS(G, root) is let Q be a queue Q.enqueue(root) while Q is not empty do v := Q.dequeue() if v is not labeled as discovered then label v as discovered for all edges from v to w in G.adjacentEdges(v) do Q.enqueue(w)
Chessboard
Pseudo-code & Log
livefunction solve(row) { if (row == N) return true; for (col = 0; col < N; col++) { if (isSafe(row, col)) { board[row] = col; if (solve(row + 1)) return true; board[row] = UNASSIGNED; // backtrack } } return false; }
Weighted Graph
Priority Queue (dist β node)
empty
Visited Order
none yet
Pseudo-code & Log
livefunc dijkstra(graph, src): dist[all] = β; dist[src] = 0 prev[all] = null pq.push(src, 0) // (node, dist) while pq not empty: (u, d) = pq.pop_min() // lowest dist if u already visited: skip mark u as visited for each neighbor v of u: if d + w(u,v) < dist[v]: dist[v] = d + w(u,v) pq.push(v, dist[v])