DP 202
DP 202
for (int i = 3; i <= n; i++) The function uses a for loop to iterate from 3 to
{ n, and in each iteration, it calculates the next
temp = a + b; number in the series using the formula temp = a
a = b; + b. The function then updates the values of a
b = temp; and b using the assignments a = b and b =
printf("%d ", b); temp.
}
}` Finally, the function prints the next number in
the series using the statement printf("%d ", b).
int main() { Enter the number of terms: 10
Fibonacci series: 0 1 1 2 3 5 8 13 21 34
int n;
return 0;
}
Key Concepts of Dynamic Programming in Design and Analysis of Algorithms (DAA)
1. Optimal Substructure:
○ This means that the solution to a given problem can be obtained by combining the optimal
solutions to its subproblems. It's a hallmark of problems suited for dynamic programming.
2. Overlapping Subproblems:
○ Dynamic programming is efficient because it stores the solutions to subproblems in a table
(usually an array ) and reuses these solutions when the same subproblem arises again.
○
Memoization:
● This involves storing the results of expensive function calls and reusing them when the
same inputs occur again. It's typically implemented using a top-down approach with
recursion.
Tabulation:
● This is the opposite of memoization. It uses a bottom-up approach, starting with the
simplest subproblems and combining them to solve larger problems, usually with iterative
loops.
1. Fibonacci Sequence (Basic DP Example)
● Problem: Given a sequence of matrices, find the minimum number of scalar multiplications needed to
multiply them.
Examples of Dynamic Programming