Find the Bottleneck Weight Between Two Nodes
Trees & GraphsAuto-graded · 19 test casesLast updated
Compute the smallest possible maximum edge weight along any path connecting two vertices in a weighted undirected graph.
You have an undirected graph with weighted edges. Given two vertices, source and destination, you need to find a path between them that minimizes the heaviest edge along the way. Return this minimum achievable maximum edge weight, or -1 if no path exists.
Task
Write a function that takes:
n: the number of vertices (labeled0throughn - 1)edges: a list of tuples(u, v, weight)representing undirected edgessource: the starting vertexdestination: the ending vertex
Return the smallest value w such that there exists a path from source to destination where every edge has weight at most w. If the two vertices are not connected, return -1. If source equals destination, return 0 (the empty path uses no edges).
Details
- Each edge
(u, v, weight)can be traversed in both directions. - Multiple edges between the same pair of vertices are allowed.
- Edge weights are positive integers.
Constraints
1 ≤ n ≤ 200,0000 ≤ len(edges) ≤ 300,0001 ≤ weight ≤ 10^90 ≤ source, destination < n
Examples
Example 1
Input: min_bottleneck_weight(5, [[0, 1, 8], [0, 2, 3], [2, 1, 4], [1, 3, 6], [2, 3, 10]], 0, 3)
Output: 6
Hints
Hint 1
Think about what happens when you only allow edges up to a certain weight threshold—does increasing the threshold help or hurt connectivity?
Hint 2
Sort the edges by weight and add them incrementally. Track when the two endpoints first become connected.
Hint 3
A union-find data structure can efficiently maintain connected components as you add edges one by one.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem asks us to minimize the largest edge weight rather than the total path weight. The key insight is to think in terms of thresholds: if we're only allowed to use edges with weight at most w, when do source and destination first become connected?
This connectivity property is monotonic—once two vertices are connected at threshold w, they remain connected at any higher threshold (since we only add more edges). Therefore, the answer is the smallest weight at which the two vertices first share a component.
We use a greedy union-find approach inspired by Kruskal's MST algorithm:
- Sort all edges by weight in ascending order
- Process edges from lightest to heaviest, maintaining connected components with a union-find structure
- When adding an edge connects two previously separate components, check if
sourceanddestinationare now in the same component - The first edge that unites them gives the answer—its weight is the minimum bottleneck
Why this works: When edge (u, v, w) first connects source and destination:
- Every edge processed so far has weight ≤
w, so a path exists using only those edges (upper bound) - Before processing this edge, no path existed using only lighter edges, so any valid path must include an edge of weight ≥
w(lower bound)
Together, these prove the answer is exactly w.
Edge cases:
- If
source == destination, return0immediately (empty path) - If edges run out without connecting the vertices, return
-1 - Self-loops are harmlessly ignored (they never merge different components)
- Parallel edges are handled naturally—the lighter one connects first
Complexity
Time: O(E log E) for sorting the edges, plus O(E α(n)) for union-find operations where α is the inverse Ackermann function (effectively constant). Overall O(E log E).
Space: O(n) for the parent array, plus O(E) for the sorted edge list.
Solution
def min_bottleneck_weight(n: int, edges: list[tuple[int, int, int]], source: int, destination: int) -> int:
if source == destination:
return 0
parent = list(range(n))
def find(x: int) -> int:
while parent[x] != x:
parent[x] = parent[parent[x]]
x = parent[x]
return x
for u, v, w in sorted(edges, key=lambda e: e[2]):
ru, rv = find(u), find(v)
if ru != rv:
parent[ru] = rv
if find(source) == find(destination):
return w
return -1