Minimize the Maximum Edge Weight of Graph - Problem

Imagine you're designing a transportation network where all cities must be able to reach the capital (node 0), but you want to minimize the heaviest route needed. You're given a directed weighted graph with n nodes (0 to n-1) and a list of edges with weights.

Your challenge is to remove some edges from the graph such that:

  • Connectivity: Node 0 must be reachable from every other node
  • Efficiency: The maximum edge weight in the final graph is minimized
  • Constraint: Each node can have at most threshold outgoing edges

Return the minimum possible maximum edge weight after optimal edge removal, or -1 if impossible.

Example: With nodes [0,1,2], edges [[1,0,1], [2,0,2], [1,0,3]], and threshold=1, we can keep edges with weights 1 and 2, giving us a maximum weight of 2.

Input & Output

example_1.py — Basic Case
$ Input: n = 3, threshold = 2, edges = [[1,0,1], [2,0,2], [1,2,3]]
Output: 2
💡 Note: We need all nodes to reach node 0. Keep edges [1,0,1] and [2,0,2] (weights 1,2). Node 1 reaches 0 directly, node 2 reaches 0 directly. Maximum edge weight is 2.
example_2.py — Threshold Constraint
$ Input: n = 4, threshold = 1, edges = [[1,0,5], [2,0,3], [3,0,1], [1,3,2]]
Output: 3
💡 Note: With threshold=1, each node can have at most 1 outgoing edge. We can use [3,0,1], [1,3,2], [2,0,3]. All nodes reach 0: 1→3→0, 2→0, 3→0. Maximum weight is 3.
example_3.py — Impossible Case
$ Input: n = 3, threshold = 1, edges = [[0,1,1], [0,2,2]]
Output: -1
💡 Note: Node 0 has outgoing edges but we need nodes 1 and 2 to reach node 0. With only outgoing edges from node 0, nodes 1 and 2 cannot reach node 0.

Constraints

  • 1 ≤ n ≤ 105
  • 0 ≤ threshold ≤ n - 1
  • 0 ≤ edges.length ≤ min(105, n * (n - 1))
  • edges[i] = [Ai, Bi, Wi]
  • 0 ≤ Ai, Bi ≤ n - 1
  • Ai ≠ Bi
  • 1 ≤ Wi ≤ 106
  • No duplicate edges between same pair of nodes

Visualization

Tap to expand
Emergency Evacuation Network Optimization🏥Shelter (0)1District A2District B3District CEasy (1)Medium (3)Hard (5)Not neededBinary Search Process:🔍 Try max_difficulty = 4: Can all districts reach shelter with routes ≤ 4?✅ Yes! Routes of difficulty 1 and 3 are sufficient for connectivity🔍 Try max_difficulty = 2: Can we do better?❌ No! District B needs difficulty 3 route, so minimum answer is 3
Understanding the Visualization
1
Identify Goal
All nodes must be able to reach node 0 (the emergency shelter)
2
Apply Constraints
Each node can maintain at most 'threshold' outgoing roads due to budget limits
3
Binary Search
Search for the minimum possible 'maximum difficulty level' needed
4
Greedy Selection
For each candidate difficulty, greedily choose the easiest roads
5
Verify Connectivity
Check if all neighborhoods can still reach the shelter
Key Takeaway
🎯 Key Insight: Binary search on the maximum edge weight combined with greedy selection of lightest edges ensures optimal connectivity while respecting threshold constraints.
Asked in
Google 28 Amazon 22 Microsoft 18 Meta 15
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