The Most Similar Path in a Graph - Problem

You're given an undirected connected graph with n cities and m bi-directional roads. Each city has a unique three-letter name (like "NYC", "LAX", "SFO"), and you can travel between any two cities following the road connections.

Your task is to find a path of specific length that has the minimum edit distance to a given target path. The edit distance counts how many positions differ between your path and the target path.

Goal: Find any valid path with the same length as targetPath that minimizes the number of different city names at corresponding positions.

Input:

  • n cities with names[i] containing exactly 3 uppercase letters
  • roads[i] = [a, b] representing bi-directional connections
  • targetPath - the desired sequence of city names to match

Output: Return the indices of cities forming the optimal path

Input & Output

example_1.py — Basic Path Matching
$ Input: n = 5, roads = [[0,2],[0,3],[1,2],[1,3],[1,4],[2,4]], names = ["NYC","LAX","SFO","CHI","PHX"], targetPath = ["NYC","LAX","SFO"]
Output: [0,2,4] or [0,3,4]
💡 Note: The target path wants NYC→LAX→SFO, but LAX (city 1) is not directly connected to SFO (city 4). We can use NYC→SFO→PHX which has edit distance of 2 (mismatches at positions 1 and 2), or find a better path if available.
example_2.py — Perfect Match Available
$ Input: n = 4, roads = [[0,1],[1,2],[2,3],[0,3]], names = ["NYC","LAX","SFO","CHI"], targetPath = ["NYC","LAX","SFO"]
Output: [0,1,2]
💡 Note: Perfect match exists! NYC (0) → LAX (1) → SFO (2) matches the target path exactly with edit distance 0.
example_3.py — No Direct Path
$ Input: n = 3, roads = [[0,1],[1,2]], names = ["ABC","DEF","GHI"], targetPath = ["GHI","DEF","ABC"]
Output: [2,1,0]
💡 Note: Target wants GHI→DEF→ABC. The only path of length 3 is either 2→1→0 (perfect match) or 0→1→2 (all mismatches). The optimal path [2,1,0] has edit distance 0.

Constraints

  • 2 ≤ n ≤ 100
  • n-1 ≤ roads.length ≤ n*(n-1)/2
  • roads[i].length == 2
  • 0 ≤ ai, bi ≤ n-1
  • ai != bi
  • roads represents a connected undirected graph
  • names.length == n
  • names[i].length == 3
  • names[i] consists of uppercase English letters
  • 1 ≤ targetPath.length ≤ 100
  • targetPath[i].length == 3

Visualization

Tap to expand
Most Similar Path in GraphNYCLAXSFOCHIPHXStart: NYCWant: LAXWant: SFO✓ Optimal Path: NYC→CHI→PHX (Edit Distance: 2)Key Insight:• DP state: dp[position][city] = minimum edit distance• For each position, try all cities reachable from previous position• Cost = previous_cost + (0 if name matches target, else 1)
Understanding the Visualization
1
Map the Cities
Build a graph representing all city connections
2
Compare Preferences
At each step, check if current city matches target preference
3
Build Optimal Routes
Use DP to find minimum mismatches for each position and city
4
Choose Best Path
Select the complete route with minimum total mismatches
Key Takeaway
🎯 Key Insight: Dynamic Programming allows us to build optimal paths incrementally, avoiding exponential brute force while guaranteeing the minimum edit distance solution.
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18
24.3K Views
Medium Frequency
~25 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