Problem of the day
You are given a directed graph of ‘N’ nodes and ‘M’ edges. Also, you have two nodes ‘A’ and ‘B’. Your task is to make at least one valid path from ‘A’ to ‘B’ by doing the below operations a minimum number of times.
Choose two nodes ‘X’ and ‘Y’, such that there exists an edge from ‘X’ to ‘Y’.
Delete edge ‘X’ to ‘Y’.
Add edge ‘Y’ to ‘X’.
You need to print the minimum operations that you have done.
The first line of input contains an integer 'T' representing the number of test cases.
The first line of each test case contains two space-separated integers ‘N’ and ‘M’ where ‘N’ denotes the total number of nodes and ‘M’ denotes the total number of edges in the graph.
The second line of each test case contains two space-separated integers ‘X’ and ‘Y’ where ‘X’ denotes the starting node of the path and ‘Y’ denotes the ending node of the path.
The next ‘M’ lines contain two space-separated integers ‘U’ and ‘V’ where ‘U’ and ‘V’ represent the nodes that share an edge from ‘U’ to ‘V’.
Output Format:
For each test case, print the minimum number of operations required to make at least one valid path from ‘A’ to ‘B’.
The output of each test case will be printed in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N <= 3000
1 <= M <= min(3000, N*(N-1)/2)
0 <= X, Y, U, V <= N - 1
Time Limit : 1 sec
2
4 3
0 3
0 1
0 2
3 2
5 4
0 4
0 1
1 3
4 1
2 0
1
1
Test Case 1 :
The given graph is shown below.
Starting node = 0 and Ending node = 3.
We can reverse edge 3 -> 2 to make a valid path of 0 -> 2 -> 3.
So, the minimum number of operations required will be 1.
Test Case 2 :
The given graph is shown below.
Starting node = 0 and Ending node = 4.
We can reverse edge 4 -> 1 to make a valid path of 0 -> 1 -> 4.
So the minimum number of operations required will be 1.
2
5 4
0 4
0 1
0 2
2 3
4 3
6 6
0 5
0 1
0 2
2 3
2 4
4 5
5 3
1
0
Assign weight 1 to all edges which you have reversed and calculate the shortest path from starting node to ending node.
The idea here is to make the graph undirected by adding all edges in the reverse direction. We will assign weight zero to all edges that are not reversed and weight 1 to all edges that are reversed. Then we will use the Dijkstra algorithm to find the minimum weight path from starting node to the ending node to get the minimum number of operations.
For example:
Below left side directed graph will be converted into right side undirected graph
Then we will find the minimum weight path in the right-side graph using the Dijkstra algorithm.
Algorithm:
O(M * log (N)), where ‘N’ is the total number of nodes and ‘M’ is the total number of edges in the graph.
Here, building a graph takes O(M) time as we need to add ‘M’ edges to the graph. Also, our dijkstra algorithm takes O(M * log (N) ) as we need to visit all M edges and update distance M times. Also erasing an element from the set takes O( log(N) ) time, and we need to do this O(M) time in the worst case.
Hence, the overall time complexity will be O(M * log(N) ) + O(M) = O(M * log (N) ).
O(N + M), where ‘N’ is the total number of nodes and ‘M’ is the total number of edges in the graph.
We are using a distance array that takes O(N) space. Also, our set data structure requires O(N) space as we can have at most ‘N’ vertices in the set. Also, we are using an adjacency list that takes O(M) space as we need to insert ‘M’ edges into the list.
Hence, the overall space complexity will be O(N) + O(N) + O(M) = O(N + M).