0% found this document useful (0 votes)
6 views17 pages

DAA Unit 4 Part 1

Dynamic programming is a method for solving problems by combining solutions to overlapping subproblems, storing results to avoid recomputation. It is particularly useful for optimization problems, such as the 0/1 knapsack problem, which seeks to maximize value within a weight capacity. The All-Pairs Shortest Path algorithm, specifically the Floyd-Warshall algorithm, computes the shortest paths between all pairs of nodes in a weighted graph using a distance matrix.
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)
6 views17 pages

DAA Unit 4 Part 1

Dynamic programming is a method for solving problems by combining solutions to overlapping subproblems, storing results to avoid recomputation. It is particularly useful for optimization problems, such as the 0/1 knapsack problem, which seeks to maximize value within a weight capacity. The All-Pairs Shortest Path algorithm, specifically the Floyd-Warshall algorithm, computes the shortest paths between all pairs of nodes in a weighted graph using a distance matrix.
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/ 17

Unit 4

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.

The Floyd-Warshall Algorithm


Floyd-Warshall Algorithm
Algorithm:
• Let d[i][j] be the distance matrix where d[i][j] is the shortest distance from node i to node j.
• Initially, set d[i][j] to the edge weight if there is an edge from i to j, or to infinity (∞) if there
is no edge (except d[i][i]=0).
• For each vertex k, update the distances by checking if a path from i to j via k is shorter
than the currently known path: d[i][j]=min(d[i][j],d[i][k]+d[k][j])
• After all iterations, d[i][j] will contain the shortest distance from i to j.
Floyd-Warshall Algorithm

• Following the above discussion, define recursively by


Floyd-Warshall Algorithm
Floyd-Warshall Algorithm
Floyd-Warshall Algorithm
Floyd-Warshall Algorithm
Floyd-Warshall Algorithm
Floyd-Warshall Algorithm
Floyd-Warshall Algorithm

You might also like