CMPSC 130B Algorithms and Data Structures
Algorithm analysis and graph algorithms including maximum flow, linear programming, and NP-completeness.
CMPSC 130B
- Dynamic Programming
- Divide and Conquer
- Fast Fourier Transform (FFT)
- NP-Completeness
Fibonacci Numbers
Matrix form, which can be computed in logarithmic time using fast exponentiation:
is the golden ratio. can also be computed using Binet's formula:
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 items with weights and values , and maximum weight , what is the maximum for each subset of items of size while ensuring ?
- Subset Sum: Given integers and , determine whether there exists a subset of the given set whose elements sum up to .
- Counting Paths in a 2D Array: Given and , count all possible distinct paths from to , where each step is either from to or .
- Longest Common Subsequence: Given strings and , find the length of the longest string that is a subsequence of both and .
- 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 units, given an integer array cuts where cuts 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 where :
- If for some , then .
- If for some , then .
- If for some and for some and sufficiently large , then .
Recurrence Relations
:
NP-Completeness
for some constant .
- 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. .
- 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 is NP-complete needs:
- .
- . (every problem in NP can be reduced to in polynomial time)
- Alternatively, for some known NP-complete problem .
k-SAT to 3-SAT Reduction
Generally, for a clause with literals , we can introduce new variables and create the following clauses:
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
- is an independent set if . No edges between vertices in .
- is a clique if . Every pair of vertices in is connected.
- is a vertex cover if . Every edge in has at least one endpoint in .
They are all NP-complete. Given a graph and an integer :
- Independent Set problem: determine if there exists an independent set of size at least in .
- Clique problem: determine if there exists a clique of size at least in .
- Vertex Cover problem: determine if there exists a vertex cover of size at most in .
3SAT to Independent Set Reduction
Given a 3SAT formula with clauses and variables , we can construct a graph as follows with :
- For each clause , create vertices for the literals .
- Connect vertices corresponding to literals in the same clause with edges. For example, connect , , and with edges.
- Connect vertices corresponding to complementary literals across different clauses. For example, if corresponds to and corresponds to , connect and with an edge.
Independent Set to Clique Reduction
Given a graph , we can construct its complement graph where . An independent set of size in corresponds to a clique of size in because the vertices in the independent set are not connected by edges in , which means they are fully connected by edges in , forming a clique.
Independent Set to Vertex Cover Reduction
Given , construct the input .
is an independent set of size in if and only if is a vertex cover of size .
- Forward: If is an independent set, then no edge has both endpoints in , so every edge has at least one endpoint in . Thus is a vertex cover.
- Backward: If is a vertex cover, then every edge has an endpoint in , so no edge has both endpoints in . Thus is an independent set.
Knapsack (search version)
Given items with weights and values , and maximum weight , determine if there exists a subset of items such that the total weight is at most and the total value is at least .
Subset-Sum Problem: Given integers and a target integer , determine if there exists a subset such that .
3SAT to Subset-Sum Reduction
Overview: numbers with digits in base 10, where the first digits correspond to variables and the next digits correspond to clauses.
- For each variable , create two numbers and :
- Both have a in variable digit .
- has a in clause digit for each clause where appears.
- has a in clause digit for each clause where appears.
- represents ; represents .
- For each clause , create two buffer numbers and , each with a only in clause digit and elsewhere.
- Set the target to have a in each variable digit and a in each clause digit .
- The Subset-Sum problem is to find a subset of that sums to .
- Correctness (forward): A satisfying assignment picks exactly one of or 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 must pick exactly one of per variable (to hit 1 in digit ), 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:
| Number \ Digit | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| 1 | 0 | 0 | 1 | 0 | 1 | |
| 1 | 0 | 0 | 0 | 1 | 0 | |
| 0 | 1 | 0 | 1 | 1 | 0 | |
| 0 | 1 | 0 | 0 | 0 | 1 | |
| 0 | 0 | 1 | 0 | 1 | 1 | |
| 0 | 0 | 1 | 1 | 0 | 0 | |
| 0 | 0 | 0 | 1 | 0 | 0 | |
| 0 | 0 | 0 | 1 | 0 | 0 | |
| 0 | 0 | 0 | 0 | 1 | 0 | |
| 0 | 0 | 0 | 0 | 1 | 0 | |
| 0 | 0 | 0 | 0 | 0 | 1 | |
| 0 | 0 | 0 | 0 | 0 | 1 | |
| 1 | 1 | 1 | 3 | 3 | 3 |
Subset-Sum to Knapsack Reduction
- For each integer , create an item with weight and value .
- Set the maximum weight and the target value .
For a subset of items,
- .
Weight is at most and value is at least if and only if the sum of the selected integers is exactly .
Linear Programming
ILP (Integer Linear Programming) is NP-complete, while LP (Linear Programming) can be solved in polynomial time.
Standard Form
Given:
- Variables:
- Coefficients (Objective Function):
- Constraints: ,
Maximize:
Subject to:
Generic form:
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 -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 , approximately .
It operates on the vertices of the feasible region defined by the constraints. The algorithm proceeds as follows:
- 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.
- 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.
- Termination: The algorithm terminates when no adjacent vertex can improve the objective function, indicating that the optimal solution has been reached.
- Renormalize so that is feasible.
- 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 . To find the tightest upper bound, we should minimize the .
Minimize the upper bound optimal value of the primal:
Subject to:
Reasoning: For any feasible solution to the primal and any feasible solution to the dual, we have:
The dual problem to solve is:
- Weak Duality: For any feasible solution to the primal and any feasible solution to the dual, .
- Strong Duality: If the primal has an optimal solution, and the dual also has an optimal solution, then .
RSA Cryptosystem
- Choose two distinct large prime numbers and .
- Compute and .
- Choose an integer such that and .
- Compute such that .
- The public key is and the private key is .
- To encrypt a message (where ), compute the ciphertext .
- To decrypt a ciphertext , compute the original message .