0% found this document useful (0 votes)
13 views

Daa Unit-3 (Mid-1)

Uploaded by

lalithendran10
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 views

Daa Unit-3 (Mid-1)

Uploaded by

lalithendran10
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/ 6

Discuss about the Dynamic Programming.

Explain about
i) Principles of optimality ii) Feasible solution iii) Optimal solution.
And also differentiate how the Dynamic Programming is different from Divide
and Conquer.

Dynamic Programming (DP)


Dynamic Programming (DP) is a method used to solve complex problems by
breaking them down into simpler subproblems that overlap. It is particularly
useful for optimization problems where you need to find the best solution (e.g.,
shortest path, maximum value, etc.). DP saves the results of subproblems to
avoid recalculating them, making it more efficient than brute force methods.
There are two main approaches in DP:
1. Top-down (Memoization): Recursively solve subproblems and store their
results to avoid re-computation.
2. Bottom-up (Tabulation): Build the solution from the smallest
subproblems and use their results to solve bigger ones.
1. Principle of Optimality
The principle of optimality states that an optimal solution to a problem can be
constructed from optimal solutions to its subproblems. In other words, for a
global optimal solution to exist, the solutions to the smaller subproblems must
also be optimal.
For instance, in the shortest path problem, if the optimal path from A to D
passes through B, then the path from A to B must also be the shortest.
2. Feasible Solution
A feasible solution is any solution that satisfies all the constraints of the
problem, but it may not necessarily be the best or optimal solution. It is a
solution that is valid in the context of the problem but may not yield the best
outcome.
For example, in the knapsack problem, a feasible solution is a selection of items
that fit within the weight limit, even if they don’t provide the maximum value.
3. Optimal Solution
An optimal solution is the best solution among all feasible solutions. It is the
one that maximizes or minimizes the objective of the problem, depending on
the context.
For instance, in the same knapsack problem, the optimal solution is the
selection of items that provides the maximum possible value without
exceeding the weight limit.

Difference Between Dynamic Programming and Divide and Conquer

Dynamic Programming (DP) Divide and Conquer

Subproblem Overlap: Solves overlapping


Non-overlapping Subproblems: Solves
subproblems and uses memoization to store
subproblems independently without reusing
previously computed results to avoid redundant
results.
calculations.

Optimal Substructure: Relies on the principle of No Need for Optimal Substructure: Breaks the
optimality, where the optimal solution to a problem into independent subproblems and
problem can be constructed from optimal combines their solutions without needing to
solutions to its subproblems. store intermediate results.

Efficiency: More efficient when subproblems


Efficiency: More efficient when subproblems do
overlap, as it avoids solving the same
not overlap, as it solves them independently.
subproblem multiple times.

Approach: Bottom-up approach, where smaller Approach: Top-down approach, where the
subproblems are solved first and combined to problem is recursively divided into smaller
solve the overall problem. subproblems.

Example: Fibonacci sequence, where Example: Merge sort, where the array is divided
overlapping subproblems are solved once and into two halves, and each half is sorted
stored for future reference. independently.
Discuss about the Algorithm for All Pairs Shortest Path problem and Apply
the Same on the following.

All-Pairs Shortest Path (APSP) Problem


The All-Pairs Shortest Path (APSP) problem involves finding the shortest paths
between every pair of vertices in a graph. One popular algorithm to solve the
APSP problem is the Floyd-Warshall Algorithm, which works efficiently for
graphs with negative weight edges but no negative weight cycles.
Floyd-Warshall Algorithm
Steps:
1. Initialize the Distance Matrix: Create a distance matrix dist, where
dist[i][j] represents the distance from vertex i to vertex j. Initially, set
dist[i][j] to the weight of the edge between i and j. If there's no direct
edge between the vertices, set dist[i][j] to infinity (∞). Set dist[i][i] = 0
for all vertices (no distance from a vertex to itself).
2. Iterate over All Pairs: For every vertex k, update the distances for each
pair of vertices (i, j) such that: dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j])
This means the distance from i to j is either the direct distance or the
distance through k (if shorter).
3. Result: After all iterations, the matrix dist will contain the shortest
distances between all pairs of vertices.
Applying Floyd-Warshall Algorithm to the Given Graph
You have a rectangular graph with vertices A, B, C, D, and the following edges:
• AB = 5, BD = 3, DC = 6, CA = 4
• Diagonal edges: AD = 1, BC = 2
Let’s define the initial distance matrix for this graph.

ABCD

A0541

B5023

C 4206

D1 360

Step 1: Initial Matrix


The initial matrix represents the direct distances between the vertices (use
infinity ∞ where there's no direct edge):

ABCD

A0541

B5023

C 4206

D1 360

Step 2: Iterating Through All Vertices


We now apply the Floyd-Warshall algorithm, iterating over all vertices. For each
iteration, we update the distances based on the possibility of finding a shorter
path through an intermediate vertex.
Using Vertex A as an Intermediate (k = A):
Update dist[i][j] for each pair of vertices:

ABCD

A0541

B5023

C 4205

D1 350

Here, the distance from C to D is updated to 5 using vertex A (C → A → D).


Using Vertex B as an Intermediate (k = B):
Update dist[i][j] for each pair of vertices:

ABCD

A0541

B5023

C 4205

D1 350

No new updates are made in this iteration.


Using Vertex C as an Intermediate (k = C):
Update dist[i][j] for each pair of vertices:

ABCD

A0541

B5023

C 4205

D1 350

Again, no new updates are made.


Using Vertex D as an Intermediate (k = D):
Update dist[i][j] for each pair of vertices:

ABCD

A0441

B4023

C 4205

D1 350

The distances from A to B and B to A are updated to 4, using D as an


intermediate vertex.
Final Distance Matrix

ABCD

A0441

B4023

C 4205

D1 350

Conclusion
The final matrix represents the shortest distances between all pairs of vertices.
For example, the shortest path from A to B is 4, and the shortest path from C to
D is 5.

You might also like