1

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:

enter image description here

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.

Community
  • 1
  • 1
IronMan007
  • 105
  • 4
  • 14
  • How do you select the first edge? Obviously, in your example, if you start with the edge of weight 7, you cannot find an optimum solution. – Henrik Nov 09 '15 at 09:45
  • on sorting the edges we get 2,3,5,7 the difference between 2 & 3 is 1 which is minimum difference that could be possible by selecting two edges. So first I select 2,3 then after I select accordingly. – IronMan007 Nov 09 '15 at 09:47
  • 1
    So, if the weights are 1, 2, 100, 102, 104, 108, you start by selecting 1 and 2? I'm pretty sure you can construct a counter example from this. – Henrik Nov 09 '15 at 09:51
  • Yeah, true, I got it. So how can it be solved? – IronMan007 Nov 09 '15 at 10:34
  • An obvious solution is to enumerate all subsets of edges and find the one that is a spanning tree and has minimum weight. Time complexity is exponential, of course. So are there any constraints on the running time? – nwellnhof Nov 09 '15 at 11:52

2 Answers2

2

You should be able to do it in O(E * (cost of MST computation)):

T = no tree
for all edge weights w_fix sorted in ascending order:
    for all edges e:
        if w(e) >= w_fix:
            set w'(e) = w(e) - w_fix
        else:
            set w'(e) = infinity
    find MST T' according to w'
    if T == no tree or max edge weight(T) > max edge weight(T'):
        set T := T'
print T

The idea is that some edge weight has to be the minimum edge weight among the edges in an optimal spanning tree; so fix a minimum edge weight and find an MST that only contains edges heavier than that. Since all MSTs are also minimum bottleneck spanning trees, this will work.


Here's an improvement that is optimal up to a log-square factor; the basic idea remains the same.

sort edge array E[] by increasing weights

low := high := 0
opt_low := opt_high := 0
opt := infinity
connected := false

while (high < E.length - 1) or (connected):

    if not connected:
        high = high + 1
    else:
        low = low + 1

    update(connected)

    if connected:
        if E[high].weight - E[low].weight < opt:
            opt = E[high].weight - E[low].weight
            opt_low = low
            opt_high = high

print(opt, opt_low, opt_high)

The idea is to keep a sliding window over the edges and use connectivity to maintain the window. To maintain connectivity information, you would use special data structures. There's a number of them that allow for polylogarithmic time costs to maintain connectivity information for both deleting and adding edges, and you can find information on those data structures in these MIT 6.851 lecture notes.

G. Bach
  • 3,869
  • 2
  • 25
  • 46
  • Thank you!! Can it be optimized to O(ElogE)? – IronMan007 Nov 09 '15 at 13:45
  • 1
    @RjlovesS Not that I can see, but I don't have a proof for a lower bound of `Ω(E * cost(MST))` either. So: possibly, but I can't do it. If you figure out how, please post it as an answer because I would be interested in that. – G. Bach Nov 09 '15 at 14:06
  • 1
    I think we can do it in O(ElogE*logE) instead of going linearly for each edge as minimum we can do it in binary i.e selecting one middle element as minimum edge and if we are able to construct spanning tree go for right side and if we are not able to go for left side, and O(ElogE) is for MST. What do you say? – IronMan007 Nov 09 '15 at 17:38
  • 1
    @RjlovesS I doubt that works. Take a complete graph G with a unique optimal spanning tree according to your rules with edge weights in the lower half of the range, and a complete G' with ... in the upper half of the range. The fact that in both cases you can build a spanning tree during the first iteration doesn't tell you whether to look at heavier or lighter edges next. For binary search you need to be able to discard a linear portion of the search space after each iteration, and I don't see why you would be able to do this. – G. Bach Nov 09 '15 at 21:01
0

The algorithm described by G Bach infact works correctly and has a run time of O(m*m) where m is the number of edges(considering the computation of mst takes O(m) time). This was a question asked in codeforces edu section.

Sai Kumar
  • 1
  • 1