Reverse Edges Problem Statement
You are given a directed graph with ‘N’ nodes and ‘M’ edges, along with two specific nodes, ‘A’ and ‘B’. Your task is to find the minimum number of operations required to create at least one valid path from ‘A’ to ‘B’. The available operation involves reversing the direction of an edge.
Input:
The first input line contains an integer 'T', the number of test cases.
For each test case:
- The first line contains two space-separated integers ‘N’ (total nodes) and ‘M’ (total edges).
- The second line contains two space-separated integers ‘X’ (starting node) and ‘Y’ (ending node).
- The following ‘M’ lines each contain two space-separated integers ‘U’ and ‘V’, denoting a directed edge from ‘U’ to ‘V’.
Output:
For each test case, output the minimum number of edge reversals necessary to create a path from ‘A’ to ‘B’. Print the result for each test case on a new line.
Example:
Suppose you have a graph of 3 nodes and 3 edges, and the edges are as follows: (0, 1), (1, 2), (2, 0)
. To create a path from node ‘0’ (A) to node ‘2’ (B) with minimum operations, you need to reverse the edge (2, 0) to (0, 2). Therefore, the number of operations is 1.
Constraints:
1 ≤ T ≤ 5
1 ≤ N ≤ 3000
1 ≤ M ≤ min(3000, N*(N-1)/2)
0 ≤ X, Y, U, V ≤ N - 1
Note: You are not required to print anything; the printing is handled by the system. Implement the function to return the correct result.
The task is to find the minimum number of operations required to create a valid path from node A to node B by reversing edges in a directed graph.
Iterate through the graph to find the shortest path fr...read more
Top Nutanix Mts1 interview questions & answers
Popular interview questions of Mts1
Reviews
Interviews
Salaries
Users/Month