A deep-dive into the most powerful Dynamic Programming optimization techniques
As explored on @code-with-Bharadwaj
This repository is the official code companion to the YouTube series by @code-with-Bharadwaj, covering advanced Dynamic Programming optimization algorithms that are widely used in competitive programming and system design.
These are not your everyday DP problems β these are the techniques that separate good programmers from great ones. Each algorithm here is a tool to reduce time complexity from O(nΒ³) to O(nΒ²) or even O(n log n) when the right conditions are met.
π¦ DP-Optimization-Algorithms
β£ π BitsetDPAlgorithm
β£ π BrokenProfileDPAlgorithm
β£ π DPSMAWKAlgorithm
β£ π DivideAndConquerDPAlgorithm
β£ π KnuthOptimizationAlgorithm
β£ π LagrangianRelaxationDPAlgorithm
β£ π MongeDPOptimizationAlgorithm
β£ π QuadrangleInequalityDPOptimizationAlgorithm
β£ π SubsetConvolutionAlgorithm
β£ π SumOverSubsetsDPAlgorithm
β π Main.java
Leverages bitwise operations to speed up DP transitions. By representing states as bits, operations that normally cost O(n) can run in O(n/64) using 64-bit integers.
- Use case: Subset problems, reachability, counting paths
- Complexity gain:
O(nΒ²/w)wherew = 64
A profile dynamic programming technique used for tiling problems on grids. The "broken profile" moves column-by-column (or row-by-row), encoding the current partial state.
- Use case: Grid tiling, domino problems, Hamiltonian paths on grids
- Complexity:
O(n Γ 2^m)wheremis the smaller grid dimension
The SMAWK algorithm efficiently finds row minima in totally monotone matrices, enabling faster DP transitions.
- Use case: Optimal BST, matrix chain multiplication variants
- Complexity gain: Reduces
O(nΒ²)toO(n)for eligible problems
Applicable when the optimal split point of a DP is monotone. Splits the problem recursively, exploiting the ordering of opt values.
- Condition:
opt(i, j) β€ opt(i, j+1) - Complexity gain:
O(nΒ²)βO(n log n)
A refinement for interval DP problems. When the cost function satisfies both the quadrangle inequality and monotonicity, the optimal split point is bounded.
- Use case: Optimal BST, Matrix Chain Multiplication
- Complexity gain:
O(nΒ³)βO(nΒ²)
Also called the "WQS Binary Search" or Aliens trick. Converts a DP with a constraint "use exactly k items" into a penalized version, binary searching on the penalty.
- Use case: When a constraint like "exactly k operations" makes DP slow
- Complexity gain:
O(n Γ k)βO(n log n)
Based on the Monge matrix property (a generalization of concavity for 2D cost arrays). Allows efficient computation of DP transitions.
- Condition:
C[a][c] + C[b][d] β€ C[a][d] + C[b][c]fora β€ b β€ c β€ d - Complexity gain: Used with SMAWK or Divide & Conquer for
O(n log n)
The quadrangle inequality (or concave/convex SMAWK condition) enables monotone optimization of the optimal decision in interval DP.
- Condition:
w(a,c) + w(b,d) β€ w(a,d) + w(b,c)fora β€ b β€ c β€ d - Complexity gain:
O(nΒ³)βO(nΒ²)
Computes the convolution over subsets β i.e., h[S] = Ξ£ f[T] Γ g[S\T] for all T β S. Used in counting problems on subsets.
- Use case: Counting colorings, matchings, covers over subsets
- Complexity:
O(2βΏ Γ nΒ²)
Efficiently computes sum of values over all subsets of every mask. A fundamental building block for bitmask DP.
- Formula:
dp[mask] = Ξ£ a[submask]for all submasks ofmask - Complexity:
O(2βΏ Γ n)β far better than the naiveO(3βΏ)
- Java 11 or higher
- Any IDE (IntelliJ IDEA recommended) or terminal
# Clone the repository
git clone https://github.com/YOUR_USERNAME/YOUR_REPO_NAME.git
# Navigate to the project
cd YOUR_REPO_NAME
# Compile
javac Main.java
# Run
java Main| Algorithm | Before Optimization | After Optimization | Key Condition |
|---|---|---|---|
| Divide & Conquer DP | O(nΒ²) | O(n log n) | Monotone opt |
| Knuth's Optimization | O(nΒ³) | O(nΒ²) | Quadrangle inequality |
| Lagrangian Relaxation | O(nΒ·k) | O(n log n) | Convexity in k |
| SMAWK | O(nΒ²) | O(n) | Totally monotone matrix |
| SOS DP | O(3βΏ) | O(2βΏ Β· n) | All subsets |
| Subset Convolution | O(4βΏ) | O(2βΏ Β· nΒ²) | Ranked zeta/MΓΆbius |
| Bitset DP | O(nΒ²) | O(nΒ²/64) | Bitwise parallelism |
- π Competitive programmers aiming for Codeforces Div. 1 / ICPC level
- πΌ Interview preppers targeting FAANG+ companies
- π― CS students studying algorithm design
- πΊ Viewers of @code-with-Bharadwaj who want the code alongside the videos
All these algorithms are explained visually with examples on the channel:
Contributions are welcome! If you find a bug, have a cleaner implementation, or want to add test cases:
- Fork this repository
- Create your feature branch:
git checkout -b feature/improve-knuth - Commit your changes:
git commit -m 'Improve Knuth optimization with comments' - Push to the branch:
git push origin feature/improve-knuth - Open a Pull Request
If this repository helped you β star it and share it with your fellow competitive programmers!
And don't forget to subscribe to @code-with-Bharadwaj for more in-depth algorithm content. π