# Minimum edges to reverse to make path from a source to a destination

Given a directed graph and a source node and destination node, we need to find how many edges we need to reverse in order to make at least 1 path from the source node to the destination node.

Examples:

```In above graph there were two paths from node 0 to node 6,
0 -> 1 -> 2 -> 3 -> 6
0 -> 1 -> 5 -> 4 -> 6
But for first path only two edges need to be reversed, so answer will be 2 only.```

This problem can be solved assuming a different version of the given graph. In this version we make a reverse edge corresponding to every edge and we assign that a weight 1 and assign a weight 0 to original edge. After this modification above graph looks something like below,

Now we can see that we have modified the graph in such a way that, if we move towards original edge, no cost is incurred, but if we move toward reverse edge 1 cost is added. So if we apply Dijkstraâ€™s shortest path on this modified graph from given source, then that will give us minimum cost to reach from source to destination i.e. minimum edge reversal from source to destination.

Below is the code based on above concept.

## C++

 `// C++ Program to find minimum edge reversal to get` `// atleast one path from source to destination` `#include ` `using` `namespace` `std;` `#define INF 0x3f3f3f3f`   `// This class represents a directed graph using` `// adjacency list representation` `class` `Graph` `{` `    ``int` `V;` `    ``list> *graph;`   `public``:` `    ``// Constructor:` `    ``Graph(``int` `V)` `    ``{` `        ``this``->V = V;` `        ``graph = ``new` `list>[V];` `    ``}`   `    ``// Adding edges into the graph:` `    ``void` `addEdge(``int` `u, ``int` `v, ``int` `w)` `    ``{` `        ``graph[u].push_back(make_pair(v, w));` `    ``}`   `    ``// Returns shortest path from source to all other vertices.` `    ``vector<``int``> shortestPath(``int` `source)` `    ``{` `        ``// Create a set to store vertices that are being preprocessed` `        ``set> setds;`   `        ``// Create a vector for distances and initialize all` `        ``// distances as infinite (INF)` `        ``vector<``int``> distance(V, INF);`   `        ``// Insert source itself in Set and initialize its distance as 0.` `        ``setds.insert(make_pair(0, source));` `        ``distance = 0;`   `        ``/* Looping till all shortest distance are finalized` `           ``then setds will become empty */` `        ``while` `(!setds.empty())` `        ``{` `            ``// The first vertex in Set is the minimum distance` `            ``// vertex, extract it from set.` `            ``pair<``int``, ``int``> tmp = *(setds.begin());` `            ``setds.erase(setds.begin());`   `            ``// vertex label is stored in second of pair (it` `            ``// has to be done this way to keep the vertices` `            ``// sorted distance (distance must be first item` `            ``// in pair)` `            ``int` `u = tmp.second;`   `            ``list>::iterator i;` `            ``for` `(i = graph[u].begin(); i != graph[u].end(); ++i)` `            ``{` `                ``// Get vertex label and weight of current adjacent` `                ``// of u.` `                ``int` `v = (*i).first;` `                ``int` `weight = (*i).second;`   `                ``//  If there is shorter path to v through u.` `                ``if` `(distance[v] > distance[u] + weight)` `                ``{` `                    ``/*  If distance of v is not INF then it must be in` `                        ``our set, so removing it and inserting again` `                        ``with updated less distance.` `                        ``Note : We extract only those vertices from Set` `                        ``for which distance is finalized. So for them,` `                        ``we would never reach here.  */` `                    ``if` `(distance[v] != INF)` `                        ``setds.erase(setds.find(make_pair(distance[v], v)));`   `                    ``// Updating distance of v` `                    ``distance[v] = distance[u] + weight;` `                    ``setds.insert(make_pair(distance[v], v));` `                ``}` `            ``}` `        ``}` `        ``return` `distance;` `    ``}`   `    ``Graph modelGraphWithEdgeWeight(``int` `edge[][2], ``int` `E, ``int` `V)` `    ``{` `        ``Graph g(V);` `        ``for` `(``int` `i = 0; i < E; i++)` `        ``{` `            ``// original edge : weight 0` `            ``g.addEdge(edge[i][0], edge[i][1], 0);`   `            ``// reverse edge : weight 1` `            ``g.addEdge(edge[i][1], edge[i][0], 1);` `        ``}` `        ``return` `g;` `    ``}`   `    ``int` `getMinEdgeReversal(``int` `edge[][2], ``int` `E, ``int` `V, ``int` `source, ``int` `destination)` `    ``{` `        ``// get modified graph with edge weight.` `        ``Graph g = modelGraphWithEdgeWeight(edge, E, V);`   `        ``// distance vector stores shortest path.` `        ``vector<``int``> dist = g.shortestPath(source);`   `        ``// If distance of destination is still INF then we cannot reach destination. Hence, not possible.` `        ``if` `(dist[destination] == INF)` `            ``return` `-1;` `        ``else` `            ``return` `dist[destination];` `    ``}` `};`   `int` `main()` `{` `    ``int` `V = 7;` `    ``Graph g(V);`   `    ``int` `edge[][2] = {{0, 1}, {2, 1}, {2, 3}, {5, 1}, {4, 5}, {6, 4}, {6, 3}};` `    ``int` `E = ``sizeof``(edge) / ``sizeof``(edge[0]);`   `    ``int` `minEdgeToReverse = g.getMinEdgeReversal(edge, E, V, 0, 6);`   `    ``if` `(minEdgeToReverse != -1)` `        ``cout << minEdgeToReverse << endl;` `    ``else` `        ``cout << ``"Not Possible."` `<< endl;`   `    ``return` `0;` `}`

## Java

 `// Java program to find minimum edge reversal to get` `// atleast one path from source to destination` `import` `java.util.ArrayList;` `import` `java.util.Arrays;` `import` `java.util.HashSet;` `import` `java.util.Iterator;` `import` `java.util.List;` `import` `java.util.Set;`   `class` `Pair ` `{` `    ``int` `first, second;`   `    ``public` `Pair(``int` `first, ``int` `second) ` `    ``{` `        ``this``.first = first;` `        ``this``.second = second;` `    ``}` `}`   `// This class represents a directed graph using` `// adjacency list representation` `class` `Graph{` `    `  `final` `int` `INF = (``int``)``0x3f3f3f3f``;`   `// No. of vertices` `int` `V; `   `// In a weighted graph, we need to store vertex` `// and weight pair for every edge` `List[] adj;`   `// Allocates memory for adjacency list` `@SuppressWarnings``(``"unchecked"``)` `public` `Graph(``int` `V) ` `{` `    ``this``.V = V;` `    ``adj = ``new` `ArrayList[V];`   `    ``for``(``int` `i = ``0``; i < V; i++) ` `    ``{` `        ``adj[i] = ``new` `ArrayList();` `    ``}` `}`   `// Function adds a directed edge from` `// u to v with weight w` `void` `addEdge(``int` `u, ``int` `v, ``int` `w)` `{` `    ``adj[u].add(``new` `Pair(v, w));` `}`   `// Prints shortest paths from ` `// src to all other vertices` `int``[] shortestPath(``int` `src)` `{` `    `  `    ``// Create a set to store vertices` `    ``// that are being preprocessed` `    ``Set setds = ``new` `HashSet();`   `    ``// Create a vector for distances and ` `    ``// initialize all distances as infinite(INF)` `    ``int``[] dist = ``new` `int``[V];` `    ``Arrays.fill(dist, INF);`   `    ``// Insert source itself in Set and initialize` `    ``// its distance as 0.` `    ``setds.add(``new` `Pair(``0``, src));` `    ``dist[src] = ``0``;`   `    ``// Looping till all shortest distance are ` `    ``// finalized then setds will become empty` `    ``while` `(!setds.isEmpty()) ` `    ``{` `        `  `        ``// The first vertex in Set is the minimum` `        ``// distance vertex, extract it from set.` `        ``Iterator itr = setds.iterator();` `        ``Pair tmp = itr.next();` `        ``itr.remove();`   `        ``// Vertex label is stored in second of pair (it` `        ``// has to be done this way to keep the vertices` `        ``// sorted distance (distance must be first item` `        ``// in pair)` `        ``int` `u = tmp.second;`   `        ``// 'i' is used to get all adjacent ` `        ``// vertices of a vertex` `        ``for``(Pair p : adj[u])` `        ``{` `            `  `            ``// Get vertex label and weight of` `            ``// current adjacent of u.` `            ``int` `v = p.first;` `            ``int` `weight = p.second;`   `            ``// If there is shorter path to v through u.` `            ``if` `(dist[v] > dist[u] + weight)` `            ``{` `                `  `                ``// If distance of v is not INF then it` `                ``// must be in our set, so removing it ` `                ``// and inserting again with updated` `                ``// less distance. Note : We extract ` `                ``// only those vertices from Set for ` `                ``// which distance is finalized. So ` `                ``// for them, we would never reach here.` `                ``if` `(dist[v] != INF) ` `                ``{` `                    ``setds.remove(``new` `Pair(dist[v], v));` `                ``}` `                `  `                ``// setds.erase(setds.find(new Pair(dist[v], v)));`   `                ``// Updating distance of v` `                ``dist[v] = dist[u] + weight;` `                ``setds.add(``new` `Pair(dist[v], v));` `            ``}` `        ``}` `    ``}` `    ``return` `dist;` `}` `}`   `class` `GFG{` `static` `final` `int` `INF = (``int``)``0x3f3f3f3f``;`   `// Function adds reverse edge of each original` `// edge in the graph. It gives reverse edge ` `// a weight = 1 and all original edges a ` `// weight of 0. Now, the length of the ` `// shortest path will give us the answer.` `// If shortest path is p: it means we ` `// used p reverse edges in the shortest path.` `static` `Graph modelGraphWithEdgeWeight(``int` `edge[][], ` `                                      ``int` `E, ``int` `V) ` `{` `    ``Graph g = ``new` `Graph(V);` `    ``for``(``int` `i = ``0``; i < E; i++)` `    ``{` `        `  `        ``// Original edge : weight 0` `        ``g.addEdge(edge[i][``0``], edge[i][``1``], ``0``);`   `        ``// Reverse edge : weight 1` `        ``g.addEdge(edge[i][``1``], edge[i][``0``], ``1``);` `    ``}` `    ``return` `g;` `}`   `// Function returns minimum number of edges to be` `// reversed to reach from src to dest` `static` `int` `getMinEdgeReversal(``int` `edge[][], ``int` `E, ` `                              ``int` `V, ``int` `src, ``int` `dest) ` `{` `    `  `    ``// Get modified graph with edge weight` `    ``Graph g = modelGraphWithEdgeWeight(edge, E, V);`   `    ``// Get shortes path vector` `    ``int``[] dist = g.shortestPath(src);`   `    ``// If distance of destination is still INF,` `    ``// not possible` `    ``if` `(dist[dest] == INF)` `        ``return` `-``1``;` `    ``else` `        ``return` `dist[dest];` `}`   `// Driver code ` `public` `static` `void` `main(String[] args) ` `{` `    ``int` `V = ``7``;` `    ``int` `edge[][] = { { ``0``, ``1` `}, { ``2``, ``1` `},` `                     ``{ ``2``, ``3` `}, { ``5``, ``1` `}, ` `                     ``{ ``4``, ``5` `}, { ``6``, ``4` `}, ` `                     ``{ ``6``, ``3` `} };` `    ``int` `E = edge.length;`   `    ``int` `minEdgeToReverse = getMinEdgeReversal(` `        ``edge, E, V, ``0``, ``6``);` `    `  `    ``if` `(minEdgeToReverse != -``1``)` `        ``System.out.println(minEdgeToReverse);` `    ``else` `        ``System.out.println(``"Not possible"``);` `}` `}`   `// This code is contributed by sanjeev2552`

## Python3

 `# Python3 Program to find minimum edge reversal to get` `# atleast one path from source to destination`   `# method adds a directed edge from u to v with weight w` `def` `addEdge(u, v, w):` `    ``global` `adj` `    ``adj[u].append((v, w))`   `# Prints shortest paths from src to all other vertices` `def` `shortestPath(src):` `  `  `    ``# Create a set to store vertices that are being` `    ``# preprocessed` `    ``setds ``=` `{}`   `    ``# Create a vector for distances and initialize all` `    ``# distances as infinite (INF)` `    ``dist ``=` `[``10``*``*``18` `for` `i ``in` `range``(V)]`   `    ``# Insert source itself in Set and initialize its` `    ``global` `adj` `    ``setds[(``0``, src)] ``=` `1` `    ``dist[src] ``=` `0`   `    ``# /* Looping till all shortest distance are finalized`   `    ``# then setds will become empty */` `    ``while` `(``len``(setds) > ``0``):` `      `  `        ``# The first vertex in Set is the minimum distance` `        ``# vertex, extract it from set.` `        ``tmp ``=` `list``(setds.keys())[``0``]` `        ``del` `setds[tmp]`   `        ``# vertex label is stored in second of pair (it` `        ``# has to be done this way to keep the vertices` `        ``# sorted distance (distance must be first item` `        ``# in pair)` `        ``u ``=` `tmp[``1``]`   `        ``# 'i' is used to get all adjacent vertices of a vertex` `        ``# list< pair >::iterator i;` `        ``for` `i ``in` `adj[u]:` `          `  `            ``# Get vertex label and weight of current adjacent` `            ``# of u.` `            ``v ``=` `i[``0``];` `            ``weight ``=` `i[``1``]`   `            ``# If there is shorter path to v through u.` `            ``if` `(dist[v] > dist[u] ``+` `weight):` `              `  `                ``# /* If distance of v is not INF then it must be in` `                ``#     our set, so removing it and inserting again` `                ``#     with updated less distance.` `                ``#     Note : We extract only those vertices from Set` `                ``#     for which distance is finalized. So for them,` `                ``#     we would never reach here. */` `                ``if` `(dist[v] !``=` `10``*``*``18``):` `                    ``del` `setds[(dist[v], v)]`   `                ``# Updating distance of v` `                ``dist[v] ``=` `dist[u] ``+` `weight` `                ``setds[(dist[v], v)] ``=` `1`   `    ``return` `dist`   `# /* method adds reverse edge of each original edge` `# in the graph. It gives reverse edge a weight = 1` `# and all original edges a weight of 0. Now, the` `# length of the shortest path will give us the answer.` `# If shortest path is p: it means we used p reverse` `# edges in the shortest path. */` `def` `modelGraphWithEdgeWeight(edge, E, V):` `    ``global` `adj` `    ``for` `i ``in` `range``(E):` `      `  `        ``# original edge : weight 0` `        ``addEdge(edge[i][``0``], edge[i][``1``], ``0``)`   `        ``# reverse edge : weight 1` `        ``addEdge(edge[i][``1``], edge[i][``0``], ``1``)`   `# Method returns minimum number of edges to be` `# reversed to reach from src to dest` `def` `getMinEdgeReversal(edge, E, V,src, dest):` `  `  `    ``# get modified graph with edge weight` `    ``modelGraphWithEdgeWeight(edge, E, V)`   `    ``# get shortes path vector` `    ``dist ``=` `shortestPath(src)`   `    ``# If distance of destination is still INF,` `    ``# not possible` `    ``if` `(dist[dest] ``=``=` `10``*``*``18``):` `        ``return` `-``1` `    ``else``:` `        ``return` `dist[dest]`   `# Driver code` `if` `__name__ ``=``=` `'__main__'``:` `    ``V ``=` `7` `    ``edge ``=` `[[``0``, ``1``], [``2``, ``1``], [``2``, ``3``], [``5``, ``1``],[``4``, ``5``], [``6``, ``4``], [``6``, ``3``]]` `    ``E, adj ``=` `len``(edge), [[] ``for` `i ``in` `range``(V ``+` `1``)]` `    ``minEdgeToReverse ``=` `getMinEdgeReversal(edge, E, V, ``0``, ``6``)` `    ``if` `(minEdgeToReverse !``=` `-``1``):` `        ``print``(minEdgeToReverse)` `    ``else``:` `        ``print``(``"Not possible"``)`   `        ``# This code is contributed by mohit kumar 29`

## C#

 `using` `System;` `using` `System.Collections.Generic;` `// C# program to find minimum edge reversal to get` `// atleast one path from source to destination` `namespace` `ConsoleApp` `{` `    `  `    `  `// This class represents a directed graph using` `// adjacency list representation` `    ``class` `Pair` `    ``{` `        ``public` `int` `First { ``get``; ``set``; }` `        ``public` `int` `Second { ``get``; ``set``; }`   `        ``public` `Pair(``int` `first, ``int` `second)` `        ``{` `            ``First = first;` `            ``Second = second;` `        ``}` `    ``}`   `    ``class` `Graph` `    ``{` `        ``private` `const` `int` `INF = 0x3f3f3f3f;` `        `  `// No. of vertices` `        ``public` `int` `V { ``get``; }` `        ``// In a weighted graph, we need to store vertex` `// and weight pair for every edge` `        ``private` `List[] adj;` `// Allocates memory for adjacency list` `        ``public` `Graph(``int` `V)` `        ``{` `            ``this``.V = V;` `            ``adj = ``new` `List[V];`   `            ``for` `(``int` `i = 0; i < V; i++)` `            ``{` `                ``adj[i] = ``new` `List();` `            ``}` `        ``}` `// Function adds a directed edge from` `// u to v with weight w` `        ``public` `void` `addEdge(``int` `u, ``int` `v, ``int` `w)` `        ``{` `            ``adj[u].Add(``new` `Pair(v, w));` `        ``}` `// Prints shortest paths from ` `// src to all other vertices` `        ``public` `int``[] shortestPath(``int` `src)` `        ``{` `                ``// Create a set to store vertices` `    ``// that are being preprocessed` `            ``var` `setds = ``new` `HashSet();` `            ``// Create a vector for distances and ` `    ``// initialize all distances as infinite(INF)` `            ``int``[] dist = ``new` `int``[V];` `            ``Array.Fill(dist, INF);` ` ``// Insert source itself in Set and initialize` `    ``// its distance as 0.` `            ``setds.Add(``new` `Pair(0, src));` `            ``dist[src] = 0;` `  ``// Looping till all shortest distance are ` `    ``// finalized then setds will become empty` `            ``while` `(setds.Count > 0)` `            ``{` `                 ``// The first vertex in Set is the minimum` `        ``// distance vertex, extract it from set.` `                ``var` `itr = setds.GetEnumerator();` `                ``itr.MoveNext();` `                 ``// Vertex label is stored in second of pair (it` `        ``// has to be done this way to keep the vertices` `        ``// sorted distance (distance must be first item` `        ``// in pair)` `                ``var` `tmp = itr.Current;` `                ``setds.Remove(tmp);`   `                ``int` `u = tmp.Second;`   `        ``// 'i' is used to get all adjacent ` `        ``// vertices of a vertex` `                ``foreach` `(``var` `p ``in` `adj[u])` `                ``{` `                      ``// Get vertex label and weight of` `            ``// current adjacent of u.` `                    ``int` `v = p.First;` `                    ``int` `weight = p.Second;` ` ``// If there is shorter path to v through u.` `                    ``if` `(dist[v] > dist[u] + weight)` `                    ``{ ``// If distance of v is not INF then it` `                ``// must be in our set, so removing it ` `                ``// and inserting again with updated` `                ``// less distance. Note : We extract ` `                ``// only those vertices from Set for ` `                ``// which distance is finalized. So ` `                ``// for them, we would never reach here.` `                        ``if` `(dist[v] != INF)` `                        ``{` `                            ``setds.Remove(``new` `Pair(dist[v], v));` `                        ``}` `   `  `                ``// setds.erase(setds.find(new Pair(dist[v], v)));`   `                ``// Updating distance of v` `                        ``dist[v] = dist[u] + weight;` `                        ``setds.Add(``new` `Pair(dist[v], v));` `                    ``}` `                ``}` `            ``}`   `            ``return` `dist;` `        ``}` `    ``}`   `    ``class` `GFG` `    ``{` `        ``private` `const` `int` `INF = 0x3f3f3f3f;` `// Function adds reverse edge of each original` `// edge in the graph. It gives reverse edge ` `// a weight = 1 and all original edges a ` `// weight of 0. Now, the length of the ` `// shortest path will give us the answer.` `// If shortest path is p: it means we ` `// used p reverse edges in the shortest path.` `        ``static` `Graph modelGraphWithEdgeWeight(``int``[][] edge, ``int` `E, ``int` `V)` `        ``{` `            ``var` `g = ``new` `Graph(V);` `            ``for` `(``int` `i = 0; i < E; i++)` `            ``{ ``// Original edge : weight 0` `                ``g.addEdge(edge[i][0], edge[i][1], 0);` `                ``g.addEdge(edge[i][1], edge[i][0], 1);` `            ``}` `            ``return` `g;` `        ``}` `// Function returns minimum number of edges to be` `// reversed to reach from src to dest` `        ``static` `int` `getMinEdgeReversal(``int``[][] edge, ``int` `E, ``int` `V, ``int` `src, ``int` `dest)` `        ``{` `            ``var` `g = modelGraphWithEdgeWeight(edge, E, V);` `            ``int``[] dist = g.shortestPath(src);` `  ``// If distance of destination is still INF,` `    ``// not possible` `            ``if` `(dist[dest] == INF)` `                ``return` `-1;` `            ``else` `                ``return` `dist[dest];` `        ``}` `//Driver code` `        ``static` `void` `Main(``string``[] args)` `        ``{` `            ``int` `V = 7;` `            ``int``[][] edge = { ``new` `[] { 0, 1 }, ``new` `[] { 2, 1 },` `                             ``new` `[] { 2, 3 }, ``new` `[] { 5, 1 },` `                             ``new` `[] { 4, 5 }, ``new` `[] { 6, 4 },` `                             ``new` `[] { 6, 3 } };` `            ``int` `E = edge.Length;`   `            ``int` `minEdgeToReverse = getMinEdgeReversal(` `                ``edge, E, V, 0, 6);`   `            ``Console.WriteLine(minEdgeToReverse);` `        ``}` `    ``}` `}`

## Javascript

 `// Javascript Program to find minimum edge reversal to get` `// atleast one path from source to destination`   `      ``let INF = Number.MAX_VALUE;`   `      ``// This class represents a directed graph using` `      ``// adjacency list representation` `      ``class Graph {` `        ``// Constructor:` `        ``constructor(V) {` `          ``this``.V = V;` `          ``this``.graph = Array.from(Array(V), () => ``new` `Array());` `        ``}`   `        ``// Adding edges into the graph:` `        ``addEdge(u, v, w) {` `          ``this``.graph[u].push([v, w]);` `        ``}`   `        ``// Returns shortest path from source to all other vertices.` `        ``shortestPath(source) {` `          ``// Create a set to store vertices that are being preprocessed`   `          ``let setds = ``new` `Set();`   `          ``// Create a vector for distances and initialize all` `          ``// distances as (INF)`   `          ``let distance = ``new` `Array(V);` `          ``distance.fill(INF);`   `          ``// Insert source itself in Set and initialize its distance as 0.` `          ``setds.add([0, source]);` `          ``distance = 0;`   `          ``/* Looping till all shortest distance are finalized` `                 ``then setds will become empty */` `          ``while` `(setds.size != 0) {` `            ``// The first vertex in Set is the minimum distance` `            ``// vertex, extract it from set.` `            ``let it = setds.values();` `            ``//get first entry:` `            ``let tm = it.next();` `            ``let tmp = tm.value;`   `            ``setds.``delete``(tmp);`   `            ``// vertex label is stored in second of pair (it` `            ``// has to be done this way to keep the vertices` `            ``// sorted distance (distance must be first item` `            ``// in pair)` `            ``let u = tmp[1];`   `            ``for` `(let j ``in` `this``.graph[u]) {` `              ``// Get vertex label and weight of current adjacent` `              ``// of u.` `              ``let i = ``this``.graph[u][j];` `              ``let v = i[0];` `              ``let weight = i[1];`   `              ``//  If there is shorter path to v through u.`   `              ``if` `(distance[v] > distance[u] + weight) {` `                ``/*  If distance of v is not INF then it must be in` `                              ``our set, so removing it and inserting again` `                              ``with updated less distance.` `                              ``Note : We extract only those vertices from Set` `                              ``for which distance is finalized. So for them,` `                              ``we would never reach here.  */` `                ``if` `(distance[v] != INF) setds.``delete``([distance[v], v]);`   `                ``// Updating distance of v` `                ``distance[v] = distance[u] + weight;`   `                ``setds.add([distance[v], v]);` `              ``}` `            ``}` `          ``}` `          ``return` `distance;` `        ``}`   `        ``modelGraphWithEdgeWeight(edge, E, V) {` `          ``let g = ``new` `Graph(V);` `          ``for` `(let i = 0; i < E; i++) {` `            ``// original edge : weight 0` `            ``g.addEdge(edge[i][0], edge[i][1], 0);`   `            ``// reverse edge : weight 1` `            ``g.addEdge(edge[i][1], edge[i][0], 1);` `          ``}` `          ``return` `g;` `        ``}`   `        ``getMinEdgeReversal(edge, E, V, source, destination) {` `          ``// get modified graph with edge weight.` `          ``let g = ``this``.modelGraphWithEdgeWeight(edge, E, V);`   `          ``// distance vector stores shortest path.` `          ``let dist = g.shortestPath(source);`   `          ``// If distance of destination is still INF then we cannot reach destination. Hence, not possible.` `          ``if` `(dist[destination] == INF) ``return` `-1;` `          ``else` `return` `dist[destination];` `        ``}` `      ``}`   `      ``let V = 7;` `      ``let g = ``new` `Graph(V);`   `      ``let edge = [` `        ``[0, 1],` `        ``[2, 1],` `        ``[2, 3],` `        ``[5, 1],` `        ``[4, 5],` `        ``[6, 4],` `        ``[6, 3],` `      ``];` `      ``let E = edge.length;`   `      ``let minEdgeToReverse = g.getMinEdgeReversal(edge, E, V, 0, 6);` `      ``if` `(minEdgeToReverse != -1) console.log(minEdgeToReverse);` `      ``else` `console.log(``"Not Possible."``);`

