Find the fewest edge reversals to travel from node 0 to node n-1 in a directed graph

Quick Overview

Find the minimum cost to travel from node 0 to node n-1 in a directed graph when following an edge is free and using an edge against its direction costs 1. It tests turning a reversal rule into edge weights and picking an efficient shortest path method for graphs with up to 200,000 edges.

Find the fewest edge reversals to travel from node 0 to node n-1 in a directed graph

Company: Discord

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

A directed graph has `n` nodes labeled `0` to `n - 1`. Each entry `edges[i] = [u, v]` is a directed edge from node `u` to node `v`. Moving along an edge in its own direction (from `u` to `v`) costs `0`. You may also reverse an edge and move along it the other way (from `v` to `u`) at a cost of `1`. Return the minimum total cost to travel from node `0` to node `n - 1`. If node `n - 1` cannot be reached from node `0` even with reversals, return `-1`. ### Function Signature ```python def min_reversal_cost(n: int, edges: list[list[int]]) -> int: ``` ### Rules - `edges` is a list of `[u, v]` pairs. Each pair is one directed edge from `u` to `v`. - The cost of a path is the number of edges on it that are used against their direction. Edges used in their own direction are free. - Multiple edges between the same two nodes are separate edges. - Return a single integer: the minimum cost, or `-1` when no path exists. ### Constraints - `2 <= n <= 10^5` - `0 <= len(edges) <= 2 * 10^5` - `len(edges[i]) == 2` - `0 <= edges[i][0], edges[i][1] < n` - `edges[i][0] != edges[i][1]` - There may be multiple edges between two nodes. ### Examples ```text Input: n = 5, edges = [[0,1],[1,2],[3,2],[3,4]] Output: 1 ``` The path goes 0 to 1 to 2 for free. Node 2 has no outgoing edge, so the path uses edge `3 -> 2` backward to reach node 3 (cost 1) and then `3 -> 4` forward. A cost of 0 is impossible because nodes 0, 1 and 2 cannot reach 3 or 4 by forward edges alone. ```text Input: n = 3, edges = [[2,1],[1,0]] Output: 2 ``` The only route from 0 to 2 passes through 1, and both edges on it point toward node 0. Each one is used backward, for a total of 2. ```text Input: n = 4, edges = [[0,1],[2,3]] Output: -1 ``` Even with every edge allowed in both directions, nodes 0 and 1 are not connected to nodes 2 and 3, so node 3 cannot be reached.

Overview: Find the minimum cost to travel from node 0 to node n-1 in a directed graph when following an edge is free and using an edge against its direction costs 1. It tests turning a reversal rule into edge weights and picking an efficient shortest path method for graphs with up to 200,000 edges.

Discord logo
Discord
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

A directed graph has n nodes labeled 0 to n - 1. Each entry edges[i] = [u, v] is a directed edge from node u to node v.

Moving along an edge in its own direction (from u to v) costs 0. You may also reverse an edge and move along it the other way (from v to u) at a cost of 1.

Return the minimum total cost to travel from node 0 to node n - 1. If node n - 1 cannot be reached from node 0 even with reversals, return -1.

Function Signature

def min_reversal_cost(n: int, edges: list[list[int]]) -> int:

Rules

  • edges is a list of [u, v] pairs. Each pair is one directed edge from u to v .
  • The cost of a path is the number of edges on it that are used against their direction. Edges used in their own direction are free.
  • Multiple edges between the same two nodes are separate edges.
  • Return a single integer: the minimum cost, or -1 when no path exists.

Constraints

  • 2 <= n <= 10^5
  • 0 <= len(edges) <= 2 * 10^5
  • len(edges[i]) == 2
  • 0 <= edges[i][0], edges[i][1] < n
  • edges[i][0] != edges[i][1]
  • There may be multiple edges between two nodes.

Examples

Input: n = 5, edges = [[0,1],[1,2],[3,2],[3,4]]
Output: 1

The path goes 0 to 1 to 2 for free. Node 2 has no outgoing edge, so the path uses edge 3 -> 2 backward to reach node 3 (cost 1) and then 3 -> 4 forward. A cost of 0 is impossible because nodes 0, 1 and 2 cannot reach 3 or 4 by forward edges alone.

Input: n = 3, edges = [[2,1],[1,0]]
Output: 2

The only route from 0 to 2 passes through 1, and both edges on it point toward node 0. Each one is used backward, for a total of 2.

Input: n = 4, edges = [[0,1],[2,3]]
Output: -1

Even with every edge allowed in both directions, nodes 0 and 1 are not connected to nodes 2 and 3, so node 3 cannot be reached.