๐ Randomization and Dynamic Programming
(Basic Algorithm Design Techniques)
PART A: RANDOMIZATION
1๏ธโฃ Introduction to Randomization
A randomized algorithm uses random numbers at some point during its execution to make decisions.
The behavior of such algorithms is not deterministic, meaning the same input may produce different execution paths.
Randomization helps avoid worst-case scenarios and improves average performance.
2๏ธโฃ Why Use Randomized Algorithms?
- Avoids worst-case inputs
- Simple and elegant solutions
- Good average-case performance
- Useful when input distribution is unknown
3๏ธโฃ Types of Randomized Algorithms
๐น 1. Las Vegas Algorithms
- Always produce correct result
- Running time is random
๐ Example:
- Randomized Quick Sort
๐น 2. Monte Carlo Algorithms
- Fixed running time
- Result may be incorrect with small probability
๐ Example:
- Randomized primality testing
4๏ธโฃ Examples of Randomized Algorithms
๐น Randomized Quick Sort
- Randomly selects pivot
- Avoids worst-case input
Expected Time Complexity:
[
O(n \log n)
]
๐น Randomized Binary Search
- Randomly selects mid
- Useful for large datasets
5๏ธโฃ Advantages of Randomization
โ Avoids worst-case scenarios
โ Simple implementation
โ Efficient average-case performance
6๏ธโฃ Disadvantages of Randomization
โ Non-deterministic behavior
โ Difficult to debug
โ Requires random number generation
PART B: DYNAMIC PROGRAMMING
7๏ธโฃ Introduction to Dynamic Programming (DP)
Dynamic Programming is an algorithm design technique used to solve problems by:
- Breaking them into overlapping sub-problems
- Solving each sub-problem only once
- Storing results to avoid recomputation
DP is an optimization of recursion.
8๏ธโฃ Characteristics of Dynamic Programming
๐น 1. Optimal Substructure
Optimal solution contains optimal solutions of sub-problems.
๐น 2. Overlapping Sub-Problems
Same sub-problems are solved multiple times in recursion.
9๏ธโฃ Approaches in Dynamic Programming
๐น 1. Top-Down (Memoization)
- Uses recursion
- Stores results in a table
๐น 2. Bottom-Up (Tabulation)
- Uses iteration
- Builds solution from smallest sub-problem
๐ Examples of Dynamic Programming
๐น Fibonacci Series (DP)
dp[0] = 0
dp[1] = 1
dp[n] = dp[n-1] + dp[n-2]
Time Complexity: O(n)
Space Complexity: O(n)
๐น 0/1 Knapsack Problem
- Choose items to maximize profit
- Weight constraint
Time Complexity: O(nW)
๐น Longest Common Subsequence (LCS)
- Find longest subsequence common to two strings
Time Complexity: O(mn)
1๏ธโฃ1๏ธโฃ Advantages of Dynamic Programming
โ Guarantees optimal solution
โ Efficient for complex problems
โ Avoids repeated computation
1๏ธโฃ2๏ธโฃ Disadvantages of Dynamic Programming
โ Higher space usage
โ Difficult to design
โ Problem-specific
1๏ธโฃ3๏ธโฃ Randomization vs Dynamic Programming
| Feature | Randomization | Dynamic Programming |
|---|---|---|
| Nature | Probabilistic | Deterministic |
| Decision | Random | Optimal |
| Guarantee | Probabilistic | Guaranteed |
| Usage | Avoid worst case | Optimization problems |
๐ Conclusion
Randomization and Dynamic Programming are powerful algorithm design techniques used for different types of problems.
Randomization improves average performance, while Dynamic Programming guarantees optimal solutions.

