2.4k views
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 _______

If all edges are already sorted then this problem will reduced to union-find problem on a graph with $E$ edges and $V$ vertices.

for each edge (u,v) in E

if(FIND-SET(u) != FIND-SET(v))
UNION(u,v)


FIND-SET$(v)$ and UNION$(u,v)$ runs in $α(|V|)$

where $α(n)$ is inverse ackermann function i.e $\log^*(n)$

So, overall complexity becomes $O(|E|.α(|V|))$

edited
0

I think the answer should simply be O(E)

https://en.wikipedia.org/wiki/Proof_of_O(log*n)_time_complexity_of_union%E2%80%93find

According to wikipedia amortized time complexity of unoin find is O(log*V), as Arjun said in one other answer log*n is practically a constant. For E  unoin find operations, the answer would simply be E*O(1), Therefore O(E)

+1

Complexity of kruskal is O(ElogE + E) .... ElogE for sorting the edges  and E for union operation of E edges ...

So here it is sorted ... so O(E) ... correct me if i am wrong ... Refer this ...

+1
answer should be $O(ElogV)$ using disjoint data structure..
0

reena_kandari But here nothing is mentioned about completeness or not ! so its could be $n^2logn$ or $mlogn$ correct me if i am wrong?

0
@Reena

Since edges are already sorted , So it will not take O(ElogV) using disjoint data structure..
0
#reena can u tell me y it is O(ElogV) ??
0

@reena_kandari  But that time complexity of O(ElogV) is for the whole 'Krushkal minimum spanning tree' algorithm, i.e, when the edges need to be sorted. According to the question the edges have already been sorted, so O(ElogV) is irrelevant here. Only need to consider union find operation on the E edges and each union find operation takes a constant amount of time, video posted above by @Puja Mishra explains this. Thus O(1)E (E times O(1)), = O(E).

+1
Kruskal algorithm:-

step 1: sort the all edges

step 2: for each edge $E$ from sorted list add it to kruskal forest and check if it doesn't form a cycle--

using union find data structure it will take $E$*$logV$ .here $log V$ is TC of "find" and "union" operation.

some are saying that we can use DFS here but after adding each edge we have to run DFS as many times as edges are i.e.$E*O(V+E)$. if we do aggregate analysis here it cannt be smaller that $E(logV)$.
0

@reena_kandari  the amortized version of union find runs in O(1), check https://www.geeksforgeeks.org/union-find-algorithm-set-2-union-by-rank/

Quote from above geeksforgeeks page

The time complexity of each operations becomes even smaller than O(Logn). In fact, amortized time complexity effectively becomes small constant.

0
this is space complexity

Time complexity for the whole 'Krushkal minimum spanning tree' algorithm, i.e, when the edges need to be sorted is O(ElogV). According to the question the edges have already been sorted, so O(ElogV) is irrelevant here. Only need to consider union find operation on the E edges and each union find operation takes a constant amount of time, video posted below explains this. Thus O(1)E (E times O(1)), = O(E). The time complexity of the accepted answer is the case when 'path compression' and 'union by rank' methods have not been implemented. For amortized union find operation which implements 'path compression' and 'union by rank' methods, the time complexity is O(1) [Check geeksforgeeks link below]. Thus time complexity for this question should be O(E)

https://www.geeksforgeeks.org/union-find-algorithm-set-2-union-by-rank/

Find operation will take at most  ~2m time
& union operation will take at most  ~nlogn
So overall complexity = 2m+nlogn
& the biggest term is mlogn since m>n for connected graph where
m is no.  of Edges & n is no.of vertices
Though edges are sorted still due to union find operation complexity is O(mlogn).
+1
Answer will be O(E LogV)  because we have to check for all the edges if there is any cycle while including in MST.

Some have given answer O(E+V) which is wrong.