DAA endsem notes
DAA endsem notes
UNIT 3 DP
Principle of optimality
The optimal solution to the dynamic optimization problem can be found using the
optimal solution to its subproblem
Overlappin subproblems
Optimal substructure
0/1 Knapsack
1. Problem Definition
Given n items with:
o Weight: wiw_iwi
o Value: viv_ivi
Base Cases
F(0,j)=0: No items → zero value.
F(i,0)=0: Knapsack with zero capacity → zero value.
3. Implementation Approaches
Bottom-Up (Tabulation)
Table Size: (n+1)×(W+1)
Steps:
1. Build table row by row using the recurrence relation.
2. Final solution: F(n,W) (bottom-right cell).
Time Complexity: O(nW)
Space Complexity: O(nW)
Top-Down (Memoization)
Use recursion with memoization:
o Before solving F(i,j)F(i, j)F(i,j), check if already computed.
o If not, solve using the recurrence relation and store the result.
Recursive Function:
int MFKnapsack(int i, int j) {
if (i == 0 || j == 0) return 0;
if (table[i][j] != -1) return table[i][j];
if (w[i] > j)
table[i][j] = MFKnapsack(i-1, j);
else
table[i][j] = max(MFKnapsack(i-1, j), v[i] + MFKnapsack(i-1, j - w[i]));
return table[i][j];
}
Recurrence Relation
Let F(i) represent the minimum number of coins needed to make the amount i.
1. Base Case:
F(0) = 0 (zero amount requires zero coins).
2. Recursive Case:
F(i) = min{F(i - dj)} + 1 for all dj <= i, where i > 0.
This checks all denominations dj that are less than or equal to i and adds 1 for
including coin dj in the solution.
Bottom-Up DP Algorithm
We iteratively compute F(1) through F(n) using the recurrence relation:
Steps:
1. Initialize a DP table F of size n+1:
o F[0] = 0
Bellman-Ford Algorithm
The Bellman-Ford algorithm is used to compute the shortest paths from a
single source vertex to all other vertices in a weighted graph, even if there are
negative edge weights. However, it does not work if the graph contains a
negative cycle.
o The distances to all other vertices are set to infinity (or a very
large value).
2. Iteration
The core of the algorithm involves iterating through the edges of the
graph multiple times.
For each edge (u,v):
o The algorithm checks whether the distance to vertex u can be
shortened:
dist[u] + dist[u,v] < dist[v]
o If this condition is true, the distance to vertex u is updated:
dist[v] = dist[u] + dist[u,v]
This step is repeated for a number of iterations equal to V - 1 (number of
vertices minus one).
Summary
The Bellman-Ford algorithm is a reliable method for single-source shortest path
problems in graphs with negative edge weights. While it is slower than Dijkstra's
algorithm for graphs without negative weights, its ability to detect negative
cycles makes it more versatile.
Here’s the table with bold formatting for the headers and key points:
Best Use Suitable for sparse graphs Typically more efficient for
Case (where E is much smaller dense graphs and graphs
than V²) and when negative with non-negative edge
edge weights may be weights.
present.
You can now paste this table directly into Microsoft Word, and the bold formatting
will be preserved.
Multistage graph
Here the graph consists of different stages
F(2,3) min cost to final node from stage 2 and vertex 3,
TSP
The Travelling Salesperson Problem (TSP) is a classic optimisation problem in
computer science. The goal is to find the shortest possible route that
visits each city exactly once and returns to the origin city. It's easily
visualised as a weighted graph where cities are vertices and distances between
cities are edge weights. The problem is to find the shortest Hamiltonian circuit (a
cycle that visits every vertex exactly once) in this graph. The TSP has numerous
real-world applications, including route planning, logistics, and even more
modern applications like circuit board fabrication and genetic engineering.
Why dynamic programming is unsuitable for solving TSP:
While dynamic programming offers efficient solutions for certain problems by
breaking them into overlapping subproblems and storing their solutions, it's
highly impractical for the TSP, especially for larger instances. Here's why:
Exponential Complexity: A naive dynamic programming approach to
the TSP would involve exploring all possible permutations of the cities. The
number of such permutations is n!, where n is the number of cities. This
factorial growth makes the computational cost explode exponentially as n
increases, rendering the approach infeasible for anything beyond a very
small number of cities. Even an improved dynamic programming
approach, which reduces complexity to O(n 2 2ⁿ), remains impractical for
realistically sized problems.
Space Requirements: The dynamic programming solution requires
storing intermediate results for all possible subsets of cities and partial
tours. The space complexity for this approach is O(n * 2ⁿ), which is also
exponential and becomes unmanageable for even moderately sized TSP
instances. This immense storage requirement outweighs any potential
speedup the dynamic programming approach might offer.
Alternative Approaches: Because of its inherent complexity, the TSP is
typically tackled using approximation algorithms or heuristics. These
methods might not find the absolute best solution, but they provide
reasonably good solutions in a reasonable amount of time for large
problem instances. Branch and bound is another technique which can
sometimes effectively handle larger instances than a pure dynamic
programming approach.
In short, while dynamic programming is a powerful tool, its exponential time and
space complexity makes it unsuitable for efficiently solving the TSP except for
extremely small instances. Approximation algorithms and heuristics are far
more practical for handling realistic-sized TSP problems.
unit 4
This is not a optimization problem
This is a method to find all possible solutions
M colouring decision problem can we color the graph using the given colurs?
M colouring optimization minimum colors to color the graph (chromatic
numee problem)
Hamiltonian cycle visit all vertices exactly once and come back to start
Graph may be directed/undirected
Should be connected
Duplicate cycle not allowed
Not possible in cases: