Dynamic Programming

 Dynamic Programming

Dynamic Programming (DP) is a technique used to solve problems by breaking them down into smaller, simpler subproblems. It's especially useful for optimization problems where the same subproblems are solved repeatedly.

Key Ideas:

  1. Overlapping Subproblems: The problem can be divided into smaller subproblems, and the same subproblems are solved multiple times.
  2. Optimal Substructure: The solution to the overall problem can be constructed from the solutions to these smaller subproblems.

How It Works:

  • Memoization: This is a top-down approach where you solve the problem recursively, but you store (or "memoize") the results of subproblems to avoid recomputing them.
  • Tabulation: This is a bottom-up approach where you solve the problem iteratively, starting with the simplest subproblems and building up to the final solution.

In Short:

Dynamic programming helps you solve problems efficiently by solving each small part of the problem only once and then reusing those solutions to build up the final answer.

MULTISTAGE GRAPH

A multistage graph is a directed graph where the nodes can be divided into stages or layers. The goal is to find the shortest path or minimum cost path from a source node to a destination node, moving through these stages and solving them recursively, storing the solutions to avoid redundant calculations.

Key Concepts:

  • The graph is divided into stages. Each stage has nodes that are connected to nodes in the next stage.
  • You start at the first stage and move to the last stage, finding the best possible way to move from one stage to the next (e.g., minimizing cost or distance).
  • At each stage, you decide the best node to move to from the previous stage, based on some criteria (like cost or distance).

Steps to Solve Using Dynamic Programming:

  1. Stage-wise Breakdown: The graph is broken into stages, where each stage has nodes.
  2. Recursion Relation: For each node at a stage, you calculate the best path (e.g., shortest or minimum cost) to get to that node from the previous stage. This is done by considering the optimal solutions to earlier stages.
  3. Backward Calculation: Starting from the last stage (destination), you work backward to the first stage (start), computing the optimal path or cost for each node.

 

ALL-PAIRS SHORTEST PATH PROBLEM (APSP)

The All-Pairs Shortest Path (APSP) problem involves finding the shortest paths between every pair of nodes in a graph. For example, given a graph with nodes A, B, C, and D, we want to find the shortest path from:

  • A to B,
  • A to C,
  • A to D,
  • B to A,
  • B to C,
  • B to D, etc.

A common algorithm to solve this problem using dynamic programming is Floyd-Warshall Algorithm. The idea is to iteratively improve the shortest paths between nodes by considering each node as an intermediate node that might provide a shorter path.

Key Idea:

·  The main idea is that the shortest path from node i to node j may go through an intermediate node k.

·  For each pair of nodes (i, j), we check if the path from i to j through node k is shorter than the direct path from i to j.

Floyd-Warshall Algorithm (Implementation):

1. Initialize distance matrix D as follows:
   For each vertex i and j:
       If i == j:
           D[i][j] = 0
       Else if there is an edge (i, j) in the graph:
           D[i][j] = W[i][j]
       Else:
           D[i][j] = ∞

2. for k from 1 to n:  // Consider each vertex as an intermediate point
       for i from 1 to n:  // Iterate over all pairs of vertices (i, j)
           for j from 1 to n:
               D[i][j] = min(D[i][j], D[i][k] + D[k][j])

3. Return matrix D as the result


Time Complexity:

  • The algorithm runs in O(n³) time, where n is the number of nodes. This is because there are three nested loops, each iterating over n nodes.

 

Comments

Popular posts from this blog

Backtracking - N-Queens Problem, Sum of Subsets, Graph Colouring, Hamiltonian Cycle

Divide and Conquer Technique - Binary Search, Quick Sort, Merge Sort

GRAPH THEORY