DAA Unit 4 Part 1
DAA Unit 4 Part 1
Dynamic Programming
Dynamic Programming
• Dynamic programming, like the divide-and-conquer method, solves problems by
combining the solutions to subproblems. (Programming in this context refers to a tabular
method, not to writing computer code.)
• Divide-and-conquer algorithms partition the problem into disjoint subproblems, solve the
subproblems recursively, and then combine their solutions to solve the original problem.
• In contrast, dynamic programming applies when the subproblems overlaps, that is, when
subproblems share subproblems.
• A dynamic-programming algorithm solves each subproblem just once and then saves its
answer in a table, thereby avoiding the work of recomputing the answer every time it
solves each subproblem.
• Dynamic programming typically applies to optimization problems. Such problems can
have many possible solutions.
• Each solution has a value, and you want to find a solution with the optimal (minimum or
maximum) value
0/1 Knapsack Problem
The 0/1 knapsack problem means that the items are either completely or no items are filled
in a knapsack.
0/1 Knapsack using dynamic programming uses a tabulation method as follows:
In the above matrix, columns represent the weight, i.e., 8. The rows represent the profits
and weights of items.
0/1 Knapsack Problem
0/1 Knapsack Problem
0/1 Knapsack Problem
0/1 Knapsack Problem
Problem Given : values = [300, 200, 400, 500]
weights = [2, 1, 5, 3]
capacity = 10
All Pair Shortest Path Algorithm
The All-Pairs Shortest Path (APSP) algorithm finds the shortest paths between every pair
of nodes in a weighted graph.