Given a weighted undirected graph G(v,e) with weights w(e), find the set of edges such that each pair of vertices (u,v)∈G are connected (in short, spanning tree
) and the range of weights of selected edges is minimum (or the difference between the minimum weight and the maximum weight is minimum).
I tried greedy approach in which sorted the edges with respect to weights and then selected two edges with minimum weight difference between the consecutive edges (g[index = current_left],g[index+1 = current_right]) in the sorted array, subsequently I moved left or right depending on the minimum difference between the (current_left,current_left-j
) or (current_right,current_right+j
) where j
is incremented till we find an edge with at least one non-visited vertex.
For example:
Here the minimum range that we can get is by selecting edges with weight {2,3,5} and the range is 3.
Please point a test case where the suggested algorithm fails and suggest an algorithm for finding such spanning tree.
Edit:
Expected time complexity is O(|E|log|E|) where |E| is number of edges.