## Saturday, November 12, 2016

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

http://www.geeksforgeeks.org/minimum-edges-reverse-make-path-source-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 source node to destination node.
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.

`// This class represents a directed graph using`
`// adjacency list representation`
`class` `Graph`
`{`
`    ``int` `V;    ``// No. of vertices`

`    ``// In a weighted graph, we need to store vertex`
`    ``// and weight pair for every edge`
`    ``list< pair<``int``, ``int``> > *adj;`

`public``:`
`    ``Graph(``int` `V);  ``// Constructor`

`    ``// function to add an edge to graph`
`    ``void` `addEdge(``int` `u, ``int` `v, ``int` `w);`

`    ``// returns shortest path from s`
`    ``vector<``int``> shortestPath(``int` `s);`
`};`

`// Allocates memory for adjacency list`
`Graph::Graph(``int` `V)`
`{`
`    ``this``->V = V;`
`    ``adj = ``new` `list< pair<``int``, ``int``> >[V];`
`}`

`//  method adds a directed edge from u to v with weight w`
`void` `Graph::addEdge(``int` `u, ``int` `v, ``int` `w)`
`{`
`    ``adj[u].push_back(make_pair(v, w));`
`}`

`// Prints shortest paths from src to all other vertices`
`vector<``int``> Graph::shortestPath(``int` `src)`
`{`
`    ``// Create a set to store vertices that are being`
`    ``// prerocessed`
`    ``set< pair<``int``, ``int``> > setds;`

`    ``// Create a vector for distances and initialize all`
`    ``// distances as infinite (INF)`
`    ``vector<``int``> dist(V, INF);`

`    ``// Insert source itself in Set and initialize its`
`    ``// distance as 0.`
`    ``setds.insert(make_pair(0, src));`
`    ``dist[src] = 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;`

`        ``// 'i' is used to get all adjacent vertices of a vertex`
`        ``list< pair<``int``, ``int``> >::iterator i;`
`        ``for` `(i = adj[u].begin(); i != adj[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` `(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.erase(setds.find(make_pair(dist[v], v)));`

`                ``// Updating distance of v`
`                ``dist[v] = dist[u] + weight;`
`                ``setds.insert(make_pair(dist[v], v));`
`            ``}`
`        ``}`
`    ``}`
`    ``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. */`
`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;`
`}`

`// Method returns minimum number of edges to be`
`// reversed to reach from src to dest`
`int` `getMinEdgeReversal(``int` `edge[][2], ``int` `E, ``int` `V,`
`                       ``int` `src, ``int` `dest)`
`{`
`    ``//  get modified graph with edge weight`
`    ``Graph g = modelGraphWithEdgeWeight(edge, E, V);`

`    ``//  get shortes path vector`
`    ``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];`
`}`