0% found this document useful (0 votes)
13 views10 pages

Checkpoint Week 1

The document covers the fundamentals of Dynamic Programming (DP), including recursion, overlapping subproblems, and optimization techniques like memoization and tabulation. It provides examples such as the Fibonacci sequence, Knapsack problem, Longest Common Subsequence, and Matrix Chain Multiplication, along with common pitfalls in Python related to recursion limits. Additionally, it suggests practice problems and resources for mastering DP concepts.

Uploaded by

naveenthumati095
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views10 pages

Checkpoint Week 1

The document covers the fundamentals of Dynamic Programming (DP), including recursion, overlapping subproblems, and optimization techniques like memoization and tabulation. It provides examples such as the Fibonacci sequence, Knapsack problem, Longest Common Subsequence, and Matrix Chain Multiplication, along with common pitfalls in Python related to recursion limits. Additionally, it suggests practice problems and resources for mastering DP concepts.

Uploaded by

naveenthumati095
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

CHECKPOINT

Coding Club, IIT Guwahati

< WEEK 1 >


DYNAMIC PROGRAMMING
Understanding Recursion & Overlapping
Subproblems in Dynamic Programming
What is Recursion
A method of solving problems where a function calls itself
Breaks a problem into smaller sub-problems
Often leads to repeated computations.
Overlapping Subproblems Propert
A problem has overlapping subproblems if it can be broken into smaller
subproblems that are solved multiple times
Storing the results of subproblems helps avoid redundant
computations.
Example (Fibonacci Numbers) :
Recursive Code: Recursion Tree for f(5):

#include <iostream>
f(5)

using namespace std;


/ \

int f(int n) {
f(4) f(3)

if (n <= 1)
/ \ / \

return n;
f(3) f(2) f(2) f(1)

return f(n - 1) + f(n - 2);


/ \ / \ / \

}
f(2) f(1) f(1) f(0) f(1) f(0)

