Graphs
Find City with Fewest Reachable
DESCRIPTION (inspired by Leetcode.com)
A regional planning committee wants to identify the most "isolated" city in a road network. Given n cities connected by bidirectional roads with varying distances, find the city that can reach the fewest other cities within a given distance threshold.
If multiple cities have the same minimum count, return the city with the highest number.
Note: The distance between two cities is the shortest path distance considering all possible routes.
Example 1:
Input:
n = 4 edges = [[0,1,3], [1,2,1], [1,3,4], [2,3,1]] distanceThreshold = 4
Output:
3
Explanation: City 3 can only reach city 1 (distance 4) within the threshold. Cities 0 and 2 can each reach 2 cities, and city 1 can reach 3 cities. City 3 has the fewest reachable.
Example 2:
Input:
n = 5 edges = [[0,1,2], [0,4,8], [1,2,3], [1,4,2], [2,3,1], [3,4,1]] distanceThreshold = 2
Output:
0
Explanation: With threshold 2, city 0 can only reach city 1 (distance 2). Other cities can reach more neighbors. City 0 has the fewest reachable.
Run your code to see results here
Have suggestions or found something wrong?
Explanation
Understanding the Problem
- City 0: reaches cities 1 (dist 3) and 2 (dist 2) → 2 reachable
- City 1: reaches cities 0 (dist 3), 2 (dist 1), 3 (dist 4) → 3 reachable
- City 2: reaches cities 0 (dist 2), 1 (dist 1) → 2 reachable
- City 3: reaches only city 1 (dist 4) → 1 reachable ← fewest!
Why This Differs from Previous Problems
The Floyd-Warshall Approach
The Algorithm Step by Step
- Initialize: dist[i][j] = edge weight if edge exists, 0 if i=j, ∞ otherwise
- For each intermediate vertex k (outer loop):
- For each source i (middle loop):
- For each destination j (inner loop):
- If dist[i][k] + dist[k][j] < dist[i][j], update dist[i][j]
- For each destination j (inner loop):
- For each source i (middle loop):
- Count reachable: For each city, count how many cities are within threshold
- Return: City with minimum reachable count (highest numbered if tied)
Walkthrough
- dist[0][3] updates: 0→3 was ∞, but 0→1→3 = 3+4 = 7
- dist[0][3] updates again: 0→2→3 = 2+5 = 7 (same)
- dist[1][3] stays 4 (1→3 direct is better than 1→2→3 = 1+5 = 6)
When to Use Floyd-Warshall vs. Running Dijkstra n Times
| Scenario | Floyd-Warshall | n × Dijkstra |
|---|---|---|
| Time complexity | O(V³) | O(V · (V+E) log V) |
| Dense graph (E ≈ V²) | O(V³) ✓ | O(V³ log V) |
| Sparse graph (E ≈ V) | O(V³) | O(V² log V) ✓ |
| Simple implementation | ✓ Three nested loops | More complex |
| Works with negative edges | ✓ (no negative cycles) | ✗ |
Solution
- Initialize distance matrix from edges (bidirectional, so set both directions)
- Run Floyd-Warshall to find all shortest paths
- Count reachable cities within threshold for each city
- Return city with minimum count (highest number if tied)
def find_the_city(n, edges, distance_threshold):# Initialize distance matrix with infinitydist = [[float('inf')] * n for _ in range(n)]for i in range(n):dist[i][i] = 0# Fill in direct edges (bidirectional)for u, v, w in edges:dist[u][v] = wdist[v][u] = w# Floyd-Warshall: try each vertex as intermediatefor k in range(n):for i in range(n):for j in range(n):if dist[i][k] + dist[k][j] < dist[i][j]:dist[i][j] = dist[i][k] + dist[k][j]# Find city with fewest reachable within thresholdresult = 0min_reachable = nfor city in range(n):count = sum(1 for j in range(n)if city != j and dist[city][j] <= threshold)if count <= min_reachable:min_reachable = countresult = cityreturn result
Initialize distance matrix: diagonal = 0, all others = ∞
0 / 21
What is the time complexity of this solution?
O(m * n * 4^L)
O(V³)
O(m * n)
O(4^L)
Mark as read