Output

`2`

Time complexity:
Therefore, the overall time complexity of the program is O(E*log(V)).

Space complexity:
Therefore, the overall space complexity of the program is O(E + V).

One more efficient approach to this problem would be by using 0-1 BFS concept

Below is the implementation of that algorithm:

## C++

 `// C++ code to find minimum edge reversal to get` `// atleast one path from source to destination using 0-1 BFS` `// Code By: Sparsh_CBS` `#include ` `using` `namespace` `std;`   `// Creating a node` `class` `Node {` `private``:` `    ``int` `val;` `    ``int` `weight;` `    ``int` `parent;`   `public``:` `    ``Node(``int` `val, ``int` `weight)` `    ``{` `        ``this``->val = val;` `        ``this``->weight = weight;` `        ``parent = -1;` `    ``}`   `    ``// We have used the concept of parent to avoid` `    ``// a child revisiting its parent and pushing it in` `    ``// the deque during the 0-1 BFS` `    ``Node(``int` `val, ``int` `distance, ``int` `parent)` `    ``{` `        ``this``->val = val;` `        ``this``->weight = distance;` `        ``this``->parent = parent;` `    ``}` `    ``int` `getVal() { ``return` `val; }` `    ``int` `getWeight() { ``return` `weight; }` `    ``int` `getParent() { ``return` `parent; }` `};`   `int` `getMinRevEdges(vector >& adj, ``int` `src,` `                   ``int` `dest)` `{` `    ``int` `n = adj.size();`   `    ``// Create the given graph into bidirectional graph` `    ``vector > newAdj(n);` `    ``for` `(``int` `i = 0; i < n; i++) {` `        ``for` `(``int` `j = 0; j < adj[i].size(); j++) {` `            ``int` `neighbour = adj[i][j];` `            ``// original edges are to be assigned a weight of` `            ``// 0` `            ``newAdj[i].push_back(Node(neighbour, 0));` `            ``// make a fake edge and assign a weight of 1` `            ``newAdj[neighbour].push_back(Node(i, 1));` `        ``}` `    ``}`   `    ``// Now, Apply 0-1 BFS using Deque to get the shortest` `    ``// path`   `    ``// In the implementation, we will only add the` `    ``// encountered node into the deque if and only if` `    ``// the distance at which it was earlier explored was` `    ``// strictly larger than the currently encountered` `    ``// distance` `    ``deque dq;` `    ``// Here Node is made up of : Node(int node_val, int` `    ``// node_distance, int node_parent)` `    ``dq.push_front(Node(src, 0, -1));` `    ``vector<``int``> dist(n, INT_MAX);` `    ``// Set the distance of all nodes to infinity(INT_MAX)` `    ``// set distance of source node as 0` `    ``dist[src] = 0;`   `    ``while` `(!dq.empty()) {` `        ``Node curr = dq.front();` `        ``dq.pop_front();` `        ``int` `currVal = curr.getVal();` `        ``int` `currWeight = curr.getWeight();` `        ``int` `currParent = curr.getParent();` `        ``// If we encounter the destination node, we return` `        ``if` `(currVal == dest)` `            ``return` `currWeight;` `        ``// Iterate over the neighbours of the current Node` `        ``for` `(``auto` `neighbourNode : newAdj[currVal]) {` `            ``int` `neighbour = neighbourNode.getVal();` `            ``if` `(neighbour == currParent)` `                ``continue``;`   `            ``int` `wt = neighbourNode.getWeight();` `            ``if` `(wt == 0 && dist[neighbour] > currWeight) {` `                ``dist[neighbour] = currWeight;` `                ``dq.push_front(` `                    ``Node(neighbour, currWeight, currVal));` `            ``}` `            ``else` `if` `(dist[neighbour] > currWeight + wt) {` `                ``dist[neighbour] = currWeight + wt;` `                ``dq.push_back(Node(` `                    ``neighbour, currWeight + wt, currVal));` `            ``}` `        ``}` `    ``}` `    ``return` `INT_MAX;` `}`   `// Driver code` `int` `main()` `{` `    ``vector > adj = { { 1 }, {},    { 1, 3 }, {},` `                                 ``{ 5 }, { 1 }, { 3, 4 } };` `    ``int` `ans = getMinRevEdges(adj, 0, 6);` `    ``if` `(ans == INT_MAX)` `        ``cout << -1;` `    ``else` `        ``cout << ans;` `    ``return` `0;` `}`

