Shortest Path with Alternating Colors - Problem

You are given an integer n, the number of nodes in a directed graph where the nodes are labeled from 0 to n - 1. Each edge is red or blue in this graph, and there could be self-edges and parallel edges.

You are given two arrays redEdges and blueEdges where:

  • redEdges[i] = [a_i, b_i] indicates that there is a directed red edge from node a_i to node b_i in the graph
  • blueEdges[j] = [u_j, v_j] indicates that there is a directed blue edge from node u_j to node v_j in the graph

Return an array answer of length n, where each answer[x] is the length of the shortest path from node 0 to node x such that the edge colors alternate along the path, or -1 if such a path does not exist.

Input & Output

Example 1 — Basic Alternating Path
$ Input: n = 3, redEdges = [[0,1],[1,2]], blueEdges = []
Output: [0,1,-1]
💡 Note: From node 0: reach node 1 via red edge (distance 1), cannot reach node 2 (would need blue edge after red, but no blue edges exist)
Example 2 — Multiple Alternating Paths
$ Input: n = 3, redEdges = [[0,1]], blueEdges = [[1,2]]
Output: [0,1,2]
💡 Note: From node 0: reach node 1 via red edge (distance 1), reach node 2 via red then blue edge (distance 2, colors alternate properly)
Example 3 — Complex Graph with Multiple Options
$ Input: n = 3, redEdges = [[0,1],[0,2]], blueEdges = [[1,0]]
Output: [0,1,1]
💡 Note: From node 0: direct red edges to nodes 1 and 2 (both distance 1). Node 1 can be reached via 0→1 (red), and node 2 can be reached via 0→2 (red). The blue edge from 1 to 0 creates a cycle but doesn't provide shorter paths to new nodes.

Constraints

  • 1 ≤ n ≤ 100
  • 0 ≤ redEdges.length, blueEdges.length ≤ 400
  • redEdges[i].length == blueEdges[j].length == 2
  • 0 ≤ ai, bi, uj, vj < n

Visualization

Tap to expand
Shortest Path with Alternating Colors INPUT Directed Graph (n=3 nodes) 0 1 2 RED RED n = 3 redEdges = [[0,1],[1,2]] blueEdges = [] Legend: Red Edge Blue Edge ALGORITHM (BFS) 1 Initialize Start BFS from node 0 Track (node, lastColor) 2 Process Queue For each node, try edges of opposite color 3 Alternate Colors Red edge must follow blue and vice versa 4 Record Distances Update answer[x] with shortest path found BFS Exploration: Queue: (0,none) dist=0 0 --R--> 1 dist=1 OK 1 --R--> 2 BLOCKED! (need blue edge, none exist) Node 2: unreachable = -1 FINAL RESULT Path Distances from Node 0 0 dist: 0 1 dist: 1 2 dist: -1 X Output Array: 0 1 -1 [0] [1] [2] [0, 1, -1] Node 2 unreachable via alternating path (no blue edges) OK - Answer Verified Key Insight: BFS explores paths level by level, ensuring we find the shortest path first. We track states as (node, lastEdgeColor) to enforce color alternation. Without blue edges, node 2 requires two consecutive red edges (0--R-->1--R-->2), which violates the alternating constraint, making it unreachable with value -1. TutorialsPoint - Shortest Path with Alternating Colors | BFS Approach
Asked in
Google 15 Facebook 12 Amazon 8
23.4K Views
Medium Frequency
~25 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen