Sandomhet

CMPSC 130B Algorithms and Data Structures

Algorithm analysis and graph algorithms including maximum flow, linear programming, and NP-completeness.

Wed, Apr 1, 2026 11 min read

CMPSC 130B

Fibonacci Numbers

F(n)={0if n=01if n=1F(n−1)+F(n−2)if n>1F(n) = \begin{cases} 0 & \text{if } n = 0 \\ 1 & \text{if } n = 1 \\ F(n-1) + F(n-2) & \text{if } n > 1 \end{cases}

Matrix form, which can be computed in logarithmic time using fast exponentiation:

(FnFn−1)=(1110)(Fn−1Fn−2)=(1110)n−1(F1F0)\begin{pmatrix}F_{n} \\ F_{n-1} \end{pmatrix} = \begin{pmatrix}1 & 1 \\ 1 & 0 \end{pmatrix} \begin{pmatrix}F_{n-1} \\ F_{n-2} \end{pmatrix} = \begin{pmatrix}1 & 1 \\ 1 & 0 \end{pmatrix}^{n-1} \begin{pmatrix}F_{1} \\ F_{0} \end{pmatrix}

ϕ=1+52≈1.618\phi = \frac{1 + \sqrt{5}}{2} \approx 1.618 is the golden ratio. FnF_n can also be computed using Binet's formula:

F(n)=ϕn−(1−ϕ)n5F(n) = \frac{\phi^n - (1 - \phi)^n}{\sqrt{5}}

Dynamic Programming

Classic DP problems include:

Click here to see
  • Longest Increasing Subsequence (LIS): Given an array of integers, find the length of the longest subsequence that is strictly increasing.
  • Knapsack Problem: Given a set of items with weights and values, determine the maximum value that can be obtained with a given weight capacity.
  • Edit Distance: Given two strings, find the minimum number of operations required to convert one string into the other, where allowed operations are insertion, deletion, or substitution of a single character.
  • Matrix Chain Multiplication: Given a sequence of matrices, find the most efficient way to multiply them together.
  • 0-1 Knapsack: Given NN items with weights wiw_i and values viv_i, and maximum weight WW, what is the maximum ∑i=1kvi\sum_{i=1}^{k} v_i for each subset of items of size kk while ensuring ∑i=1kwi≤W\sum_{i=1}^{k} w_i \leq W?
  • Subset Sum: Given NN integers and TT, determine whether there exists a subset of the given set whose elements sum up to TT.
  • Counting Paths in a 2D Array: Given NN and MM, count all possible distinct paths from (1,1)(1, 1) to (N,M)(N, M), where each step is either from (i,j)(i, j) to (i+1,j)(i+1, j) or (i,j+1)(i, j+1).
  • Longest Common Subsequence: Given strings ss and tt, find the length of the longest string that is a subsequence of both ss and tt.
  • Longest Path in a Directed Acyclic Graph (DAG): Finding the longest path in Directed Acyclic Graph (DAG).
  • Longest Palindromic Subsequence: Finding the Longest Palindromic Subsequence (LPS) of a given string.
  • Rod Cutting: Given a rod of length nn units, given an integer array cuts where cuts[i][i] denotes a position you should perform a cut at. The cost of one cut is the length of the rod to be cut. What is the minimum total cost of the cuts?

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)).

Recurrence Relations

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}

NP-Completeness

poly(n)=O(nc)poly(n) = O(n^c) for some constant cc.

  • NP: Class of decision (search) problems for which a given solution can be verified in polynomial time.
  • P: Class of decision problems that can be solved in polynomial time. P⊆NPP \subseteq NP.
  • NP-Complete: it is in NP and every problem in NP can be reduced to it in polynomial time. (e.g., Traveling Salesman, Knapsack, and SAT)
  • NP-Hard: every problem in NP can be reduced to it in polynomial time, but it is not necessarily in NP itself. (e.g., Halting Problem and the Post Correspondence Problem)
  • P vs NP: The question of whether every problem for which a solution can be verified in polynomial time can also be solved in polynomial time. It is widely believed that P ≠ NP, but this has not been proven.

Every planer graph can be colored with at most 4 colors (Four Color Theorem). However, determining if a general graph can be colored with 3 colors is NP-complete.

To prove a problem AA is NP-complete needs:

  1. A∈NPA \in NP.
  2. ∀B∈NP,B→A\forall B \in NP, B \to A. (every problem in NP can be reduced to AA in polynomial time)
  3. Alternatively, C→AC \to A for some known NP-complete problem CC.

k-SAT to 3-SAT Reduction

