The Most Similar Path in a Graph - Problem

We have n cities and m bi-directional roads where roads[i] = [ai, bi] connects city ai with city bi. Each city has a name consisting of exactly three upper-case English letters given in the string array names.

Starting at any city x, you can reach any city y where y != x (i.e., the cities and the roads are forming an undirected connected graph).

You will be given a string array targetPath. You should find a path in the graph of the same length and with the minimum edit distance to targetPath.

You need to return the order of the nodes in the path with the minimum edit distance. The path should be of the same length of targetPath and should be valid (i.e., there should be a direct road between ans[i] and ans[i + 1]).

If there are multiple answers return any one of them.

The edit distance is defined as follows:

  • Edit distance is the number of positions where the path differs from the target path
  • Lower edit distance means the path is more similar to the target

Input & Output

Example 1 — Basic Graph Path
$ Input: n = 5, roads = [[0,1],[1,2],[2,3],[3,4],[1,4]], names = ["AAA","BBB","CCC","DDD","EEE"], targetPath = ["AAA","CCC"]
Output: [1,2]
💡 Note: Path [1,2] gives ["BBB","CCC"] vs target ["AAA","CCC"] with edit distance 1. Path [0,2] is invalid since there's no direct edge 0-2. The optimal valid path [1,2] has edit distance 1.
Example 2 — Minimum Edit Distance
$ Input: n = 4, roads = [[0,1],[1,2],[2,3],[0,3]], names = ["ATL","PEK","LAX","DXB"], targetPath = ["ABC","DEF","GHI"]
Output: [0,1,2]
💡 Note: No perfect match exists. Path [0,1,2] gives ["ATL","PEK","LAX"] with edit distance 3 (all positions differ from ["ABC","DEF","GHI"]).
Example 3 — Single City Path
$ Input: n = 2, roads = [[0,1]], names = ["AAA","BBB"], targetPath = ["BBB"]
Output: [1]
💡 Note: Target has length 1. City 1 has name "BBB" which exactly matches targetPath[0], giving 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
  • names.length == n
  • names[i].length == 3
  • names[i] consists of uppercase English letters
  • 1 ≤ targetPath.length ≤ 100
  • targetPath[i].length == 3
  • targetPath[i] consists of uppercase English letters

Visualization

Tap to expand
Most Similar Path in Graph - DP Approach INPUT Graph Structure (n=5) 0:AAA 1:BBB 2:CCC 3:DDD 4:EEE Target Path: AAA CCC Find path with min edit dist to target (length = 2) roads=[[0,1],[1,2],[2,3],[3,4],[1,4]] ALGORITHM STEPS 1 Initialize DP Table dp[i][j] = min edit dist ending at node j for path[0..i] 2 Base Case (i=0) dp[0][j] = 0 if names[j]==AAA dp[0][j] = 1 otherwise 3 Fill DP Table For each position i, node j: dp[i][j] = min(dp[i-1][k]) + cost where k is neighbor of j 4 Backtrack Path Find min in last row Trace back optimal path DP Table (simplified): i\j 1 2 0 1 1 1 1 0 Best! FINAL RESULT Optimal Path Found: Node 1 BBB Node 2 CCC Comparison: Target: AAA --> CCC Found: BBB --> CCC Edit Distance: 1 (minimal) Output: [1, 2] OK - Valid path with min edit dist Key Insight: The DP approach uses dp[i][j] to track the minimum edit distance to reach node j at position i of the path. Transitions only occur between adjacent nodes in the graph. Time complexity: O(path_len * n * max_degree). The path [1,2] (BBB-->CCC) differs from target (AAA-->CCC) at only position 0, giving edit distance = 1. TutorialsPoint - The Most Similar Path in a Graph | DP Approach
Asked in
Google 15 Facebook 12 Amazon 8 Microsoft 6
23.4K Views
Medium Frequency
~35 min Avg. Time
542 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