## Java

 `// Java code to find minimum edge reversal to get` `// atleast one path from source to destination using 0-1 BFS` `// Code By: Sparsh_CBS` `import` `java.util.*;`   `class` `Node {` `    ``private` `int` `val;` `    ``private` `int` `weight;` `    ``private` `Integer parent;` `    ``Node(``int` `val, ``int` `weight)` `    ``{` `        ``this``.val = val;` `        ``this``.weight = weight;` `        ``parent = ``null``;` `    ``}` `    ``// We have used the concept of parent to avoid` `    ``// a child revisiting its parent and pushing it in` `    ``// the deque during the 0-1 BFS` `    ``Node(``int` `val, ``int` `distance, Integer parent)` `    ``{` `        ``this``.val = val;` `        ``this``.weight = distance;` `        ``this``.parent = parent;` `    ``}`   `    ``public` `int` `getVal() { ``return` `val; }`   `    ``public` `int` `getWeight() { ``return` `weight; }`   `    ``public` `Integer getParent() { ``return` `parent; }` `}`   `public` `class` `Gfg {` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``List > adj = ``new` `ArrayList<>();` `        ``for` `(``int` `i = ``0``; i < ``7``; i++)` `            ``adj.add(``new` `ArrayList<>());`   `        ``adj.get(``0``).add(``1``);`   `        ``adj.get(``2``).add(``1``);`   `        ``adj.get(``5``).add(``1``);`   `        ``adj.get(``2``).add(``3``);`   `        ``adj.get(``6``).add(``3``);`   `        ``adj.get(``6``).add(``4``);`   `        ``adj.get(``4``).add(``5``);`   `        ``int` `ans = getMinRevEdges(adj, ``0``, ``6``);`   `        ``if` `(ans == Integer.MAX_VALUE)` `            ``System.out.println(-``1``);` `        ``else` `            ``System.out.println(ans);` `    ``}`   `    ``private` `static` `int` `    ``getMinRevEdges(List > adj, ``int` `src,` `                   ``int` `dest)` `    ``{` `        ``int` `n = adj.size();`   `        ``// Create the given graph into bidirectional graph` `        ``List > newAdj` `            ``= getBiDirectionalGraph(adj);`   `        ``// Now, Apply 0-1 BFS using Deque to get the` `        ``// shortest path`   `        ``// In the implementation, we will only add the` `        ``// encountered node into the deque if and only if` `        ``// the distance at which it was earlier explored was` `        ``// strictly larger than the currently encountered` `        ``// distance` `        ``Deque dq = ``new` `LinkedList<>();`   `        ``// Here Node is made up of : Node(int node_val, int` `        ``// node_distance, int node_parent)` `        ``dq.offer(``new` `Node(src, ``0``, ``0``));` `        ``int``[] dist = ``new` `int``[n];` `        ``// Set the distance of all nodes to` `        ``// infinity(Integer.MAX_VALUE)` `        ``Arrays.fill(dist, Integer.MAX_VALUE);` `        ``// set distance of source node as 0` `        ``dist[src] = ``0``;`   `        ``while` `(!dq.isEmpty()) {` `            ``Node curr = dq.pollFirst();` `            ``int` `currVal = curr.getVal();` `            ``int` `currWeight = curr.getWeight();` `            ``int` `currParent = curr.getParent();` `            ``// If we encounter the destination node, we` `            ``// return` `            ``if` `(currVal == dest)` `                ``return` `currWeight;` `            ``// Iterate over the neighbours of the current` `            ``// Node` `            ``for` `(Node neighbourNode : newAdj.get(currVal)) {` `                ``int` `neighbour = neighbourNode.getVal();` `                ``if` `(neighbour == currParent)` `                    ``continue``;`   `                ``int` `wt = neighbourNode.getWeight();` `                ``if` `(wt == ``0` `                    ``&& dist[neighbour] > currWeight) {` `                    ``dist[neighbour] = currWeight;` `                    ``dq.offerFirst(``new` `Node(` `                        ``neighbour, currWeight, currVal));` `                ``}` `                ``else` `if` `(dist[neighbour]` `                         ``> currWeight + wt) {` `                    ``dist[neighbour] = currWeight + wt;` `                    ``dq.offerLast(``new` `Node(neighbour,` `                                          ``currWeight + wt,` `                                          ``currVal));` `                ``}` `            ``}` `        ``}` `        ``return` `Integer.MAX_VALUE;` `    ``}`   `    ``private` `static` `List >` `    ``getBiDirectionalGraph(List > adj)` `    ``{` `        ``int` `n = adj.size();` `        ``List > newAdj = ``new` `ArrayList<>();`   `        ``for` `(``int` `i = ``0``; i < n; i++)` `            ``newAdj.add(``new` `ArrayList<>());`   `        ``boolean``[] visited = ``new` `boolean``[n];` `        ``Queue queue = ``new` `LinkedList<>();`   `        ``for` `(``int` `i = ``0``; i < n; i++) {` `            ``if` `(!visited[i]) {` `                ``visited[i] = ``true``;` `                ``queue.offer(i);`   `                ``while` `(!queue.isEmpty()) {` `                    ``int` `curr = queue.poll();` `                    ``for` `(``int` `neighbour : adj.get(curr)) {` `                        ``// original edges are to be assigned` `                        ``// a weight of 0` `                        ``newAdj.get(curr).add(` `                            ``new` `Node(neighbour, ``0``));` `                        ``// make a fake edge and assign a` `                        ``// weight of 1` `                        ``newAdj.get(neighbour).add(` `                            ``new` `Node(curr, ``1``));`   `                        ``if` `(visited[neighbour]) {` `                            ``// if the neighbour was visited,` `                            ``// then dont` `                            ``// add it again in the queue` `                            ``continue``;` `                        ``}` `                        ``visited[neighbour] = ``true``;` `                        ``queue.offer(neighbour);` `                    ``}` `                ``}` `            ``}` `        ``}` `        ``return` `newAdj;` `    ``}` `}`

## Python3

 `# Python Code to find minimum edge reversal to get` `# atleast one path from source to destination using 0-1 BFS` `# Code By: Sparsh_CBS`     `class` `Node:` `    ``def` `__init__(``self``, val, weight, parent``=``None``):` `        ``self``.val ``=` `val` `        ``self``.weight ``=` `weight` `        ``self``.parent ``=` `parent`     `def` `getMinRevEdges(adj, src, dest):` `    ``n ``=` `len``(adj)` `    ``# Create the given graph into bidirectional graph` `    ``newAdj ``=` `getBiDirectionalGraph(adj)` `    ``# Now, Apply 0-1 BFS using Deque to get the shortest path` `    ``dq ``=` `[]` `    ``dq.append(Node(src, ``0``, ``0``))` `    ``dist ``=` `[``float``(``"inf"``)] ``*` `n` `    ``# Set the distance of all nodes to infinity(Integer.MAX_VALUE)` `    ``dist[src] ``=` `0` `    ``# set distance of source node as 0` `    ``while` `len``(dq):` `        ``curr ``=` `dq.pop(``0``)` `        ``currVal ``=` `curr.val` `        ``currWeight ``=` `curr.weight` `        ``currParent ``=` `curr.parent` `        ``# If we encounter the destination node, we return` `        ``if` `currVal ``=``=` `dest:` `            ``return` `currWeight` `        ``# Iterate over the neighbours of the current Node` `        ``for` `neighbourNode ``in` `newAdj[currVal]:` `            ``neighbour ``=` `neighbourNode.val` `            ``if` `neighbour ``=``=` `currParent:` `                ``continue` `            ``wt ``=` `neighbourNode.weight` `            ``if` `wt ``=``=` `0` `and` `dist[neighbour] > currWeight:` `                ``dist[neighbour] ``=` `currWeight` `                ``dq.append(Node(neighbour, currWeight, currVal))` `            ``elif` `dist[neighbour] > currWeight ``+` `wt:` `                ``dist[neighbour] ``=` `currWeight ``+` `wt` `                ``dq.append(Node(neighbour, currWeight ``+` `wt, currVal))` `    ``return` `float``(``"inf"``)`     `def` `getBiDirectionalGraph(adj):` `    ``n ``=` `len``(adj)` `    ``newAdj ``=` `[[] ``for` `_ ``in` `range``(n)]` `    ``visited ``=` `[``False``] ``*` `n` `    ``queue ``=` `[]` `    ``for` `i ``in` `range``(n):` `        ``if` `not` `visited[i]:` `            ``visited[i] ``=` `True` `            ``queue.append(i)` `            ``while` `len``(queue):` `                ``curr ``=` `queue.pop(``0``)` `                ``for` `neighbour ``in` `adj[curr]:` `                    ``# original edges are to be assigned a weight of 0` `                    ``newAdj[curr].append(Node(neighbour, ``0``))` `                    ``# make a fake edge and assign a weight of 1` `                    ``newAdj[neighbour].append(Node(curr, ``1``))` `                    ``if` `visited[neighbour]:` `                        ``continue`  `# if the neighbour was visited, then dont` `                    ``# add it again in the queue` `                    ``visited[neighbour] ``=` `True` `                    ``queue.append(neighbour)` `    ``return` `newAdj`     `if` `__name__ ``=``=` `"__main__"``:` `    ``adj ``=` `[[] ``for` `_ ``in` `range``(``7``)]` `    ``adj[``0``].append(``1``)` `    ``adj[``2``].append(``1``)` `    ``adj[``5``].append(``1``)` `    ``adj[``2``].append(``3``)` `    ``adj[``6``].append(``3``)` `    ``adj[``6``].append(``4``)` `    ``adj[``4``].append(``5``)`   `    ``ans ``=` `getMinRevEdges(adj, ``0``, ``6``)`   `    ``if` `ans ``=``=` `float``(``"inf"``):` `        ``print``(``-``1``)` `    ``else``:` `        ``print``(ans)`

## C#

 `using` `System;` `using` `System.Collections.Generic;`   `class` `Node {` `    ``private` `int` `val;` `    ``private` `int` `weight;` `    ``private` `int` `? parent;` `    ``public` `Node(``int` `val, ``int` `weight)` `    ``{` `        ``this``.val = val;` `        ``this``.weight = weight;` `        ``parent = ``null``;` `    ``}` `    ``// We have used the concept of parent to avoid` `    ``// a child revisiting its parent and pushing it in` `    ``// the deque during the 0-1 BFS` `    ``public` `Node(``int` `val, ``int` `distance, ``int``? parent)` `    ``{` `        ``this``.val = val;` `        ``this``.weight = distance;` `        ``this``.parent = parent;` `    ``}`   `    ``public` `int` `GetVal() { ``return` `val; }`   `    ``public` `int` `GetWeight() { ``return` `weight; }`   `    ``public` `int` `? GetParent() { ``return` `parent; }` `}`   `public` `class` `Gfg {` `    ``public` `static` `void` `Main(``string``[] args)` `    ``{` `        ``List > adj = ``new` `List >();` `        ``for` `(``int` `i = 0; i < 7; i++)` `            ``adj.Add(``new` `List<``int``>());`   `        ``adj[0].Add(1);`   `        ``adj[2].Add(1);`   `        ``adj[5].Add(1);`   `        ``adj[2].Add(3);`   `        ``adj[6].Add(3);`   `        ``adj[6].Add(4);`   `        ``adj[4].Add(5);`   `        ``int` `ans = GetMinRevEdges(adj, 0, 6);`   `        ``if` `(ans == ``int``.MaxValue)` `            ``Console.WriteLine(-1);` `        ``else` `            ``Console.WriteLine(ans);` `    ``}`   `    ``private` `static` `int` `GetMinRevEdges(List > adj,` `                                      ``int` `src, ``int` `dest)` `    ``{` `        ``int` `n = adj.Count;`   `        ``// Create the given graph into bidirectional graph` `        ``List > newAdj` `            ``= GetBiDirectionalGraph(adj);`   `        ``// Now, Apply 0-1 BFS using Deque to get the` `        ``// shortest path`   `        ``// In the implementation, we will only add the` `        ``// encountered node into the deque if and only if` `        ``// the distance at which it was earlier explored was` `        ``// strictly larger than the currently encountered` `        ``// distance` `        ``Queue dq = ``new` `Queue();`   `        ``// Here Node is made up of : Node(int node_val, int` `        ``// node_distance, int? node_parent)` `        ``dq.Enqueue(``new` `Node(src, 0, ``null``));` `        ``int``[] dist = ``new` `int``[n];` `        ``// Set the distance of all nodes to` `        ``// infinity(int.MaxValue)` `        ``Array.Fill(dist, ``int``.MaxValue);` `        ``// set distance of source node as 0` `        ``dist[src] = 0;`   `        ``while` `(dq.Count > 0) {` `            ``Node curr = dq.Dequeue();` `            ``int` `currVal = curr.GetVal();` `            ``int` `currWeight = curr.GetWeight();` `            ``int` `? currParent = curr.GetParent();` `            ``// If we encounter the destination node, we` `            ``// return` `            ``if` `(currVal == dest)` `                ``return` `currWeight;` `            ``// Iterate over the neighbours of the current` `            ``// Node` `            ``foreach``(Node neighbourNode ``in` `newAdj[currVal])` `            ``{` `                ``int` `neighbour = neighbourNode.GetVal();` `                ``if` `(neighbour == currParent)` `                    ``continue``;`   `                ``int` `wt = neighbourNode.GetWeight();` `                ``if` `(wt == 0` `                    ``&& dist[neighbour] > currWeight) {` `                    ``dist[neighbour] = currWeight;` `                    ``dq.Enqueue(``new` `Node(` `                        ``neighbour, currWeight, currVal));` `                ``}` `                ``else` `if` `(dist[neighbour]` `                         ``> currWeight + wt) {` `                    ``dist[neighbour] = currWeight + wt;` `                    ``dq.Enqueue(``new` `Node(neighbour,` `                                        ``currWeight + wt,` `                                        ``currVal));` `                ``}` `            ``}` `        ``}` `        ``// If destination is not reachable from source,` `        ``// return -1` `        ``return` `int``.MaxValue;` `    ``}`   `    ``// Utility function to convert given graph into` `    ``// bidirectional graph` `    ``private` `static` `List >` `    ``GetBiDirectionalGraph(List > adj)` `    ``{` `        ``int` `n = adj.Count;` `        ``List > newAdj = ``new` `List >();`   `        ``for` `(``int` `i = 0; i < n; i++)` `            ``newAdj.Add(``new` `List());`   `        ``for` `(``int` `i = 0; i < n; i++) {` `            ``foreach``(``int` `neighbour ``in` `adj[i])` `            ``{` `                ``newAdj[i].Add(``new` `Node(neighbour, 0));` `                ``newAdj[neighbour].Add(``new` `Node(i, 1));` `            ``}` `        ``}`   `        ``return` `newAdj;` `    ``}` `}`

## Javascript

 `// JavaScript Code to find minimum edge reversal to get` `// atleast one path from source to destination using 0-1 BFS` `// Code By: Sparsh_CBS` `class Node {` `    ``constructor(val, weight, parent = ``null``) {` `        ``this``.val = val;` `        ``this``.weight = weight;` `        ``this``.parent = parent;` `    ``}` `}`   `function` `getMinRevEdges(adj, src, dest) {` `    ``let n = adj.length;` `    ``// Create the given graph into bidirectional graph` `    ``let newAdj = getBiDirectionalGraph(adj);` `    ``// Now, Apply 0-1 BFS using Deque to get the shortest path` `    ``let dq = [];` `    ``dq.push(``new` `Node(src, 0, 0));` `    ``let dist = ``new` `Array(n).fill(Number.MAX_VALUE);` `    ``// Set the distance of all nodes to infinity(Number.MAX_VALUE)` `    ``dist[src] = 0;` `    ``// set distance of source node as 0` `    ``while` `(dq.length) {` `        ``let curr = dq.shift();` `        ``let currVal = curr.val;` `        ``let currWeight = curr.weight;` `        ``let currParent = curr.parent;` `        ``// If we encounter the destination node, we return` `        ``if` `(currVal == dest) {` `            ``return` `currWeight;` `        ``}` `        ``// Iterate over the neighbours of the current Node` `        ``for` `(let neighbourNode of newAdj[currVal]) {` `            ``let neighbour = neighbourNode.val;` `            ``if` `(neighbour == currParent) {` `                ``continue``;` `            ``}` `            ``let wt = neighbourNode.weight;` `            ``if` `(wt == 0 && dist[neighbour] > currWeight) {` `                ``dist[neighbour] = currWeight;` `                ``dq.push(``new` `Node(neighbour, currWeight, currVal));` `            ``} ``else` `if` `(dist[neighbour] > currWeight + wt) {` `                ``dist[neighbour] = currWeight + wt;` `                ``dq.push(``new` `Node(neighbour, currWeight + wt, currVal));` `            ``}` `        ``}` `    ``}` `    ``return` `Number.MAX_VALUE;` `}`   `function` `getBiDirectionalGraph(adj) {` `    ``let n = adj.length;` `    ``let newAdj = ``new` `Array(n);` `    ``for` `(let i = 0; i < n; i++) {` `        ``newAdj[i] = [];` `    ``}` `    ``let visited = ``new` `Array(n).fill(``false``);` `    ``let queue = [];` `    ``for` `(let i = 0; i < n; i++) {` `        ``if` `(!visited[i]) {` `            ``visited[i] = ``true``;` `            ``queue.push(i);` `            ``while` `(queue.length) {` `                ``let curr = queue.shift();` `                ``for` `(let neighbour of adj[curr]) {` `                    ``// original edges are to be assigned a weight of 0` `                    ``newAdj[curr].push(``new` `Node(neighbour, 0));` `                    ``// make a fake edge and assign a weight of 1` `                    ``newAdj[neighbour].push(``new` `Node(curr, 1));` `                    ``if` `(visited[neighbour]) {` `                        ``continue``; ``// if the neighbour was visited, then dont add it again in the queue` `                    ``}` `                    ``visited[neighbour] = ``true``;` `                    ``queue.push(neighbour);` `                ``}` `            ``}` `        ``}` `    ``}` `    ``return` `newAdj;` `}`   `let adj = [` `    ``[],` `    ``[],` `    ``[],` `    ``[],` `    ``[],` `    ``[],` `    ``[]` `];` `adj[0].push(1);` `adj[2].push(1);` `adj[5].push(1);` `adj[2].push(3);` `adj[6].push(3);` `adj[6].push(4);` `adj[4].push(5);`   `let ans = getMinRevEdges(adj, 0, 6);`   `if` `(ans == Number.MAX_VALUE) {` `    ``console.log(-1);` `} ``else` `{` `    ``console.log(ans);` `}`

Output

`2`

Time Complexity: O(V+E)
Space Complexity: O(V+2*E)

If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next