Given adjacency list representation of a directed graph, the task is to check whether the cost of going from any vertex to any other vertex via all possible paths is equal or not. If there is a cost c for going from vertex A to vertex B, then the cost to travel from vertex B to vertex A will be -c.
Here the cost of going from any node to any other node is equal for all possible paths. For example, if we go from 1 to 4 via (1 -> 2 -> 3 -> 4) whose cost is (2 + 1 + (-2)) i.e. 1 and via ( 1 -> 4 ) which is a reverse edge whose cost is 1. Similarly, the cost for all other paths is equal.
For the following two paths from edge 1 to 4, (1 -> 2 -> 3 -> 4), Cost = (1 + 3 + 1) = 5 and (1 -> 4), Cost = 3. Since the costs are difference, the answer is No
Approach: The idea is to maintain two arrays dis which maintains the distance of the paths travelled and visited which maintains the visited vertices. The graph is stored using 2D vector of pairs. The first value of the pair is the destination node and the second value is the cost associated with it. Now, DFS is run on the graph. The following two conditions occur for every vertex:
- If the next node to reach is not visited, then the dis array is updated with the value dis[current_node] + cost of the new edge found from the 2D vector i.e. current node to next node to reach and the same function is called with the unvisited same node.
- If the node is visited, then the distance of the next node to reach is compared with the dis[curr] + cost of edge to reach the next node. If they are equal, then the boolean variable flag is updated with true and the loop is continued for the next vertices.
Below is the implementation of the above approach:
Time Complexity: O(V + E) where V is no of vertices and E is no of edges
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimum cost path from source node to destination node via an intermediate node
- Minimum Cost Path in a directed graph via given set of intermediate nodes
- Count of all possible Paths in a Tree such that Node X does not appear before Node Y
- Count all possible paths between two vertices
- Print all possible paths from the first row to the last row in a 2D array
- Print all possible paths in a DAG from vertex whose indegree is 0
- Count number of times each Edge appears in all possible paths of a given Tree
- Minimum Cost using Dijkstra by reducing cost of an Edge
- Find whether it is possible to finish all tasks or not from given dependencies
- Minimum number of reversals to reach node 0 from every other node
- Sum of the distances from every node to all other nodes is maximum
- Check if given path between two nodes of a graph represents a shortest paths
- Johnson's algorithm for All-pairs shortest paths
- Print all paths from a given source to a destination
- Print all paths from a given source to a destination using BFS
- Johnson’s algorithm for All-pairs shortest paths | Implementation
- Shortest paths from all vertices to a destination
- Print all shortest paths between given source and destination in an undirected graph
- Sum of all pair shortest paths in a Tree
- Print node whose each neighboring Tree has all nodes of same color
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.