Sandomhet

CMPSC 130A Algorithms and Data Structures

Algorithm analysis and graph algorithms including shortest path, 2-SAT, and minimum spanning tree.

Mon, Dec 8, 2025 4 min read

CMPSC 130A

Algorithm Analysis with Big-O

Common complexities: O(1)O(1), O(log⁡n)O(\log n), O(n)O(n), O(nlog⁡n)O(n \log n), O(n2)O(n^2), O(2n)O(2^n), O(n!)O(n!).

  • Big-O: f(n)=O(g(n))f(n) = O(g(n)) if ∃c>0,n0\exists c > 0, n_0 such that ∀n≥n0:f(n)≤cg(n)\forall n \geq n_0: f(n) \leq c g(n). (f(n)<g(n)f(n) < g(n) upper bound)
  • Big-Omega: f(n)=Ω(g(n))f(n) = \Omega(g(n)) if ∃c>0,n0\exists c > 0, n_0 such that ∀n≥n0:f(n)≥cg(n)\forall n \geq n_0: f(n) \geq c g(n). (f(n)>g(n)f(n) > g(n) lower bound)
  • Big-Theta: f(n)=Θ(g(n))f(n) = \Theta(g(n)) if ∃c1,c2>0,n0\exists c_1, c_2 > 0, n_0 such that ∀n≥n0:c1g(n)≤f(n)≤c2g(n)\forall n \geq n_0: c_1 g(n) \leq f(n) \leq c_2 g(n). (f(n)=g(n)f(n) = g(n) tight bound)

Master Theorem

For T(n)=aT(nb)+f(n)T(n) = a T(\frac{n}{b}) + f(n) where a≥1,b>1a \geq 1, b > 1:

  • If f(n)=O(nlog⁡ba−ϵ)f(n) = O(n^{\log_b a - \epsilon}) for some ϵ>0\epsilon > 0, then T(n)=Θ(nlog⁡ba)T(n) = \Theta(n^{\log_b a}).
  • If f(n)=Θ(nlog⁡balog⁡kn)f(n) = \Theta(n^{\log_b a} \log^k n) for some k≥0k \geq 0, then T(n)=Θ(nlog⁡balog⁡k+1n)T(n) = \Theta(n^{\log_b a} \log^{k+1} n).
  • If f(n)=Ω(nlog⁡ba+ϵ)f(n) = \Omega(n^{\log_b a + \epsilon}) for some ϵ>0\epsilon > 0 and af(nb)≤cf(n)a f(\frac{n}{b}) \leq c f(n) for some c<1c < 1 and sufficiently large nn, then T(n)=Θ(f(n))T(n) = \Theta(f(n)).

Patterns

T(n)=aT(nb)+O(n)T(n) = aT(\frac{n}{b}) + O(n):

T(n)={O(n)if a<bO(nlog⁡n)if a=bO(nlog⁡ba)if a>bT(n) = \begin{cases} O(n) & \text{if } a < b \\ O(n \log n) & \text{if } a = b \\ O(n^{\log_b a}) & \text{if } a > b \end{cases}

Markov Chains

NN states, transition stochastic matrix PP where PijP_{ij} is the probability of transitioning from state ii to state jj.
The sum of each row in PP is 1.

Pr(Xt+1=j∣Xt=i)=PijPr(X_{t+1} = j | X_t = i) = P_{ij}

Let μt\mu_t (vector) be the state distribution at time tt. Then μt=μt−1P=μ0Pt\mu_{t} = \mu_{t-1} P = \mu_0 P^{t}.

As t→∞t \to \infty, PtP^{t} approaches a matrix with identical rows, and μt\mu_t converges to a stationary (limiting) distribution π\pi such that π=πP\pi = \pi P. Pt=[ππ⋮π]P^t = \begin{bmatrix}\pi \\ \pi \\ \vdots \\ \pi\end{bmatrix}.
π=πP\pi = \pi P means π\pi is a left eigenvector of PP with eigenvalue 1. Since PP is stochastic, 1 is the largest eigenvalue.

Ergodic Markov Chains

If ∃t\exists t such that all entries of PtP^{t} are positive, then the Markov chain is ergodic. In the graph, for every pair of states i,ji, j, there is a path from ii to jj of length tt.

Theorem: For a finite ergodic Markov chain, there exists a unique stationary distribution π\pi with all positive entries.

A graph is ergodic iff it is irreducible and aperiodic.

  1. Irreducible (strongly connected): for every pair of states i,ji, j, there is a path from ii to jj.
  2. Aperiodic (non-bipartite): the gcd of the lengths of all cycles in the graph is 1.

If PP is symmetric (Pij=PjiP_{ij} = P_{ji}), then the stationary distribution is uniform: πi=1N\pi_i = \frac{1}{N} for all ii. If ∀i,j:πiPij=πjPji\forall i, j: \pi_i P_{ij} = \pi_j P_{ji}, then π\pi is the stationary distribution (detailed balance condition). PP is reversible with respect to π\pi.

PageRank (Google's Algorithm)

Method to measure the importance of web pages using a Markov chain model of web surfing.

G=(V,E)G = (V, E) is a directed graph where vertices are web pages and edges are hyperlinks.

Ideas:

  1. number of citations -- in-degree. π(y)=in-degree(y)\pi(y) = \text{in-degree}(y)
  2. every page xx has 1 citation to give, then each yy linked from xx gets 1out-degree(x)\frac{1}{\text{out-degree}(x)} citation from xx. π(y)=∑x→y1out-degree(x)\pi(y) = \sum\limits_{x \to y} \frac{1}{\text{out-degree}(x)}
  3. recursive definition: important pages are linked by other important pages. π(y)=∑x→yπ(x)out-degree(x)\pi(y) = \sum\limits_{x \to y} \frac{\pi(x)}{\text{out-degree}(x)}

P(x,y)={1out-degree(x)if x→y0otherwiseP(x, y) = \begin{cases} \frac{1}{\text{out-degree}(x)} & \text{if } x \to y \\ 0 & \text{otherwise} \end{cases}

Lazy random surfer model: with probability d(0.85)d (0.85) follow a link, with probability 1−d1-d jump to a random page. This is ergodic because ∀i,j:Pij≥1−dN>0\forall i, j: P_{ij} \geq \frac{1-d}{N} > 0.

P=dP^+1−dNJP = d \hat{P} + \frac{1-d}{N} J where JJ is the all-ones matrix and P^\hat{P} is the sparse matrix defined above.

Search

Loading search…