Generally, for a clause with kk literals C=(x1∨x2∨…∨xk)C = (x_1 \lor x_2 \lor \ldots \lor x_k), we can introduce k−3k-3 new variables y1,y2,…,yk−3y_1, y_2, \ldots, y_{k-3} and create the following k−2k-2 clauses:

C′=(x1∨x2∨y1)∧(yˉ1∨x3∨y2)∧(yˉ2∨x4∨y3)∧…∧(yˉk−4∨xk−2∨yk−3)∧(yˉk−3∨xk−1∨xk)C' = (x_1 \lor x_2 \lor y_1) \land (\bar y_1 \lor x_3 \lor y_2) \land (\bar y_2 \lor x_4 \lor y_3) \land \ldots \land (\bar y_{k-4} \lor x_{k-2} \lor y_{k-3}) \land (\bar y_{k-3} \lor x_{k-1} \lor x_k) C⇔C′C \Leftrightarrow C'

This construction ensures that the original clause is satisfied if and only if at least one of the new clauses is satisfied, thus preserving the satisfiability of the original formula while reducing it to 3-SAT.

Independent Set, Clique, and Vertex Cover

G=(V,E)G = (V, E)

  • S⊆VS \subseteq V is an independent set if ∀u,v∈S,(u,v)∉E\forall u, v \in S, (u, v) \notin E. No edges between vertices in SS.
  • S⊆VS \subseteq V is a clique if ∀u,v∈S,(u,v)∈E\forall u, v \in S, (u, v) \in E. Every pair of vertices in SS is connected.
  • S⊆VS \subseteq V is a vertex cover if ∀(u,v)∈E,u∈S∨v∈S\forall (u, v) \in E, u \in S \lor v \in S. Every edge in EE has at least one endpoint in SS.

They are all NP-complete. Given a graph GG and an integer kk:

  • Independent Set problem: determine if there exists an independent set of size at least kk in GG.
  • Clique problem: determine if there exists a clique of size at least kk in GG.
  • Vertex Cover problem: determine if there exists a vertex cover of size at most kk in GG.

3SAT to Independent Set Reduction

Given a 3SAT formula ff with clauses C1,C2,…,CmC_1, C_2, \ldots, C_m and variables x1,x2,…,xnx_1, x_2, \ldots, x_n, we can construct a graph GG as follows with k=mk = m:

  1. For each clause Ci=(x1∨x2∨x3)C_i = (x_1 \lor x_2 \lor x_3), create vertices vi1,vi2,vi3v_{i1}, v_{i2}, v_{i3} for the literals x1,x2,x3x_1, x_2, x_3.
  2. Connect vertices corresponding to literals in the same clause with edges. For example, connect vi1v_{i1}, vi2v_{i2}, and vi3v_{i3} with edges.
  3. Connect vertices corresponding to complementary literals across different clauses. For example, if vi1v_{i1} corresponds to x1x_1 and vj2v_{j2} corresponds to xˉ1\bar{x}_1, connect vi1v_{i1} and vj2v_{j2} with an edge.

Independent Set to Clique Reduction

Given a graph G=(V,E)G = (V, E), we can construct its complement graph Gˉ=(V,Eˉ)\bar{G} = (V, \bar{E}) where Eˉ={(u,v)∣(u,v)∉E}\bar{E} = \{(u, v) | (u, v) \notin E\}. An independent set of size kk in GG corresponds to a clique of size kk in Gˉ\bar{G} because the vertices in the independent set are not connected by edges in GG, which means they are fully connected by edges in Gˉ\bar{G}, forming a clique.

Independent Set to Vertex Cover Reduction

Given (G,k)(G, k), construct the input (G,n−k)(G, n - k).
S⊆VS \subseteq V is an independent set of size ≥k\geq k in GG if and only if V∖SV \setminus S is a vertex cover of size ≤n−k\leq n - k.

  • Forward: If SS is an independent set, then no edge has both endpoints in SS, so every edge (u,v)∈E(u, v) \in E has at least one endpoint in V∖SV \setminus S. Thus V∖SV \setminus S is a vertex cover.
  • Backward: If C=V∖SC = V \setminus S is a vertex cover, then every edge has an endpoint in CC, so no edge has both endpoints in S=V∖CS = V \setminus C. Thus SS is an independent set.

Knapsack (search version)

Given NN items with weights wiw_i and values viv_i, and maximum weight WW, determine if there exists a subset of items such that the total weight is at most WW and the total value is at least VV.

