Unit Iv
Unit Iv
Answer: overlapping
2. In Dynamic Programming, the sub-problems are solved __________ and their results
are stored to avoid redundant computation.
Answer: once
3. The two main approaches used in Dynamic Programming are __________ and
__________.
Answer: memorization
Answer: tabulation
6. The problem of finding the shortest path in a graph can be solved using __________
algorithm, which is based on Dynamic Programming.
Answer: Floyd-Warshall
Answer: overlapping
12. The __________ algorithm uses Dynamic Programming to find the longest common
subsequence between two strings.
Answer: Dynamic
A) Greedy structure
B) Divide and conquer only
C) Optimal substructure and overlapping sub-problems
D) Random inputs
A) Binary Search
B) Merge Sort
C) Knapsack Problem
D) Quick Sort
A) Tabulation
B) Recursion
C) Memoization
D) Iteration
A) Backtracking
B) Tabulation
C) Memoization
D) Greedy approach
A) Prim's Algorithm
B) Dijkstra's Algorithm
C) Floyd-Warshall Algorithm
D) Kruskal's Algorithm
A) Brute-force
B) Memoization
C) Tabulation
D) Recursion
A) O(n log n)
B) O(n^2)
C) O(n * W), where n = number of items, W = capacity
D) O(W^n)
10. Which of the following problems does NOT typically require Dynamic
Programming?
A) Merge Sort
B) Binary Search
C) Fibonacci Sequence
D) Quick Sort
A) Bellman-Ford
B) Kadane’s Algorithm
C) Patience Sorting
D) LIS using O(n²) DP solution
A) Depth-First manner
B) Bottom-Up manner
C) Top-Down manner
D) Randomized order
A) O(1)
B) O(n)
C) O(log n)
D) O(n²)
19. Which of the following is solved using Dynamic Programming and not a
greedy algorithm?
A) Activity Selection
B) Fractional Knapsack
C) 0/1 Knapsack
D) Huffman Coding
A) Bellman-Ford Algorithm
B) Matrix Chain Multiplication
C) Longest Common Substring
D) Edit Distance
Dynamic Programming (DP) is a powerful algorithmic technique used for solving complex problems
by breaking them down into simpler sub-problems, solving each of those only once, and storing
their solutions – typically using a table (array or matrix) to avoid re-computation. It is particularly
useful when a problem has overlapping sub-problems and optimal substructure.
The term “dynamic programming” was coined by Richard Bellman in the 1950s. He developed it as a
method for solving multistage decision problems efficiently.
Key Concepts of Dynamic Programming:
1. Overlapping Sub-problems:
o Problems can be broken down into sub-problems that are reused several times.
o Example: In computing Fibonacci numbers, F(5) requires F(4) and F(3), and F(4)
again requires F(3) and F(2) – F(3) appears multiple times.
2. Optimal Substructure:
o The optimal solution to a problem contains within it the optimal solution to its sub-
problems.
o Example: In the shortest path problem, the shortest path from node A to C via B is
made up of the shortest paths from A to B and B to C.
3. Memoization (Top-Down Approach):
o Recursively solves sub-problems and stores results to avoid duplicate work.
o Uses a hash map or array to cache results of function calls.
4. Tabulation (Bottom-Up Approach):
o Solves all related sub-problems first, then combines them to solve the overall
problem.
o Uses a table to iteratively build solutions from smallest sub-problems.
1. Fibonacci Sequence
o Recursive solution: exponential time
o DP solution: O(n) time with memoization or tabulation
2. 0/1 Knapsack Problem
o Optimization problem: maximize value without exceeding weight capacity
o Solved using a 2D DP table
3. Longest Common Subsequence (LCS)
o Find the longest sequence common to two strings
o DP builds a matrix of subproblem solutions
4. Matrix Chain Multiplication
o Determine the most efficient way to multiply a chain of matrices
o Uses DP to avoid redundant computations
5. Edit Distance (Levenshtein Distance)
o Minimum number of operations to convert one string into another
o Solved using a 2D table (tabulation)
6. Rod Cutting Problem
o Determine maximum revenue by cutting and selling rod pieces
o Solved using top-down or bottom-up DP
7. Floyd-Warshall Algorithm
o All-pairs shortest path in a graph
o Dynamic Programming-based graph algorithm
2Q) What are the main characteristics of problems that can be solved using Dynamic
Programming? Explain with examples.
To effectively apply Dynamic Programming (DP), a problem must exhibit two key properties:
1. Overlapping Sub-Problems:
This means the problem can be broken down into smaller sub-problems which are reused several
times. Instead of solving the same sub-problems repeatedly, DP solves them once and stores the
results.
Example:
In the Fibonacci sequence, F(n) = F(n-1) + F(n-2). Both F(n-1) and F(n-2) are used in
future calculations multiple times. By storing their results, we avoid redundant calculations.
2. Optimal Substructure:
A problem exhibits optimal substructure if the optimal solution to the entire problem contains
optimal solutions to its sub-problems.
Example:
In the shortest path problem, if the shortest path from node A to D goes through B and C, then the
path from A to B, B to C, and C to D must all be the shortest paths themselves.
Answer:
Memoization and Tabulation are two different ways to implement Dynamic Programming.
Memoization (Top-Down):
Tabulation (Bottom-Up):
Answer:
Overlapping Sub-Problems
Optimal Substructure
2. Define Sub-Problems
Break the main problem into smaller, manageable sub-problems. Decide what parameters change
and what values must be calculated.
Example:
In LCS (Longest Common Subsequence), the sub-problem is:
LCS(i, j) = LCS of first i characters of string A and first j characters of
string
3. Recurrence Relation
Example (LCS):
If A[i] == B[j]:
LCS(i, j) = 1 + LCS(i-1, j-1)
Else:
LCS(i, j) = max(LCS(i-1, j), LCS(i, j-1))
Decide between:
Example:
In LCS, LCS(0, j) = 0 and LCS(i, 0) = 0
Either recursively compute the value using memoization or fill the table iteratively in tabulation.
For optimization problems like LCS, you may also trace back through the table to find the actual
sequence or path.
Field of Application:
Logistics
Investment Portfolio Selection
Inventory Management
Use Case:
A company has a limited-capacity truck and a list of goods, each with a weight and profit. The goal is
to choose the items that maximize total profit without exceeding the truck's capacity.
How DP is used:
Dynamic programming solves this by creating a 2D table, where each cell represents the best value
achievable with a given weight capacity and number of items. This avoids recalculating the same
combinations repeatedly.
Field of Application:
Bioinformatics
File comparison tools
Version control systems (e.g., Git)
Use Case:
How DP is used:
The LCS is found by building a DP matrix that compares characters from two sequences. This helps
identify common subsequences in O(m × n) time.
Field of Application:
Compiler Optimization
Mathematical Computation Systems
Computer Graphics
Use Case:
When multiplying a chain of matrices, the order of multiplication can affect the number of
operations. The goal is to find the most efficient order of multiplications.
How DP is used:
DP helps find the minimum number of scalar multiplications needed. The solution involves
partitioning the problem and solving subproblems optimally.
Field of Application:
Network Routing
Urban Transportation Systems
Game AI Pathfinding
Use Case:
In computer networks or GPS systems, finding the shortest path between all pairs of nodes or cities
is essential for routing efficiency.
How DP is used:
Field of Application:
Use Case:
Determining how similar two strings are by calculating the minimum number of insertions, deletions,
or substitutions required to transform one into another.
How DP is used:
A DP table is built where each cell represents the minimum edit operations required up to that
character in both strings.
6. Rod Cutting Problem
Field of Application:
Revenue Optimization
Manufacturing
Pricing Strategy
Use Case:
A company wants to maximize revenue by cutting a rod into different lengths with assigned prices.
How DP is used:
DP considers all possible ways to cut the rod and stores intermediate results to avoid recalculations.
Field of Application:
Use Case:
Given a set of coin denominations, determine the minimum number of coins needed to make a
given amount.
How DP is used:
DP builds a solution using previously computed answers to smaller amounts, ensuring the minimum
number of coins is used.
Field of Application:
Artificial Intelligence
Robotics
Game Theory
Use Case:
An AI robot navigating a grid must decide the optimal action at each step to reach the goal
efficiently.
How DP is used:
Dynamic Programming helps in policy iteration and value iteration to compute the best long-term
strategy.
Field of Application:
Operations Research
Project Management
Economics
Use Case:
Allocating a fixed budget across various projects to maximize total return or minimize cost.
How DP is used:
By dividing the total budget into smaller units, DP finds the optimal way to allocate resources across
multiple tasks.
Field of Application:
Game Development
Robotics Navigation
Maze Solving Algorithms
Use Case:
Finding the shortest or safest route for a character or agent in a game map.
How DP is used:
Algorithms like A* or Dijkstra's with DP components are used to store and reuse path values.
1. Identify Stages:
Break the decision-making process into stages. Each stage represents a variable in the LP model.
State variables represent the condition or resource left after a decision at a given stage (e.g.,
remaining capacity, remaining budget).
These are the choices made at each stage (e.g., how much of a product to produce using remaining
resources).
Establish a function that relates the current stage to the next using a recurrence relation.
5. Boundary Conditions:
Compute the optimal value using either bottom-up (tabulation) or top-down (memoization)
approaches.
When to Use DP for LP:
Real-World Examples:
Limitations:
Answer:
Dynamic Programming (DP) is a powerful method for solving optimization problems, but it
is not typically used for general Linear Programming (LP) because of several limitations:
8Q) What type of Linear Programming problems are best suited for Dynamic
Programming approaches?
Answer:
Dynamic Programming is ideal for LP problems that can be structured into a sequence of
decisions, typically with the following characteristics:
1. Multistage Nature:
o The problem can be broken down into stages, where each stage involves
making a decision based on previous outcomes.
2. Recursive Structure:
o The optimal solution at any stage depends only on the state resulting from the
previous decision — this is known as the optimal substructure property.
3. Integer or Discrete Variables:
o Problems where variables take on discrete values, such as 0, 1, 2, … (e.g.,
items in a knapsack, investment units, number of products), are better suited
for DP.
4. Resource Allocation Problems:
o Examples include capital budgeting, project selection, or knapsack-type
problems where a fixed resource must be divided among competing activities.
5. Sequential or Time-based Decisions:
o Problems involving inventory planning, maintenance scheduling, or stage-
wise production over time are perfect for DP approaches.
9Q) Compare the Dynamic Programming approach with the Simplex Method in solving LP problems.
Example Scenario:
A company has a $10 million budget to invest over 3 years in 2 projects. The return depends
on the investment made each year.
Stage-wise breakdown:
Stage 1 (Year 1): Decide how much to invest in Project A and Project B.
Stage 2 (Year 2): Reinvest profits or allocate unused budget.
Stage 3 (Year 3): Final investments based on prior returns.
where s' is the new state after the decision, and R is the return.
Outcome:
DP calculates the maximum cumulative return by recursively evaluating investment
combinations at each stage, storing the best outcomes at each state.
Answer:
Despite these limitations, DP remains invaluable in niche areas such as inventory control,
multistage decision-making, and resource management.
1. Telecommunications:
o Managing data traffic in networks, call centers, and internet service providers.
2. Banking and Retail:
o Reducing customer wait times, optimizing the number of tellers or checkout
lanes.
3. Healthcare:
o Scheduling in hospitals and clinics to minimize patient waiting time.
4. Manufacturing:
o Assembly lines and machine servicing processes.
5. Transportation:
o Managing queues at toll booths, airports, or public transport systems.
6. IT Services:
o Resource allocation in cloud computing, load balancing in web servers.
1. M/M/1 Queue:
2. M/M/c Queue:
3. M/M/1/K Queue:
4. M/G/1 Queue:
5. G/G/1 Queue:
6. Priority Queues: