retagged by
27,105 views
48 48 votes
Complexity of Kruskal’s algorithm for finding the minimum spanning tree of an undirected graph containing $n$ vertices and $m$ edges if the edges are sorted is _______

13 Answers

0 0 votes
IF EDGES ARE ALREADY SORTED, THEN IN WORST CASE WE HAVE TO TRAVERSE THE WHOLE EDGES TO GET MINIMUM SPANNING TREE. M EDGES WILL BE TRAVERSED AND WE HAVE TO DO UNION OPERATION WHICH TAKE O(LOGN) TIME, SO COMPLEXITY WILL BE Mlog(n).
0 0 votes

Since the edges are sorted, we simply need to visit the vertex, and select an edge.

Now, Selecting an edge means, check if the edge creates a cycle or not and if not then insert. It does not consume a lot of time, we can assume it to be an O(1) operation.

So we need to visit all the vertices: O(n)

And we need to perform Select operation for m edges: O(m) (worst case).

So overall time complexity is O(m+n).

0 0 votes

Detailed Analysis
 

Time Complexity analysis of Kruskal = Time to sort the edges in inreasing order (A) + time to detect cycle for every edges (B)

Since, the edges are already sorted (given in question). Hence, A = 0

Let, number of edges = E and, number of vertices = V

Now for B, B = E (no. of edges) * time to detect cycle

Time Complexity of detecting cycle

Using Union-Find : O(1) i.e, contant time
Using DFS/BFS = O(V+E)

Since, in question, which algorithm is used to detect cycle is not given, we will consider Union-Find Algorithm
Therefore, Time Complexity analysis of Kruskal = A + B = 0 + E*(O(1)) = O(E) = O(m) ------ (Final Answer)

Extended Analysis

If BFS/DFS is used for cycle detection,
Time Complexity analysis of Kruskal = A + B = 0 + E*(V+E) = VE + V^2

Case 1: If the  graph is densed, then E = V^2 (approx)

Case 2: If the graph is sparsed, then E = V (approx)

Therefore, for Case 1: Time Complexity of Kruskal = VE + V^2 = V*(V^2) + (V^2)^2 = O(V^4) = O(n^4)
And, for Case 2: Time Complexity of Kruskal = VE + V^2 = V*(V) + V^2 = O(V^2) = O(n^2)

0 0 votes

O(m α(n))​ — using Union–Find with union by rank + path compression (since sorting is already done).
(In practice α(n) is tiny, so it’s ~O(m). With a naïve DSU it would be O(m log⁡ n))

Let's understand easy way

Kruskal’s Algorithm Steps

  1. Sort all edges by weight.

  2. Take edges one by one (from smallest to largest).

  3. Use Union–Find (Disjoint Set Union, DSU) to check if adding the edge makes a cycle.

    • If not, include it in the MST.

    • If yes, skip it.

Now complexity:

  • Sorting edges → Normally O(mlog⁡m).
    But in the question it says edges are already sorted, so this part is skipped.

  • Union–Find operations → For each of the mmm edges, we do at most 2 finds + 1 union.
    With efficient DSU (path compression + union by rank),
    each operation is almost constant: O(α(n)),
    where α(n) = inverse Ackermann function (grows super slowly, < 5 for all practical inputs).

So total = O(m⋅α(n))

which is basically linear in number of edges.

Final Answer:
If edges are already sorted, Kruskal’s algorithm runs in O(m α(n)) time.
(Without sorting, it would be O(mlog⁡m).)

 

–1 –1 vote
ANSWER SHOULD BE O(V+E).
Position:
Show:

Related questions

21 21 votes
4 answers 4 answers
9.1k
9.1k views
Kathleen asked Sep 12, 2014
9,058 views
Maximum number of edges in a planar graph with $n$ vertices is _____
10 10 votes
4 4 answers
3.0k
3.0k views
Kathleen asked Sep 12, 2014
2,999 views
Macro expansion is done in pass one instead of pass two in a two pass macro assembler because _________
16 16 votes
2 answers 2 answers
5.9k
5.9k views
Kathleen asked Sep 12, 2014
5,897 views
A simple and reliable data transfer can be accomplished by using the 'handshake protocol'. It accomplishes reliable data transfer because for every data item sent by the ...
13 13 votes
5 answers 5 answers
5.9k
5.9k views
Kathleen asked Sep 12, 2014
5,874 views
Many of the advanced microprocessors prefetch instructions and store it in an instruction buffer to speed up processing. This speed up is achieved because ________