Subset-Sum Problem: Given integers A={a1,a2,…,an}A = \{a_1, a_2, \ldots, a_n\} and a target integer TT, determine if there exists a subset SS such that ∑ai∈Sai=T\sum\limits_{a_i \in S} a_i = T.

3SAT to Subset-Sum Reduction

Overview: 2n+2m2n+2m numbers with n+mn+m digits in base 10, where the first nn digits correspond to variables and the next mm digits correspond to clauses.

  1. For each variable xix_i, create two numbers viv_i and vi′v_i':
    • Both have a 11 in variable digit ii.
    • viv_i has a 11 in clause digit n+jn+j for each clause CjC_j where xix_i appears.
    • vi′v_i' has a 11 in clause digit n+jn+j for each clause CjC_j where xˉi\bar x_i appears.
    • viv_i represents xi=truex_i = \text{true}; vi′v_i' represents xi=falsex_i = \text{false}.
  2. For each clause CjC_j, create two buffer numbers sjs_j and sj′s_j', each with a 11 only in clause digit n+jn+j and 00 elsewhere.
  3. Set the target TT to have a 11 in each variable digit 1,…,n1, \ldots, n and a 33 in each clause digit n+1,…,n+mn+1, \ldots, n+m.
  4. The Subset-Sum problem is to find a subset of {vi,vi′}∪{sj,sj′}\{v_i, v_i'\} \cup \{s_j, s_j'\} that sums to TT.
    • Correctness (forward): A satisfying assignment picks exactly one of viv_i or vi′v_i' per variable, hitting 1 in every variable digit. Each satisfied clause contributes at least 1 to its clause digit; buffer numbers bring it up to exactly 3.
    • Correctness (backward): Any subset summing to TT must pick exactly one of {vi,vi′}\{v_i, v_i'\} per variable (to hit 1 in digit ii), defining a truth assignment. Each clause digit must reach 3 with at most 2 from buffer, so at least one chosen literal satisfies every clause.
    • Base 10 is used and no digit exceeds 5, so there is no carrying between digits.

Example: f=(x1∨x2∨x3ˉ)∧(x1ˉ∨x2∨x3)∧(x1∨x2ˉ∨x3)f = (x_1 \lor x_2 \lor \bar{x_3}) \land (\bar{x_1} \lor x_2 \lor x_3) \land (x_1 \lor \bar{x_2} \lor x_3)

Number \ Digit123456
v1v_1100101
v1′v_1'100010
v2v_2010110
v2′v_2'010001
v3v_3001011
v3′v_3'001100
s1s_1000100
s1′s_1'000100
s2s_2000010
s2′s_2'000010
s3s_3000001
s3′s_3'000001
TT111333

Subset-Sum to Knapsack Reduction

  • For each integer aia_i, create an item with weight wi=aiw_i = a_i and value vi=aiv_i = a_i.
  • Set the maximum weight W=TW = T and the target value V=TV = T.

For a subset SS of items,

  • ∑i∈Swi=∑i∈Sai≤W=T\sum\limits_{i \in S} w_i = \sum\limits_{i \in S} a_i \leq W = T
  • ∑i∈Svi=∑i∈Sai≥V=T\sum\limits_{i \in S} v_i = \sum\limits_{i \in S} a_i \geq V = T.

Weight is at most TT and value is at least TT if and only if the sum of the selected integers is exactly TT.

Linear Programming

ILP (Integer Linear Programming) is NP-complete, while LP (Linear Programming) can be solved in polynomial time.

Standard Form

Given:

  • Variables: x∈Rn=(x1,x2,…,xn)x \in \mathbb{R}^n = (x_1, x_2, \ldots, x_n)
  • Coefficients (Objective Function): c∈Rn=(c1,c2,…,cn)c \in \mathbb{R}^n = (c_1, c_2, \ldots, c_n)
  • Constraints: A∈Rm×n=(aij)A \in \mathbb{R}^{m \times n} = (a_{ij}), b∈Rm=(b1,b2,…,bm)b \in \mathbb{R}^m = (b_1, b_2, \ldots, b_m)

Maximize:

c1x1+c2x2+…+cnxnc_1 x_1 + c_2 x_2 + \ldots + c_n x_n

Subject to:

{a11x1+a12x2+…+a1nxn≤b1a21x1+a22x2+…+a2nxn≤b2⋮am1x1+am2x2+…+amnxn≤bmx1≥0,x2≥0,…,xn≥0\begin{cases} a_{11} x_1 + a_{12} x_2 + \ldots + a_{1n} x_n \leq b_1 \\ a_{21} x_1 + a_{22} x_2 + \ldots + a_{2n} x_n \leq b_2 \\ \vdots \\ a_{m1} x_1 + a_{m2} x_2 + \ldots + a_{mn} x_n \leq b_m \\ x_1 \geq 0, x_2 \geq 0, \ldots, x_n \geq 0 \end{cases}

Generic form:

maximizecTxsubject toAx≤bx≥0\begin{aligned} \text{maximize} \quad & c^T x \\ \text{subject to} \quad & Ax \leq b \\ & x \geq 0 \end{aligned}

Algorithms

  • Simplex Algorithm: A popular method for solving linear programming problems. It is efficient in practice, but has exponential worst-case time complexity.
  • Interior Point Methods: These algorithms approach the optimal solution from the interior of the feasible region and have polynomial time complexity in the worst case.
  • Ellipsoid Algorithm: A theoretical algorithm that can solve linear programming problems in polynomial time, but is not efficient in practice.

Simplex Algorithm

Feasible region: The set of all points that satisfy all constraints. It's the intersection of half-spaces. It is a convex polyhedron in nn-dimensional space.
Vertices (or extreme points): The corners of the feasible region. The optimal solution, if it exists, will occur at one of these vertices. The number of vertices is at most (n+mn)\binom{n+m}{n}, approximately O((n+m)n)O((n+m)^n).

It operates on the vertices of the feasible region defined by the constraints. The algorithm proceeds as follows:

  1. Initialization: Start at a vertex of the feasible region, which can be found using methods like the Big M method or the Two-Phase method.
  2. Iterative Improvement: At each iteration, the algorithm checks the adjacent vertices of the current vertex to find one that improves the objective function. If such a vertex is found, the algorithm moves to that vertex and repeats the process.
  3. Termination: The algorithm terminates when no adjacent vertex can improve the objective function, indicating that the optimal solution has been reached.

  1. Renormalize so that xˉ=0⃗\bar{x} = \vec{0} is feasible.
  2. If exists better neighbor, move to it and repeat until no better neighbor exists.

LP Duality

The upper bound optimal value of the primal problem is cTx≤Vc^T x \leq V. To find the tightest upper bound, we should minimize the V≤bTyV \leq b^T y.

Minimize the upper bound optimal value of the primal:

b1y1+b2y2+…+bmymb_1 y_1 + b_2 y_2 + \ldots + b_m y_m

Subject to:

{a11y1+a21y2+…+am1ym≥c1a12y1+a22y2+…+am2ym≥c2⋮a1ny1+a2ny2+…+amnym≥cny1≥0,y2≥0,…,ym≥0\begin{cases} a_{11} y_1 + a_{21} y_2 + \ldots + a_{m1} y_m \geq c_1 \\ a_{12} y_1 + a_{22} y_2 + \ldots + a_{m2} y_m \geq c_2 \\ \vdots \\ a_{1n} y_1 + a_{2n} y_2 + \ldots + a_{mn} y_m \geq c_n \\ y_1 \geq 0, y_2 \geq 0, \ldots, y_m \geq 0 \end{cases}

Reasoning: For any feasible solution xx to the primal and any feasible solution yy to the dual, we have: cTx≤yTAx≤yTbc^T x \leq y^T A x \leq y^T b

The dual problem to solve yy is:

minimizebTysubject toATy≥cy≥0\begin{aligned} \text{minimize} \quad & b^T y \\ \text{subject to} \quad & A^T y \geq c \\ & y \geq 0 \end{aligned}
  • Weak Duality: For any feasible solution xx to the primal and any feasible solution yy to the dual, cTx≤bTyc^T x \leq b^T y.
  • Strong Duality: If the primal has an optimal solution, and the dual also has an optimal solution, then cTx∗=bTy∗c^T x^* = b^T y^*.

RSA Cryptosystem

  1. Choose two distinct large prime numbers pp and qq.
  2. Compute N=p⋅qN = p \cdot q and ϕ(N)=(p−1)(q−1)\phi(N) = (p-1)(q-1).
  3. Choose an integer ee such that 1<e<ϕ(N)1 < e < \phi(N) and gcd⁡(e,ϕ(N))=1\gcd(e, \phi(N)) = 1.
  4. Compute dd such that d≡e−1mod  ϕ(N)d \equiv e^{-1} \mod \phi(N).
  5. The public key is (N,e)(N, e) and the private key is (N,d)(N, d).
  • To encrypt a message mm (where 0≤m<N0 \leq m < N), compute the ciphertext c=memod  Nc = m^e \mod N.
  • To decrypt a ciphertext cc, compute the original message m=cdmod  Nm = c^d \mod N.
Search

Loading search…