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
thresholdoutgoing 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
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.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code