Dado un gráfico dirigido y un Node de origen y un Node de destino, necesitamos encontrar cuántos bordes necesitamos invertir para hacer al menos 1 ruta desde el Node de origen hasta el Node de destino.
Ejemplos:
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.
Este problema se puede resolver asumiendo una versión diferente del gráfico dado. En esta versión, hacemos un borde inverso correspondiente a cada borde y le asignamos un peso 1 y asignamos un peso 0 al borde original. Después de esta modificación, el gráfico anterior se parece a lo siguiente,
Ahora podemos ver que hemos modificado el gráfico de tal manera que, si nos movemos hacia el borde original, no se incurre en ningún costo, pero si nos movemos hacia el borde inverso, se agrega 1 costo. Entonces, si aplicamos la ruta más corta de Dijkstra en este gráfico modificado desde una fuente dada, entonces eso nos dará un costo mínimo para llegar desde la fuente hasta el destino, es decir, una inversión de borde mínima desde la fuente hasta el destino.
A continuación se muestra el código basado en el concepto anterior.
C++
// C++ Program to find minimum edge reversal to get // atleast one path from source to destination #include <bits/stdc++.h> using namespace std; #define INF 0x3f3f3f3f // This class represents a directed graph using // adjacency list representation class Graph { int V; list<pair<int, int>> *graph; public: // Constructor: Graph(int V) { this->V = V; graph = new list<pair<int, int>>[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<pair<int, int>> 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<pair<int, int>>::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<Pair>[] 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<Pair> setds = new HashSet<Pair>(); // 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<Pair> 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<int, int> >::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
Producción:
2
Un enfoque más eficiente para este problema sería utilizar el concepto 0-1 BFS .
A continuación se muestra la implementación de ese algoritmo:
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<List<Integer>> 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<List<Integer>> adj, int src, int dest){ int n = adj.size(); //Create the given graph into bidirectional graph List<List<Node>> 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<Node> 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<List<Node>> getBiDirectionalGraph(List<List<Integer>> adj){ int n = adj.size(); List<List<Node>> newAdj = new ArrayList<>(); for(int i = 0; i < n; i++) newAdj.add(new ArrayList<>()); boolean[] visited = new boolean[n]; Queue<Integer> 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; } }
Producción:
2
Complejidad temporal: O(V+E)
Complejidad espacial: O(V+2*E)
Este artículo es una contribución de Utkarsh Trivedi . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA