The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+12 votes
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 _______
asked in Algorithms by Veteran (59.5k points) | 2.4k views

5 Answers

+21 votes
Best answer

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

answered by Boss (13.5k points)
edited by
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
0 votes

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.youtube.com/watch?v=fAuF0EuZVCk

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

answered by (131 points)
0 votes
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
answered by (11 points)
–2 votes
Though edges are sorted still due to union find operation complexity is O(mlogn).
answered by Active (4k points)
–2 votes
ANSWER SHOULD BE O(V+E).
answered by Active (4.3k points)
+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.


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

38,051 questions
45,543 answers
131,851 comments
48,879 users