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
Minimize Maximum Edge Weight of Graph INPUT 0 (capital) 1 2 1 2 3 n = 3 threshold = 2 edges = [ [1,0,1], [2,0,2], [1,2,3] ] ALGORITHM STEPS 1 Sort Edges By weight: 1, 2, 3 2 Binary Search On max weight allowed 3 Check Feasibility All nodes reach 0? 4 Greedy Selection Pick min weight edges Check max=2: - Edge 1-->0 (w=1) OK - Edge 2-->0 (w=2) OK - Edge 1-->2 (w=3) SKIP Node 1: 1 outgoing (OK) Node 2: 1 outgoing (OK) FINAL RESULT 0 1 2 1 2 (removed) Output: 2 Max edge weight = 2 All nodes reach capital Threshold satisfied Key Insight: Use binary search on the maximum allowed edge weight. For each candidate weight, greedily select the minimum weight edges that allow all nodes to reach node 0 while respecting the threshold constraint on outgoing edges. The answer is the minimum weight where this is feasible. TutorialsPoint - Minimize the Maximum Edge Weight of Graph | Greedy Approach
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