Minimum cost of path between given nodes containing at most K nodes in a directed and weighted graph


Introduction

The issue of finding the minimum cost of a path between two given nodes in a directed and weighted graph, whereas ensuring that the path contains at most K nodes, may be a significant challenge in chart hypothesis. This issue has different applications in differing domains, counting transportation systems, logistics planning, and network optimization. In this article, we have investigated two distinctive approaches to handling this issue using C language. Each approach utilizes a special algorithmic procedure to discover the least cost path whereas considering the constraint on the number of nodes in the way.

Approach 1: Dynamic Programming (Bottom-up Approach)

Algorithm

  • Step 1 − Make a 2D table to store the least cost of paths from the source to each vertex with at most K nodes.

  • Step 2 − Initialize all components of the table with an expansive value, but for the source node which is set to 0.

  • Step 3 − Iterate over the vertices for K times. For each vertex, iterate over all the vertices.

  • Step 4 − Update the least cost in the table as the least value between the current cost and the cost of coming to the vertex through any intermediate vertex inside K-1 nodes.

  • Step 5 − The least cost of the way from the source to the goal with at most K nodes is the value within the table for the destination vertex. 

Example

#include <stdio.h>
#include <limits.h>

#define V 5
#define INF INT_MAX

int min(int a, int b) {
   return (a < b) ? a : b;
}

int minCostPath(int graph[V][V], int src, int dest, int K) {
   int dp[V][K + 1];

   for (int i = 0; i < V; i++) {
      for (int j = 0; j <= K; j++) {
         dp[i][j] = INF;
      }
   }

   dp[src][0] = 0;

   for (int k = 1; k <= K; k++) {
      for (int i = 0; i < V; i++) {
         for (int j = 0; j < V; j++) {
            if (graph[i][j] != 0 && dp[i][k - 1] != INF) {
               dp[j][k] = min(dp[j][k], dp[i][k - 1] + graph[i][j]);
            }
         }
      }
   }

   int minCost = INF;
   for (int k = 0; k <= K; k++) {
      minCost = min(minCost, dp[dest][k]);
   }

   return minCost;
}
int main() {
   int graph[V][V] = {
      {0, 10, 3, 2, 0},
      {0, 0, 0, 7, 0},
      {0, 0, 0, 6, 0},
      {0, 0, 0, 0, 2},
      {0, 0, 0, 0, 0}
   };
   int src = 0;
   int dest = 4;
   int K = 2;

   int minCost = minCostPath(graph, src, dest, K);

   printf("Minimum cost of path from %d to %d with at most %d nodes: %d\n", src, dest, K, minCost);

   return 0;
}

Output

Minimum cost of path from 0 to 4 with at most 2 nodes: 4

Approach 2: Depth-First Search (DFS)

Algorithm

  • Step 1 − Make a gone-to array to keep track of going to vertices and initialize a variable to store the least cost way as infinity.

  • Step 2 − Actualize a depth-first search function with parameters for the current vertex, cost, number of paths to nodes, and the goal vertex.

  • Step 3 − Within the DFS function, check in case the current vertex is the goal and the nodes are less than or rise to K. In case so, upgrade the least cost way as the least between the current cost and the least cost way.

  • Step 4 − If the count is greater than K, return. Mark the current vertex as visited and recursively call the DFS function for all unvisited adjoining vertices with an upgraded cost and check.

  • Step 5 − After the DFS is total, the value of the least cost way speaks to the least fetched of the way from the source to the goal with at most K nodes.

Example

#include <stdio.h>
#include <stdbool.h>
#include <limits.h>

#define V 5
#define INF INT_MAX

int min(int a, int b) {
   return (a < b) ? a : b;
}
void dfs(int graph[V][V], int src, int dest, int K, bool visited[], int cost, int count, int* minCost) {
   if (src == dest && count <= K) {
      *minCost = min(*minCost, cost);
      return;
   }

   if (count > K)
      return;

   visited[src] = true;

   for (int i = 0; i < V; i++) {
      if (graph[src][i] != 0 && !visited[i]) {
         dfs(graph, i, dest, K, visited, cost + graph[src][i], count + 1, minCost);
      }
   }

   visited[src] = false;
}
int minCostPath(int graph[V][V], int src, int dest, int K) {
   bool visited[V];
   for (int i = 0; i < V; i++) {
      visited[i] = false;
   }

   int minCost = INF;

   dfs(graph, src, dest, K, visited, 0, 0, &minCost);

   return minCost;
}
int main() {
   int graph[V][V] = {
      {0, 10, 3, 2, 0},
      {0, 0, 0, 7, 0},
      {0, 0, 0, 6, 0},
      {0, 0, 0, 0, 2},
      {0, 0, 0, 0, 0}
   };
   int src = 0;
   int dest = 4;
   int K = 2;

   int minCost = minCostPath(graph, src, dest, K);

   printf("Minimum cost of path from %d to %d with at most %d nodes: %d\n", src, dest, K, minCost);

   return 0;
}

Output

Minimum cost of path from 0 to 4 with at most 2 nodes: 4

Conclusion

In this article, we talked about two strategies for deciding the slightest cost path in a coordinated and weighted graph between two given nodes, with the extra necessity that the way contains most K nodes. For the same illustration input, all two strategies allow the same and amend the result. The strategy utilized depends on the person’s prerequisites and properties of the graph and the current issue. By being mindful of these techniques, architects can effectively address the issue of the least cost route with confinement on the number of nodes, picking up profitable encounters in asset allotment.

Updated on: 25-Aug-2023

157 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements