Organized by Task - All Formulas, Decision Rules, and Complexities
Algorithms for finding shortest paths from source to other vertices
For unweighted graphs
Why this rule? Queue ensures FIFO → explores vertices level by level → guarantees shortest path in unweighted graphs
| Variable | Meaning |
|---|---|
| V | Number of vertices |
| E | Number of edges |
| d[v] | Distance (number of edges) from source to v |
| Q | Queue of vertices to visit |
For weighted graphs with non-negative weights
Why this rule? Greedy choice of minimum distance is safe because all weights are non-negative → cannot find shorter path later
| Variable | Meaning |
|---|---|
| d[v] | Current shortest distance estimate from source to v |
| π[v] | Predecessor of v in shortest path tree |
| w(u,v) | Weight of edge from u to v (must be ≥ 0) |
| Q | Min-priority queue keyed by d[v] |
Algorithms for exploring graphs and establishing orderings
For deep exploration and cycle detection
Why this rule? Stack/recursion ensures LIFO → explores as deep as possible before backtracking
For ordering dependent tasks (DAGs only)
Why this rule? Vertices with later finish times have no outgoing edges to vertices with earlier finish times → correct topological order
For finding maximal strongly connected subgraphs
Algorithms for connecting all vertices with minimum total edge weight
Why this rule? Minimum weight edge that doesn't create cycle is always safe to add (Cut Property)
Why this rule? Minimum weight edge crossing cut between MST and non-MST vertices is always safe (Cut Property)
| Variable | Meaning |
|---|---|
| key[v] | Minimum weight of edge connecting v to MST |
| π[v] | Parent of v in MST |
| Q | Min-priority queue of vertices not yet in MST |
Problems where local optimal choices lead to global optimum
Maximize number of non-overlapping activities
Why this rule? Activity with earliest finish time leaves maximum room for remaining activities
| Variable | Meaning |
|---|---|
| s[i] | Start time of activity i |
| f[i] | Finish time of activity i |
| k | Index of last selected activity |
Optimal prefix-free binary encoding
Why this rule? Merging lowest frequency nodes minimizes total cost Σ f[c] · depth[c]
Problems with overlapping subproblems and optimal substructure
| Approach | Method | When to Use |
|---|---|---|
| Top-Down (Memoization) | Recursive with cache | When not all subproblems needed; easier to write |
| Bottom-Up (Tabulation) | Iterative table filling | When all subproblems needed; better performance |
Maximize revenue from cutting rod
Why this rule? Try all possible first cuts, choose best. Remaining piece solved optimally by subproblem.
| Variable | Meaning |
|---|---|
| r[i] | Maximum revenue for rod of length i |
| p[i] | Price for rod of length i (no cuts) |
Minimize scalar multiplications
Why this rule? Try all possible split points, choose minimum cost. Parenthesization matters!
| Variable | Meaning |
|---|---|
| m[i,j] | Min scalar multiplications for A[i..j] |
| p[i] | Dimensions: A[i] is p[i-1] × p[i] |
| s[i,j] | Optimal split index k for A[i..j] |
Find longest subsequence common to two sequences
Why this rule? If characters match, must include. If not, one must be excluded - try both, take best.
| Variable | Meaning |
|---|---|
| LCS[i,j] | Length of LCS of X[1..i] and Y[1..j] |
| m | Length of sequence X |
| n | Length of sequence Y |
Maximizing flow through a network with capacity constraints
Iteratively find augmenting paths
Why this rule? Augmenting path always improves flow. Stop when no path exists → max flow reached.
Ford-Fulkerson with BFS for paths
Why this rule? BFS guarantees shortest path → ensures polynomial time O(VE²), not pseudo-polynomial
Reduce to max flow problem
Breaking problems into subproblems, solving recursively, and combining results
For analyzing divide-and-conquer recurrences
| Recurrence | a | b | n^(log_b a) | Case | Solution |
|---|---|---|---|---|---|
| T(n) = 2T(n/2) + n | 2 | 2 | n | 2 (k=0) | Θ(n log n) |
| T(n) = 4T(n/2) + n | 4 | 2 | n² | 1 | Θ(n²) |
| T(n) = T(n/2) + 1 | 1 | 2 | 1 | 2 (k=0) | Θ(log n) |
| T(n) = 7T(n/2) + n² | 7 | 2 | n^2.81 | 1 | Θ(n^2.81) |
Faster matrix multiplication using divide-and-conquer
| Task | Algorithm | Time | Space | Key Decision Rule |
|---|---|---|---|---|
| Shortest Path | BFS | O(V+E) | O(V) | FIFO queue → level-by-level |
| Dijkstra's | O((V+E) log V) | O(V) | Min distance vertex + relax | |
| Traversal/Ordering | DFS | O(V+E) | O(V) | LIFO stack → depth-first |
| Topological Sort | O(V+E) | O(V) | DFS finish times (reverse) | |
| SCC (Kosaraju) | O(V+E) | O(V) | 2 DFS: G then G^T | |
| MST | Kruskal's | O(E log E) | O(V) | Min edge without cycle |
| Prim's | O((V+E) log V) | O(V) | Min edge to new vertex | |
| Greedy Opt | Activity Selection | O(n log n) | O(1) | Earliest finish time |
| Huffman | O(n log n) | O(n) | Merge two min frequencies | |
| DP Opt | Rod Cutting | O(n²) | O(n) | max over all first cuts |
| Matrix Chain | O(n³) | O(n²) | min over all split points | |
| LCS | O(mn) | O(mn) | Match? +1 : max of exclude | |
| Flow | Ford-Fulkerson | O(E·|f*|) | O(V+E) | Any augmenting path |
| Edmonds-Karp | O(VE²) | O(V+E) | Shortest augmenting path (BFS) | |
| Divide & Conquer | Master Theorem | - | - | Compare f(n) vs n^(log_b a) |
| Strassen's | O(n^2.81) | O(n²) | 7 subproblems instead of 8 |