Skip to content

Manu577228/Advanced-Dp-Algorithms

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

code-with-Bharadwaj

⚑ Advanced DP Optimization Algorithms

A deep-dive into the most powerful Dynamic Programming optimization techniques
As explored on @code-with-Bharadwaj


🎯 What Is This Repository?

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.


πŸ“ Repository Structure

πŸ“¦ DP-Optimization-Algorithms
 ┣ πŸ“‚ BitsetDPAlgorithm
 ┣ πŸ“‚ BrokenProfileDPAlgorithm
 ┣ πŸ“‚ DPSMAWKAlgorithm
 ┣ πŸ“‚ DivideAndConquerDPAlgorithm
 ┣ πŸ“‚ KnuthOptimizationAlgorithm
 ┣ πŸ“‚ LagrangianRelaxationDPAlgorithm
 ┣ πŸ“‚ MongeDPOptimizationAlgorithm
 ┣ πŸ“‚ QuadrangleInequalityDPOptimizationAlgorithm
 ┣ πŸ“‚ SubsetConvolutionAlgorithm
 ┣ πŸ“‚ SumOverSubsetsDPAlgorithm
 β”— πŸ“„ Main.java

🧠 Algorithms Covered

1. πŸ”’ Bitset DP Algorithm

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) where w = 64

2. 🧩 Broken Profile DP Algorithm

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) where m is the smaller grid dimension

3. ⚑ SMAWK Algorithm (DP Variant)

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Β²) to O(n) for eligible problems

4. βž— Divide and Conquer DP

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)

5. πŸͺ’ Knuth's Optimization

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Β²)

6. πŸ” Lagrangian Relaxation (Aliens Trick)

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)

7. πŸ“ Monge DP Optimization

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] for a ≀ b ≀ c ≀ d
  • Complexity gain: Used with SMAWK or Divide & Conquer for O(n log n)

8. πŸ”· Quadrangle Inequality DP Optimization

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) for a ≀ b ≀ c ≀ d
  • Complexity gain: O(nΒ³) β†’ O(nΒ²)

9. πŸŒ€ Subset Convolution Algorithm

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Β²)

10. βž• Sum Over Subsets (SOS) DP

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 of mask
  • Complexity: O(2ⁿ Γ— n) β€” far better than the naive O(3ⁿ)

πŸš€ Getting Started

Prerequisites

  • Java 11 or higher
  • Any IDE (IntelliJ IDEA recommended) or terminal

Clone & Run

# 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

πŸ“Š Complexity Cheat Sheet

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

πŸŽ“ Who Is This For?

  • πŸ† 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

πŸ“Ί YouTube Channel

All these algorithms are explained visually with examples on the channel:

YouTube

@code-with-Bharadwaj
Making hard algorithms easy to understand πŸ”₯


🀝 Contributing

Contributions are welcome! If you find a bug, have a cleaner implementation, or want to add test cases:

  1. Fork this repository
  2. Create your feature branch: git checkout -b feature/improve-knuth
  3. Commit your changes: git commit -m 'Improve Knuth optimization with comments'
  4. Push to the branch: git push origin feature/improve-knuth
  5. Open a Pull Request

⭐ Support

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. πŸš€


Built with ❀️ by @code-with-Bharadwaj Β· Happy Coding! πŸ’»

About

A collection of advanced DP algorithms with efficient implementations and clear explanations.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages