Minimize the Maximum Edge Weight of Graph - Solution & Explanation
Problem Statement
You are given two integers, n and threshold, as well as a directed weighted graph of n nodes numbered from 0 to n - 1. The graph is represented by a 2D integer array edges, where edges[i] = [Ai, Bi, Wi] indicates that there is an edge going from node Ai to node Bi with weight Wi.
You have to remove some edges from this graph (possibly none), so that it satisfies the following conditions:
- Node 0 must be reachable from all other nodes.
- The maximum edge weight in the resulting graph is minimized.
- Each node has at most
thresholdoutgoing edges.
Return the minimum possible value of the maximum edge weight after removing the necessary edges. If it is impossible for all conditions to be satisfied, return -1.
Example 1:
Input: n = 5, edges = [[1,0,1],[2,0,2],[3,0,1],[4,3,1],[2,1,1]], threshold = 2
Output: 1
Explanation:

Remove the edge 2 -> 0. The maximum weight among the remaining edges is 1.
Example 2:
Input: n = 5, edges = [[0,1,1],[0,2,2],[0,3,1],[0,4,1],[1,2,1],[1,4,1]], threshold = 1
Output: -1
Explanation:
It is impossible to reach node 0 from node 2.
Example 3:
Input: n = 5, edges = [[1,2,1],[1,3,3],[1,4,5],[2,3,2],[3,4,2],[4,0,1]], threshold = 1
Output: 2
Explanation:

Remove the edges 1 -> 3 and 1 -> 4. The maximum weight among the remaining edges is 2.
Example 4:
Input: n = 5, edges = [[1,2,1],[1,3,3],[1,4,5],[2,3,2],[4,0,1]], threshold = 1
Output: -1
Constraints:
2 <= n <= 1051 <= threshold <= n - 11 <= edges.length <= min(105, n * (n - 1) / 2).edges[i].length == 30 <= Ai, Bi < nAi != Bi1 <= Wi <= 106- There may be multiple edges between a pair of nodes, but they must have unique weights.
Approach Overview
Problem Overview: You are given a weighted graph and need a path where the largest edge weight used is as small as possible. Instead of minimizing the sum of weights, the goal is to minimize the maximum edge weight along the chosen path.
Approach 1: Modified Dijkstra (Minimax Path) (Time: O(E log V), Space: O(V))
This problem can be treated as a variation of shortest path. Instead of tracking the total distance, store the smallest possible maxEdge needed to reach each node. When exploring an edge (u β v) with weight w, update the state as newCost = max(currentCost, w). Use a priority queue that always expands the node with the smallest current maximum edge weight. This works because once a node is processed with the minimal possible maximum weight, any later path will only increase that value.
Approach 2: Binary Search + Graph Traversal (Time: O(E log W), Space: O(V))
Another perspective: if you fix a maximum allowed edge weight X, the problem reduces to checking if a path exists using only edges β€ X. The feasibility check can be done with Breadth-First Search or Depth-First Search. Binary search over the answer range (from the smallest to largest edge weight). For each midpoint, run BFS/DFS ignoring edges heavier than the threshold. If the destination becomes reachable, the threshold is feasible and you try a smaller value.
Approach 3: Minimum Spanning Tree Observation (Time: O(E log V), Space: O(V))
The minimax property of paths in an MST provides another insight. In a Minimum Spanning Tree, the maximum edge weight on the path between two nodes is minimized among all possible paths in the original graph. Construct the MST using Kruskal or Prim, then find the path between the required nodes and return the largest edge weight along that path.
Recommended for interviews: Binary search with BFS/DFS is often the most intuitive explanation because it directly models the decision question: βCan a path exist if the maximum edge weight is β€ X?β. The modified Dijkstra solution is equally strong and often considered the cleanest implementation for production code. Mentioning both shows strong understanding of graph algorithms and optimization techniques.
Solutions for this problem are being prepared.
Try solving it yourselfDetailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Modified Dijkstra (Minimax Path) | O(E log V) | O(V) | General case when you want the optimal minimax path directly |
| Binary Search + BFS/DFS | O(E log W) | O(V) | When edge weights have a clear numeric range and feasibility checking is simple |
| Minimum Spanning Tree Path | O(E log V) | O(V) | When MST is already computed or when analyzing minimax path properties |
Video Solution
Minimize the Maximum Edge Weight of Graph | Detailed Intuition | BFS | DFS | Leetcode 3419 | MIK β’ codestorywithMIK β’ 4,992 views views
Watch 6 more video solutions βFrequently Asked Questions
Is Minimize the Maximum Edge Weight of Graph easy or hard?
Minimize the Maximum Edge Weight of Graph Python/Java solution
How to solve Minimize the Maximum Edge Weight of Graph in O(E log V)?
What is the best approach for Minimize the Maximum Edge Weight of Graph?
Is Minimize the Maximum Edge Weight of Graph asked at Google/Amazon/Meta?
What data structure is used in Minimize the Maximum Edge Weight of Graph?
What is the time complexity of Minimize the Maximum Edge Weight of Graph?
Ready to solve this problem?
Practice Minimize the Maximum Edge Weight of Graph with our built-in code editor and test cases.
Practice on FleetCodeTable of Contents
Practice this problem
Open in Editor