Shortest Path with Alternating Colors - Problem

Imagine you're navigating through a city where every street is painted either red or blue, and you must follow a special traffic rule: you can only alternate between red and blue streets!

Given a directed graph with n nodes (labeled 0 to n-1), you have two types of edges:

  • Red edges: represented by redEdges[i] = [from, to]
  • Blue edges: represented by blueEdges[i] = [from, to]

Your mission is to find the shortest alternating path from node 0 to every other node. An alternating path means you must switch between red and blue edges at each step.

Goal: Return an array where answer[i] is the shortest path length from node 0 to node i following the alternating color rule, or -1 if impossible.

Input & Output

example_1.py โ€” Basic Alternating Path
$ Input: n = 3, redEdges = [[0,1],[1,2]], blueEdges = []
โ€บ Output: [0,1,-1]
๐Ÿ’ก Note: Node 0 is at distance 0. We can reach node 1 via red edge (distance 1). Node 2 cannot be reached because we need a blue edge from node 1, but none exists.
example_2.py โ€” Multiple Paths Available
$ Input: n = 3, redEdges = [[0,1]], blueEdges = [[2,1]]
โ€บ Output: [0,-1,-1]
๐Ÿ’ก Note: Only node 0 is reachable. Node 1 requires alternating colors but we can't reach it from 0 with proper alternation. Node 2 has no incoming edges from reachable nodes.
example_3.py โ€” Complex Graph with Alternating Paths
$ Input: n = 3, redEdges = [[0,1]], blueEdges = [[1,2]]
โ€บ Output: [0,1,2]
๐Ÿ’ก Note: Node 0 at distance 0. Reach node 1 via red edge (distance 1). Then reach node 2 via blue edge from node 1 (distance 2). Perfect alternating pattern: red โ†’ blue.

Constraints

  • 1 โ‰ค n โ‰ค 100
  • 0 โ‰ค redEdges.length, blueEdges.length โ‰ค 400
  • redEdges[i].length == blueEdges[i].length == 2
  • 0 โ‰ค ai, bi, uj, vj < n
  • Self-loops and parallel edges are allowed

Visualization

Tap to expand
๐Ÿšฆ Smart City Navigation System๐Ÿข 0City Center๐Ÿฌ 1๐Ÿช 2๐Ÿ›๏ธ 3๐Ÿจ 4๐ŸŽฏ Navigation Rulesโ€ข Start from City Center (0)โ€ข Red street โ†’ Blue streetโ€ข Blue street โ†’ Red streetโ€ข Find shortest alternating pathโ€ข BFS guarantees optimal routes๐Ÿ“Š Shortest Distances๐Ÿข Node 0: 0 (start)๐Ÿฌ Node 1: 1 (red street)๐Ÿช Node 2: 1 (blue street)๐Ÿ›๏ธ Node 3: 2 (alternating)๐Ÿจ Node 4: 3 (alternating)โšก Algorithm1. BFS from node 02. Track (node, color)3. Alternate edge colors4. Level-by-level searchโฑ๏ธ O(V + E) time
Understanding the Visualization
1
Start Your Journey
Begin at city center (node 0) with option to take either red or blue street first
2
Follow Alternating Pattern
From each intersection, you can only take streets of opposite color to the one you just used
3
BFS Exploration
Like spreading ripples, explore all reachable intersections level by level
4
Track Shortest Routes
Record the shortest path to each destination following the alternating rule
Key Takeaway
๐ŸŽฏ Key Insight: By treating (node, last_edge_color) as our state space, we transform the alternating constraint into a standard shortest path problem that BFS can solve optimally in O(V + E) time.
Asked in
Google 35 Meta 28 Amazon 22 Microsoft 18
23.5K Views
Medium Frequency
~18 min Avg. Time
892 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