I am trying to develop an algorithm which will be able to find minimum spanning tree from a graph.I know there are already many existing algorithms for it.However I am trying to eliminate the sorting of edges required in Kruskal's Algorithm.The algorithm I have developed so far has a part where counting of disjoint sets is needed and I need a efficient method for it.After a lot of study I came to know that the only possible way is using BFS or DFS which has a complexity of O(V+E) whereas Kruskal's algorithms has a complexity of O(ElogE).Now my question is which one is better,O(V+E) or O(ElogE)?
In general, E = O(V^2)
, but that bound may not be tight for all graphs. In particular, in a sparse graph, E = O(V)
, but for an algorithm complexity is usually stated as a worst-case value.
O(V + E)
is a way of indicating that the complexity depends on how many edges there are. In a sparse graph, O(V + E) = O(V + V) = O(V)
, while in a dense graph O(V + E) = O(V + V^2) = O(V^2)
.
Another way of looking at it is to see that in big-O notation, O(X + Y)
means the same thing as O(max(X, Y))
.
Note that this is only useful when V
and E
might have the same magnitude. For Kruskal's algorithm, the dominating factor is that you need to sort the list of edges. Whether you have a sparse graph or a dense graph, that step dominates anything that might be O(V)
, so one simply writes O(E lg E)
instead of O(V + E lg E)
.