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
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Minimum cost path from source node to destination node via an intermediate node
- Minimum edges to be added in a directed graph so that any node can be reachable from a given node
- Kth largest node among all directly connected nodes to the given node in an undirected graph
- Level of Each node in a Tree from source node (using BFS)
- Check if given path between two nodes of a graph represents a shortest paths
- XOR of all the nodes in the sub-tree of the given node
- Queries for M-th node in the DFS of subtree
- Find the node whose xor with x gives minimum value
- Find the node whose xor with x gives maximum value
- Sum of the distances from every node to all other nodes is maximum
- Find the node whose sum with X has maximum set bits
- Find the node whose sum with X has minimum set bits
- Print the degree of every node from the given Prufer sequence
- Find the node whose absolute difference with X gives minimum value
- Find all reachable nodes from every node present in a given set
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.