Running time of Kruskal's algorithm

后端 未结 1 726
醉话见心
醉话见心 2021-02-09 05:04

The Kruskal\'s algorithm is the following:

MST-KRUSKAL(G,w)
1. A={}
2. for each vertex v∈ G.V
3.      MAKE-SET(v)
4. sort the edges of G.E into nondecreasing ord         


        
1条回答
  •  我在风中等你
    2021-02-09 05:07

    Could you explain me why we deduce that the time to sort the edges in line 4 is O(E*lgE)?

    To sort a set of N items we use O(Nlg(N)) algorithm, which is quick sort, merge sort or heap sort. To sort E edges we therefore need O(Elg(E)) time. This however is not necessary in some cases, as we could use sorting algorithm with better complexity (read further).

    Also how do we get that the total time complexity is O((V+E)α(V))?

    I don't think total complexity is O((V+E)α(V)). That would be complexity of the 5-8 loop. O((V+E)α(V)) complexity comes from V MAKE-SET operations and E Union operations. To find out why we multiply that with α(V) you will need to read in depth analysis of disjoint set data structure in some algorithmic book.

    How fast can you make Kruskal's algorithm run?

    For first part, line 4, we have O(E*lg(E)) complexity and for second part, line 5-8, we have O((E+V)α(V)) complexity. This two summed up yield O(Elg(E)) complexity. If we use O(N*lg(N)) sort this can't be improved.

    What if the edges weights are integers in the range from 1 to W for some constant W?

    If that is the case, than we could use counting sort for first part. Giving line 4 complexity of O(E+W) = O(E). In that case algorithm would have O((E+V)*α(V)) total complexity. Note that however O(E + W) in reality includes a constant that could be rather large and might be impractical for large W.

    How does the time complexity depend on the weight of the edges?

    As said, if weight of the edges is small enough we can use counting sort and speed up the algorithm.

    EDIT:

    In addition, suppose that all edge weights in a graph are integers from 1 to |V|. How fast can you make Kruskal's algorithm run? I have thought the following:

    In order the Kruskal's algorithm to run faster, we can sort the edges applying Counting Sort.

    The line 1 requires O(1) time. The lines 2-3 require O(vα(|V|)) time. The line 4 requires O(|V|+|E|) time. The lines 5-8 require O(|E|α(|V|)) time. The line 9 requires O(1) time.

    Your idea is correct, however you can make bounds smaller.

    The lines 2-3 requires O(|V|) rather than O(|V|α(|V|)). We however simplified it to O(|V|α(|V|)) in previous calculations to make calculations easier.

    With this you get the time of: O(1) + O(|V|) + O(|V| + |E|) + O(|E|α(|V|)) + O(1) = O(|V| + |E|) + O(|E|α(|V|))

    You can simplify this to either O((|V| + |E|) * α(|V|) or to O(|V| + |E|*α(|V|).

    So while you were correct, since O((|V| + |E|) * α(|V|) < O((|V| + |E|) * lg(|E|)

    Calculations for the |W| are analogous.

    0 讨论(0)
提交回复
热议问题