Search code examples
algorithmcomputer-sciencegreedykruskals-algorithm

Kruskal's MST Algorithm non-deterministic?


The following is pseudo code for Kruskal's Minimum Spanning Tree algorithm from our CS algorithms lecturer. I wanted to know if the MST algorithm is non-deterministic. Given two edges with the same weight, how would the algorithm decide between them if neither edge formed a cycle when adding to T. Surely if it was random then one could not determine the result of what exact edges are added to T?

    Given an undirected connected graph G=(V,E)    
    T=Ø //Empty set, i.e. empty
    E'=E
    while E'≠Ø do
    begin
        pick an edge e in E' with minumum weight
        if adding e to T does not form a cycle then
            T = T∪{e} //Set union, add e to T
        E' = E'\{e} //Set difference, remove e from E'
    end

Thanks!


Solution

  • Given two edges with the same weight, how would the algorithm decide between them if neither edge formed a cycle when adding to T. Surely if it was random then one could not determine the result of what exact edges are added to T?

    This is upto the implementation.

    Kruskal's algorithm finds one of the many possible MSTs of a connected weighted graph (which is not a tree). This is because at each iteration you have multiple choices (of choosing an edge from within edges with the same weight). This is the non-deterministic bit. Of course, when you will implement the algorithm you will make a choice (i.e. impose an order) however a different implementation can very well impose a different order. Thus, you will have two implementations of the algorithm, solving the same problem, correctly but possibly with different end results.