Asked inNutanix,Mts1

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.

AnswerBot
6d

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

Help your peers!
Add answer anonymously...
Nutanix Mts1 Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter