Running time of Kruskal's algorithm

2019-03-28 12:31发布

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 order by weight w
5. for each edge (u,v) ∈ G.E, taken in nondecreasing order by weight w
6.      if FIND-SET(u)!=FIND-SET(v)   
7.         A=A U {(u,v)}  
8.         Union(u,v)
9. return A

According to my textbook:

Initializing the set A in line 1 takes O(1) time, and the time to sort the edges in line 4 is O(E lgE). The for loop of lines 5-8 performs O(E) FIND-SET and UNION operations on the disjoint-set forest. Along with the |V| MAKE-SET operations, these take a total of O((V+E)α(V)) time, where α is a very slowly growing function. Because we assume that G is connected, we have |E| <= |V|-1, and so the disjoint-set operations take O(E α(V)) time. Moreover, since α(V)=O(lgV)=O(lgE), the total running time of Kruskal's algorithm is O(E lgE). Observing that |E|<|V|^2, we have lg |E|=O(lgV), and so we can restate the running time of Kruskal's algorithm as O(E lgV).

Could you explain me why we deduce that the time to sort the edges in line 4 is O(E lgE)? Also how do we get that the total time complexity is O((V+E)α(V)) ?

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? What if the edges weights are integers in the range from 1 to W for some constant W?

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

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) 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.

So if we use Counting Sort in order to solve the edges, the time complexity of Kruskal will be

enter image description here

Could you tell me if my idea is right?

Also:

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

We will again use Counting Sort. The algorithm will be the same. We find the time complexity as follows:

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

So the time complexity will be:

enter image description here

1条回答
神经病院院长
2楼-- · 2019-03-28 12:43

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.

查看更多
登录 后发表回答