Skip to content
Home ยป Randomization and dynamic programming

Randomization and dynamic programming


๐Ÿ“˜ 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)
]


  • 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

FeatureRandomizationDynamic Programming
NatureProbabilisticDeterministic
DecisionRandomOptimal
GuaranteeProbabilisticGuaranteed
UsageAvoid worst caseOptimization 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.