Checking Existence of Edge Length Limited Paths II - Problem

Imagine you're designing a smart navigation system for a delivery network where roads have different weight limits. You need to quickly answer queries about whether packages can be delivered between two locations using only roads that can handle weights strictly less than a given limit.

You're given an undirected graph with n nodes representing locations, defined by edgeList where edgeList[i] = [ui, vi, disi] represents a road between locations ui and vi with weight capacity disi. Note that there may be multiple roads between two locations, and some locations might be unreachable from others.

Your task: Implement the DistanceLimitedPathsExist class that can efficiently answer multiple queries about path existence under weight constraints.

The class should support:

  • DistanceLimitedPathsExist(int n, int[][] edgeList) - Initialize with the graph
  • boolean query(int p, int q, int limit) - Return true if there's a path from p to q using only edges with weight strictly less than limit

Input & Output

example_1.py โ€” Basic Connectivity
$ Input: n = 3, edgeList = [[0,1,2],[1,2,4],[2,0,8],[1,0,16]] Queries: query(0, 1, 2), query(0, 2, 5)
โ€บ Output: [false, true]
๐Ÿ’ก Note: First query: limit=2, no edges < 2 exist, so 0 and 1 not connected. Second query: limit=5, edges [0,1,2] and [1,2,4] both < 5, creating path 0->1->2.
example_2.py โ€” Multiple Components
$ Input: n = 5, edgeList = [[0,1,10],[1,2,5],[2,3,9],[3,4,13]] Queries: query(0, 4, 14), query(1, 4, 13)
โ€บ Output: [true, false]
๐Ÿ’ก Note: First query: All edges < 14, path exists 0->1->2->3->4. Second query: Edge [3,4,13] not usable (13 โ‰ฅ 13), so nodes 1 and 4 disconnected.
example_3.py โ€” Same Node Query
$ Input: n = 2, edgeList = [[0,1,20]] Queries: query(0, 0, 10)
โ€บ Output: [true]
๐Ÿ’ก Note: Querying connectivity from a node to itself always returns true, regardless of edge constraints.

Visualization

Tap to expand
๐Ÿš› Smart Delivery Network with Weight LimitsWarehouseAHubBCustomerCStoreD5 tons8 tons12 tons3 tons๐Ÿš› Query: A โ†’ C with 7-ton truckโœ… Road Aโ†’B: 5 tons < 7 tons (OK)โœ… Road Bโ†’C: 8 tons โ‰ฅ 7 tons (NOT OK)โœ… Road Dโ†’C: 3 tons < 7 tons (OK)Result: Need alternative route Aโ†’Dโ†’CUnion-Find Algorithm1. Sort roads by capacity: [3, 5, 8, 12]2. Add roads with capacity < 7: [3, 5]3. Union components: {A,B}, {D,C}4. Check if A and C connected: No5. Return false - no valid route
Understanding the Visualization
1
Network Setup
Create delivery network with weighted road connections
2
Sort by Capacity
Organize roads by their weight capacity limits
3
Build Route Map
For each query, include only suitable roads in the route map
4
Check Connectivity
Use Union-Find to verify if source and destination are connected
Key Takeaway
๐ŸŽฏ Key Insight: By sorting edges by weight and using Union-Find, we can efficiently determine connectivity under weight constraints, making this approach optimal for handling multiple queries on the same graph structure.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O((E + Q) log(E + Q) + (E + Q)ฮฑ(V))

Sorting edges and queries, plus Union-Find operations with inverse Ackermann function

n
2n
โšก Linearithmic
Space Complexity
O(V + E + Q)

Union-Find structure, sorted arrays, and result storage

n
2n
โœ“ Linear Space

Constraints

  • 2 โ‰ค n โ‰ค 104
  • 0 โ‰ค edgeList.length โ‰ค min(n*(n-1)/2, 104)
  • edgeList[i].length == 3
  • 0 โ‰ค ui, vi, p, q โ‰ค n-1
  • ui != vi
  • 0 โ‰ค disi, limit โ‰ค 109
  • At most 104 calls will be made to query
Asked in
Google 45 Amazon 38 Meta 25 Microsoft 22
23.5K Views
Medium Frequency
~25 min Avg. Time
847 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