Interactive reference guide to Z3003 executable Python modules, complexity bounds, verified self-test outputs, and one-click Google Colab notebooks spanning Part 1 foundations and Part 2 large-scale algorithms.
Topic:
Part 2 · Weeks 10–11SGD per epoch: O(|K| * k) time · O((|U| + |I|) * k) memory
DM07: Recommender Systems & Matrix Factorization (SVD)
Implements user-based cosine collaborative filtering and biased latent-factor SGD matrix factorization with L2 regularization and RMSE evaluation.
Python Preview
def matrix_factorization_sgd(ratings, n_users, n_items, k=4, lr=0.03, reg=0.02, epochs=120):
mu = float(np.mean([r for _, _, r in ratings]))
bu, bi = np.zeros(n_users), np.zeros(n_items)
P = rng.normal(0.0, 0.1, size=(n_users, k))
Q = rng.normal(0.0, 0.1, size=(n_items, k))
for _ in range(epochs):
for u, i, r in ratings:
err = r - (mu + bu[u] + bi[i] + float(Q[i] @ P[u]))
bu[u] += lr * (err - reg * bu[u])
bi[i] += lr * (err - reg * bi[i])
P[u], Q[i] = P[u] + lr * (err * Q[i] - reg * P[u]), Q[i] + lr * (err * P[u] - reg * Q[i])
return mu, bu, bi, P, Q
Part 2 · Weeks 12–13Power iteration: O((V + E) * iters) time · O(V + E) memory
DM08: Web Graph, PageRank Power Iteration & HITS
Builds column-stochastic transition matrices with dead-end mass redistribution, random teleportation (beta = 0.85), and normalized HITS hub/authority updates.
Python Preview
def pagerank(adj, beta=0.85, tol=1e-10, max_iter=200):
# Dead ends redistribute uniform mass 1/n across all nodes to prevent rank leak
r = np.full(n, 1.0 / n, dtype=float)
teleport = (1.0 - beta) / n
for iters in range(1, max_iter + 1):
r_next = beta * (M @ r) + teleport
if float(np.sum(np.abs(r_next - r))) < tol:
return {u: float(r_next[idx[u]]) for u in nodes}, iters
r = r_next
Part 2 · Weeks 14–15Single pass: O(1) per stream item · O(k * log U) sketch memory
DM09: Data Streams (Flajolet-Martin & AMS Second Moment)
Estimates distinct element cardinality F0 via trailing-zero bit sketches across independent hashes and tracks frequency second moment F2 using unbiased AMS estimators.
Python Preview
def flajolet_martin_estimate(stream, n_hashes=16, groups=4):
# Track max trailing zero bit position R per hash; take median of group means
max_r = [0] * n_hashes
for item in stream:
for h_idx in range(n_hashes):
max_r[h_idx] = max(max_r[h_idx], _trailing_zeros(hash_val(item, h_idx)))
return float(statistics.median(group_means))