DAA Unit-3 ppt 19
DAA Unit-3 ppt 19
Wherever we see a recursive solution that has repeated calls for same
inputs, we can optimize it using Dynamic Programming.
In the case of recursion, repeated calls are made for the same sub-problem, but we
can optimize this problem with the help of dynamic programming.
The idea behind using dynamic programming is that we store the results of sub-
problems so that we do not need to re-compute the sub-problem whenever required.
This reduces the time complexities from exponential time to linear time.
To dynamically solve a problem, we need to check two necessary
conditions:
1. Overlapping Subproblems: When the solutions to the same subproblems are needed repetitively
for solving the actual problem. The problem is said to have overlapping subproblems property.
State:
It requires Dynamic
It is more efficient in
Programming table for
Memoization terms of memory as it
Memoization and it
never look back or revise
increases it’s memory
previous choices
complexity.
Greedy methods are generally faster. For Dynamic Programming is generally
example, Dijkstra’s shortest path slower. For example,
Time complexity
algorithm takes O(ELogV + VLogV) Bellman Ford algorithm takes O(VE)
time. time.
The greedy method computes its solution Dynamic programming computes its
by making its choices in a serial forward solution bottom up or top down by
Fashion
fashion, never looking back or revising synthesizing them from smaller optimal
previous choices. sub solutions.
Fractional knapsack .
Example 0/1 knapsack problem
0/1 Knapsack problem
Here knapsack is like a container or a bag. Suppose we have given some items which have
some weights or profits. We have to put some items in the knapsack in such a way total value
produces a maximum profit.
For example, the weight of the container is 20 kg. We have to select the items in such a way
that the sum of the weight of items should be either smaller than or equal to the weight of
the container, and the profit should be maximum.
The 0/1 knapsack problem means that the items are either completely or no items are filled in
a knapsack. For example, we have two items having weights 2kg and 3kg, respectively. If we
pick the 2kg item then we cannot pick 1kg item from the 2kg item (item is not divisible); we
have to pick the 2kg item completely. This is a 0/1 knapsack problem in which either we pick
the item completely or we will pick that item. The 0/1 knapsack problem is solved by the
dynamic programming.