You iterate over all possible costs to the edges, which leads to an outer O (m) loop. Note that if the graph is disabled when all edges> w (e) are dropped, it is also disabled for> w (e '), where w(e') < w(e) . You can use this property to perform a binary search at the cost of an edge and thus do it in O (log (n)).
lo=min(w(e) for e in edges), hi=max(w(e) for e in edges) while lo<hi: mid=(lo+hi)/2 if connected(graph after discarding all e where w(e)>w(mid)): lo=mid else: hi=mid-1 return lo
Binary search has complexity O (log (max_e-min_e)) (you can actually bring it to O (log (edges)), and dropping edges and determining connectivity can be done in O (edges + vertices), so this can be done in O ((edge + vertices) * log (edges)).
Warning: I have not tested this in code yet, so there may be errors. But the idea should work.
source share