Checking Existence of Edge Length Limited Paths - Problem

An undirected graph of n nodes is defined by edgeList, where edgeList[i] = [ui, vi, disi] denotes an edge between nodes ui and vi with distance disi. Note that there may be multiple edges between two nodes.

Given an array queries, where queries[j] = [pj, qj, limitj], your task is to determine for each queries[j] whether there is a path between pj and qj such that each edge on the path has a distance strictly less than limitj.

Return a boolean array answer, where answer.length == queries.length and the jth value of answer is true if there is a path for queries[j], and false otherwise.

Input & Output

Example 1 — Basic Connectivity
$ Input: n = 3, edgeList = [[0,1,2],[1,2,4],[2,0,8],[1,0,16]], queries = [[0,1,10],[0,2,5]]
Output: [true,true]
💡 Note: For query [0,1,10]: Can use edge [0,1,2] since 2 < 10. For query [0,2,5]: Can use path 0→1→2 with edges [0,1,2] and [1,2,4], since both 2 < 5 and 4 < 5.
Example 2 — No Valid Path
$ Input: n = 5, edgeList = [[0,1,10],[1,2,5],[2,3,20],[3,4,15]], queries = [[0,4,14]]
Output: [false]
💡 Note: To go from 0 to 4, must use path 0→1→2→3→4. Edge weights are [10,5,20,15]. Since edge [2,3,20] has weight 20 ≥ 14, no valid path exists.
Example 3 — Multiple Valid Paths
$ Input: n = 4, edgeList = [[0,1,3],[1,3,1],[0,2,7],[2,3,2]], queries = [[0,3,8]]
Output: [true]
💡 Note: Two paths exist: 0→1→3 (weights 3,1 both < 8) and 0→2→3 (weights 7,2 both < 8). Both paths are valid, so answer is true.

Constraints

  • 1 ≤ n ≤ 105
  • 0 ≤ edgeList.length, queries.length ≤ 105
  • edgeList[i].length == 3
  • queries[j].length == 3
  • 0 ≤ ui, vi, pj, qj ≤ n - 1
  • ui ≠ vi
  • pj ≠ qj
  • 1 ≤ disi, limitj ≤ 109

Visualization

Tap to expand
Edge Length Limited Paths INPUT 0 1 2 2, 16 4 8 edgeList: [[0,1,2],[1,2,4], [2,0,8],[1,0,16]] queries: [[0,1,10],[0,2,5]] n = 3 Query: path exists with edges < limit? ALGORITHM STEPS 1 Sort edges by distance [0,1,2],[1,2,4],[2,0,8],[1,0,16] 2 Sort queries by limit [0,2,5,idx1], [0,1,10,idx0] 3 Init Union-Find parent: [0, 1, 2] 4 Process queries Q1: limit=5, add edge(0,1,2) add edge(1,2,4) find(0)!=find(2) --> false Q2: limit=10, already unified find(0)==find(1) --> true After processing: 0 1 2 connected FINAL RESULT Query Results Query 1: [0,1,10] Path 0-->1 with dist 2 TRUE Query 2: [0,2,5] No path with all edges <5 FALSE Output: [true, false] Answer matches expected OK Key Insight: By sorting both edges and queries by distance/limit, we can incrementally build the Union-Find structure. For each query, we add all edges with distance < limit before checking connectivity. This avoids rebuilding the structure for each query, achieving O((E+Q) * alpha(N)) time complexity. TutorialsPoint - Checking Existence of Edge Length Limited Paths | Union-Find with Sorting Approach
Asked in
Google 15 Amazon 12 Microsoft 8 Facebook 6
23.0K Views
Medium Frequency
~35 min Avg. Time
890 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