Consider a directed graph where weight of its edges can be one of x, 2x or 3x (x is a given integer), compute the least cost path from source to destination efficiently.

For example, consider below graph

If source is 1 and destination is 3,

least cost path from source to destination is “1 4 3” having cost 2.

If source is 0 and destination is 2,

least cost path from source to destination is “0 4 2” having cost 3.

We know that breadth-first search can be used to find shortest path in an unweighted graph or in weighted graph having same cost of all its edges. BFS runs in O(E+V) time where E is the number of edges and V is number of vertices in the graph. But if the edges in the graph are weighted with different costs, then the recommended algorithm is Dijkstra’s Algorithm which takes O(E log V) time.

**Can we use BFS?**

The idea is to modify the input graph in such a way that all its edges have same weight. For edges having weight 3x, we split them into three edges of weight x each. Similarly, edges having weight 2x gets split into two edges of weight x each. Nothing needs to be done for edges already having weight x. Special care has to be taken while introducing new edges in the graph such that we should not introduce new routes into the graph. So in order to split an edge of weight 3x, we need to create two new vertices in the graph instead of using existing vertices. Similarly, in order to split edge having weight 2x, we need to create one new vertex. Lets illustrate this with the help of a diagram.

Split edge (v, u) having weight 3x into three edges (v, v+N), (v+N, v+2N) and (v+2N, u) each having weight x.

Split edge (v, u) having weight 2x into two edges (v, v+N) and (v+N, u) each having weight x.

## C++

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
#include <bits/stdc++.h> using namespace std; // Number of vertices in the graph #define N 5 // data structure to store graph edges struct Edge { int source, dest, weight; }; // class to represent a graph object class Graph { public: // A array of vectors to represent adjacency list vector<int> adjList[3*N]; // Constructor Graph(vector<Edge> edges, int x) { // add edges to the undirected graph for (unsigned i = 0; i < edges.size(); i++) { int v = edges[i].source; int u = edges[i].dest; int weight = edges[i].weight; // create two new vertices v+N and v+2*N if the weight // of edge is 3x. Also, split the edge (v, u) into (v, v+N), // (v+N, v+2N) and (v+2N, u) each having weight x if (weight == 3*x) { adjList[v].push_back(v + N); adjList[v + N].push_back(v + 2 * N); adjList[v + 2 * N].push_back(u); } // create one new vertex v+N if the weight of the edge // is 2x. Also split the edge (v, u) into (v, v+N), // (v+N, u) each having weight x else if (weight == 2*x) { adjList[v].push_back(v + N); adjList[v + N].push_back(u); } // no splitting is needed if edge weight is 1x else adjList[v].push_back(u); } } }; // Recursive function to print path of given vertex v from // the source vertex void printPath(vector<int> predecessor, int v, int& cost) { if (v < 0) return; printPath(predecessor, predecessor[v], cost); cost++; // consider only original nodes present in the graph if (v < N) cout << v << " "; } // Perform BFS on graph starting from vertex source void BFS(Graph const& graph, int source, int dest) { // stores vertex is discovered in BFS traversal or not vector<bool> discovered(3*N, false); // mark source vertex as discovered discovered[source] = true; // predecessor[] stores predecessor information. It is used // to trace least cost path from destination back to source. vector<int> predecessor(3*N, -1); // create a queue used to do BFS and push source vertex // into the queue queue<int> q; q.push(source); // run till queue is not empty while (!q.empty()) { // pop front node from queue and print it int curr = q.front(); q.pop(); // if destination vertex is reached if (curr == dest) { int cost = -1; cout << "Least cost path between " << source << " and " << dest << " is "; printPath(predecessor, dest, cost); cout << "having cost " << cost; } // do for every adjacent edge of current vertex for (int v : graph.adjList[curr]) { if (!discovered[v]) { // mark it discovered and push it into queue discovered[v] = true; q.push(v); // set curr as predecessor of vertex v predecessor[v] = curr; } } } } // Least cost path in weighted digraph using BFS int main() { int x = 1; // vector of graph edges as per above diagram vector<Edge> edges = { {0, 1, 3*x}, {0, 4, 1*x}, {1, 2, 1*x}, {1, 3, 3*x}, {1, 4, 1*x}, {4, 2, 2*x}, {4, 3, 1*x} }; // create a graph from edges Graph graph(edges, x); // given source and destination vertex int source = 0, dest = 2; // Do BFS traversal from given source BFS(graph, source, dest); return 0; } |

**Output: **

Least cost path between 0 and 2 is 0 4 2 having cost 3

**Thanks for reading.**

Please use ideone or C++ Shell or any other online compiler link to post code in comments.

Like us? Please spread the word and help us grow. Happy coding 🙂

## Leave a Reply

very nice.. thanks!