What is Dynamic Programming - Google Docs
What is Dynamic Programming - Google Docs
W
● Dynamic Programmingis a method for solving problemsbybreaking them into
overlapping subproblems, solving each subproblem justonce, and storing its result to avoid
recomputation.
Key Concepts of DP:
1. Optimal Substructure:
○ A problem hasoptimal substructureif its optimalsolution can be built from optimal
solutions of its subproblems.
2. Overlapping Subproblems:
○ A problem exhibitsoverlapping subproblemsif thesame subproblem is solved multiple
times.
General method:-
Problem
|
V
Break into Subproblems (Overlapping)
|
V
Solve Subproblem and Store in Table
|
V
Combine Subproblem Solutions
|
V
Optimal Solution
✅ Advantage❌ Disadvantage
vely computes all-pairs shortest paths even withnegicient for very large sparse gr
weights
hat are Optimal Binary Search Trees?
W
AnOptimal Binary Search Treeminimizes thesearch,insert, and delete costsin a binary
search tree (BST) by ensuring that the tree is structured in such a way that these operations are as
fast and efficient as possible.
● In a normal binary search tree, the tree may becomeunbalanced, leading to inefficient
operations (up to O(V)O(V)O(V) time complexity).
● In anOptimal BST, the tree isbalanced and well-structured,reducing the height and
ensuring better average search times.
Problem Statement
Given:
● A set ofkeysK1,K2,...,KnK_1, K_2, ..., K_nK1,K2,...,Kn.
● For each key KiK_iKi:
○ pip_ipi: the probability of accessing the key KiK_iKi.
○ qiq_iqi: the probability of accessing a non-existent key in between KiK_iKiand
Ki+1K_{i+1}Ki+1(representing gaps).
Objective:Construct a binary search tree with a structurethat minimizes theexpected search
📘
✅
cost.
Steps to Construct an Optimal BST
Step 1: Define the Probabilities
For a set ofn keysK1,K2,...,KnK_1, K_2, ..., K_nK1,K2,...,Kn:
● pip_ipi: Probability of accessing key KiK_iKi.
✅
Step 3: Initialize the Cost Matrices
1. For a single key KiK_iKi, the cost e[i][i]e[i][i]e[i][i] is just the probability of accessing KiK_iKi:
e[i][i]=pi+qi−1e[i][i] = p_i + q_{i-1}e[i][i]=pi+qi−1
✅
Step 4: Use Dynamic Programming to Build Subproblems
● Build the matrix e[i][j]e[i][j]e[i][j] iteratively from smaller subtrees to larger ones (bottom-up
approach).
● For every possible subtree size j−i+1j - i + 1j−i+1, try every possible root rrr and compute the
cost using the recursive formula.
✅
Step 5: Reconstruct the Optimal Tree
Once the matrix e[i][j]e[i][j]e[i][j] is filled, you can backtrack to reconstruct the structure of the
optimal tree.
🔹
Example
Let’s consider an example with3 keys:
Given Probabilities:
● Keys: K1,K2,K3K_1, K_2, K_3K1,K2,K3
● p1=0.1p_1 = 0.1p1=0.1, p2=0.5p_2 = 0.5p2=0.5, p3=0.2p_3 = 0.2p3=0.2
● Gaps: q0=0.05q_0 = 0.05q0=0.05, q1=0.1q_1 = 0.1q1=0.1
bjective:
O
Construct a tree that minimizes the search cost using the dynamic programming approach.
1. Initialize a cost matrix e[i][j]e[i][j]e[i][j].
2. Use the recurrence formula to fill in the costs for all nodes by trying different roots.
3. Reconstruct the tree according to the optimal choices for the roots.
nt Search Operations
📌 ● For strings of length mmm and nnn, the time complexity is O(m×n)O(m \times n)O(m×n).
Applications of String Editing
● Spell Checking
● DNA sequence analysis
● Text comparison and version control
● Data cleaning and transformation