int main() {
/ \

cout << f(5);


f(1) f(0) 


return 0;
Problem : fib(3), fib(2), and other values

are computed multiple times.

Optimized Computation Tree :

f(5)

/ \

f(4) f(3)[Already computed]

/ \

f(3) f(2)[Already computed]

/ \

Each value is computed only once

f(2) f(1)
and reused!

/ \
Solution : Store & Reuse Computed

Values(Core Idea of DP)

f(1) f(0)

Approaches to Store Results

Memoization (Top-Down DP):


Store results of subproblems in an array (or a map) and reuse them

Tabulation (Bottom-Up DP):

Solve smaller subproblems first and use their results to build up the

solutions.

Key Takeaways

Recursion without optimization leads to redundant calculations

Identifying overlapping subproblems helps optimize solutions

Dynamic Programming stores computed values to enhance efficiency.


For solving questions related to DP :

If the problem can be solved by breaking it into smaller similar


subproblems, identify the transition relation that connects them. For
example, in the Fibonacci sequence, we observe that fib(n) depends on
fib(n-1) and fib(n-2). This means that the larger problem fib(n) is being
solved using the solutions to smaller subproblems fib(n-1) and fib(n-2). The
recurrence relation fib(n) = fib(n-1) + fib(n-2) provides a clear way to
compute the solution progressively.
DP IN PYTHON
Dp in python is the same as that in cpp. Here you use lists to memoize
instead of arrays in cpp. You can refer here to learn how to use python for
dp.

There is one problem with python when it comes to dp though. Python


functions take up more time and space when compared to C++
counterparts . There is a high chances of getting runtime error (RE) or Time
limit exceeded error (TLE) in recursive code due to the following reason:

Maximum Recursion Depth exceeded (MRD):


Problem : In the backend, all recursion calls are stored in a stack. When the
number of recursion calls exceeds the stack size, you get this error.

Solution : Import the sys module and use the set recursion limit attribute to
increase the recursion limit to a value that doesn’t give RE (a matter of trial
and error). A general value that works in many problems is 3* 10^5.

Memory Limit Exceeded (MLE):


Problem : Every problem has a particular memory limit that you shouldn’t
exceed. But when your stack size increases, it uses more memory giving out
MLE.

Solution : Like the soln above, you could reduce the stack size using the set
recursion limit attribute but then again it would give you a MRD. So the
best thing would be to avoid recursive calls at all. This can be done by
writing the iterative version of the recursive logic you used.

Few resources:
CPH (pg 65 to 69) (If you don’t understand in one read, read it again
Errichto (Lecture 1 and Lecture 2
Luv Competitive Programming DP playlist
If you didn’t understand even after referring to the resources, don’t worry
' j j
it s normal, ust ump to solving problems.
Common Examples of DP :

Knapsack problem

The Knapsack Problem is a classic optimization problem that has dynamic

programming based solution. Suppose you are given n items, each with a

weight and a value and a knapsack with a maximum weight capacity W.

The problem involves selecting a subset of items to maximize total value

while staying within the given weight limit.

It is broadly of 3 types

Fractional Knapsack - Simple greedy O(nlogn) solution. Just sort the

items in decreasing order of value by weight ratio and the larger ones

first

0-1 Knapsack - Solved using DP (one item can be picked atmost once

Unbounded Knapsack - Solved using DP (each item can be picked

one or more times)

But what is the algorithm? Refer this or this

Feeling bored from reading lengthy blogs? Don’t worry click here

Practice Problems

Knapsack
Coin Combinations
Subset Sum Problem (Slight variation of knapsack

Mortal Kombat Towe


Two Sets I
The Values You Can Make (Difficult)
Longest Common Subsequence
The Longest Common Subsequence (LCS) problem is a classic dynamic
programming problem that finds the longest subsequence common to
two given strings while maintaining the relative order of characters.
Unlike substrings, elements of an LCS do not need to be contiguous.

Let’s say we are tasked to find the length of the longest common
subsequence of strings s and t.

The idea here is to make a 2 dimensional array for dp, where dp[i][j] would
store the length of the longest common subsequence of strings formed
using the first i characters of s and first j characters of t.

The transitions would then be :


if s[i] != t[j] then dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]
if s[i] == t[j] then dp[i][j] = max({1 + dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]})

Reading resources
GeeksforGeek
takeUforward

Practice Problems
LC
Print LC
Three Strings

Matrix Chain Multiplication


Another classic problems in DP is Matrix Chain Multiplication(MCM).

Suppose you are given n matrices A1, A2, . . .,An with dimensions p0 x p1,
p1 x p2, . . ., p(n−1) x pn. The goal is to determine the optimal
parenthesization that minimizes the total number of scalar
multiplications.

This problem belongs to a category of problems where


The state is defined by a range (i, j)
The solution requires taking the minimum (or sometimes maximum)
over some k.
Refer this for understanding the algorithm.
The time complexity of the solution is O(n³).

Knuth's Optimization :
Many problems like Matrix Chain Multiplication follow a quadrangle
inequality, allowing an O(n³) DP solution to be optimized to O(n²).

Refer this or this for better understanding

Practice Problems
Burst Balloon
Slime
Array Descriptio
ROCK - Sweet and Sour Rock

Miscellaneous Problems
Rudolf and k Bridge
How Does the Rook Move
Sending a Sequence Over the Networ
Colored Ball
GCD on a gri
Feed Cats
Rolling Optimization
Reduces the space complexity form O(n) to O(1)

Based on the fact that you do not need to store data once it has be
used to calculate all the states it can. Recall the iterative version of
Fibonacci number problem. We only need the information about two
previous states for the calculation of our current state ( F[n] = F[n-1] +
F[n-2] ). So, the idea is to start calculating F[3] with the help of F[2] and
F[1] and then forget about F[1] as F[4] doesn’t depends on F[1] and so on.

If your logic seems correct, but still Memory Limit Exceeded in showing,
try this approach

Probabilities and DP States


Probabilistic Dynamic Programming (DP) constitutes a
complete chapter on its own. However, its concept of finding
expectations using DP states proves highly valuable in certain
competitive programming

problems.

The fundamental idea is to decompose the expectation value


function into two or more terms involving previously calculated
values multiplied by their respective probabilities. This concept
may seem logical, but it can also be a bit vague at first –
particularly in understanding how probabilities are multiplied
with expectation values. This understanding, in fact, arises from
the Law of Total Expectations.

Ilyn and Escalators | Solution


Sushi | Solution
BITMASK DP (optional)

What is Bit masking?

Suppose we have a collection of elements which are numbered


from 1 to N. If we want to represent a subset of this set then it
can be encoded by a sequence of N bits (we usually call this
sequence a “mask”). In our chosen subset the i-th element
belongs to it if and only if the i-th bit of the mask is set i.e., it
equals to 1. For example, the mask 10000101 means that the

subset of the set [1... 8] consists of elements 1, 3 and 8. We know


that for a set of N elements there are total 2N subsets thus 2N
masks are possible, one representing each subset. Each mask is,
in fact, an integer number written in binary notation.

Bitmask DP usually have exponential time complexity, so value


of n is very small (<25).

Few resources
CPH (102-106
Nwatx

PROBLEMS
O - Matchin
Team Buildin
Elevator Ride
Carrying Conundru
Hammiltonian Flight
Shuffling Songs
Recommended practice problems for mastering DP :

CSES DP Section : Visit the DP section on the CSES problem set and

try to solve problems from there

AtCoder DP Contest : Explore the DP section in the AtCoder Beginner

Contest series, known for its comprehensive DP problems. It covers

almost all DP techniques

USACO Guide : Another useful resource for getting more familiarised

with DP.

Additional Intermediate Problems

Tenzing and Ball

Jellyfish and ME

Sending a Sequence Over the Networ

Zero Pat

Road Optimizatio

Korney Korneevich and XO

Block Sequenc

Journe

Coloring Tree

Mahmoud and a Message

Additional Hard Problems

Blocking Element

Magic Will Save the Worl

Counting Rhyme

You might also like