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.

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.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(E log E + E log W * (V + E))

E log E for sorting, log W binary search iterations, each taking O(V + E) for connectivity check

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

Space for graph representation and connectivity verification

n
2n
โœ“ Linear Space

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
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