in Algorithms edited by
8,136 views
23 votes

The most efficient algorithm for finding the number of connected components in an undirected graph on $n$ vertices and $m$ edges has time complexity

  1. $\Theta(n)$
  2. $\Theta(m)$
  3. $\Theta(m+n)$
  4. $\Theta(mn)$
in Algorithms edited by
8.1k views

2 Comments

Either  BFS or DFS both can do it in $\theta(m+n)$ time.
2
But then we can use disjoint sets to find the number of connected components. Its time complexity is only O(m). Why not option B??
0

Subscribe to GO Classes for GATE CSE 2022

5 Answers

28 votes
 
Best answer

Run DFS to find connected components. Its time complexity is $\Theta(m+n)$, hence (C) is the answer.

edited by

23 Comments

edited by

The explanation of this ans can be find in :

see first 15 minuets portion 



 


12
But then we can use disjoint sets to find the number of components. Its time complexity is only O(m). Why not option B??
0

@Vivek Jain can you explain how to use disjoint sets?? I'm not getting what is your approach??

According to me it should be O(m+n) by using dfs or bfs.

 

0

@Verma Ashish

What if we use Kruskal or Prims here??

0
We can't use prims or kruskal algorithm for finding connected components..
0
But why? It finds shortest path. right? So, finds connected component too. Isnot it?
0
edited by
Prims algorithm can't be applied on disconnected components (forest).

But if we talk in terms of complexity then prims and kruskal both take $O(m\log n)$ time. And if graph is dense then m=n(n-1)/2 (for complete graph).

Here our intention is not to calculate minimum cost (or mst) , and finding minimum edge(sorting) is going to increase complexity in prim/kruskal.
4
And these algos will not work for directed graphs..

So in case of directed graph also bfs/dfs are used for finding strongly connected components.. in O(m+n)
2

@Verma Ashish what about sparse graph?

0

I think DFS and BFS both work for unweighted graph and not weighted graph. and undirect or directed doesnot matter much for these algos. Right?

And In DFS among vertices and edges , which will be greater will be answer, Means if $m> n$, ans will be $\Theta \left ( m \right )$ and if $m< n$ ans will be $\Theta \left ( n \right )$. right?

Where as kruskal, prims takes $\Theta \left ( mn \right )$ in worst case,

Which is much larger time than BFS and DFS.

0

what about sparse graph?

O(n logn)

We can't apply directly prims algorithm to disconnected graph. We might be able if we do some modifications.

 

Bfs/dfs don't care about weighted or unweighted graph. These are just search traversals which forms a tree.(need not be minimum cost tree). 

0

 

and undirect or directed doesnot matter much for these algos. Right? 

Yes. (And prim and kruskal only work for undirected graph)

And In DFS among vertices and edges , which will be greater will be answer, Means if m>n, ans will be Θ(m) and if m<n ans will be Θ(n). right? 

Hmm..

0

We can't apply directly prims algorithm to disconnected graph

DFS and BFS algo also doesnot support disconnected graph .

1
We can apply DFT/BFT (depth first traversal and breadth first traversal)
0
I havenot got what u mean . Can u explain it clearly?
0

@Verma Ashish

Dijkstra uses BFS but Dijkstra T.C. more than BFS. Right?

That is why BFS also not a suitable option here. right??


Another question coming here, though Dijkstra uses BFS, but why Dijkstra T.C. greater than BFS?? Can u answer it?

0
0

@Verma Ashish @Rajesh Pradhan What if the question was about "directed graphs"? How do we find connected components in a directed graph?

0
In directed graph same algorithm will work, you get theta(V+E) refer CLRS .
0
I think that is $(V+E)\log V$??
0

@`JEET

How you get (V+E) log V ? 

0
It's not mentioned that the graph is weighted, so no option for using Prim's and Kruskal's.
0
edited by

@Aks9639 Yes found it. There is no such thing as "connected components" in a directed graph. We use the term "connected  components" for undirected graphs and "strongly connected components" for directed graphs. Finding strongly connected components in a directed graph is given by Kosaraju's algo which is an application of DFS. So TC is O(V+E).

2
19 votes

Both BFS or DFS can be used to find number of connected components.

The Time complexity of BFS and DFS is $\Theta$(m+n).

Hence C is Ans

2 Comments

$\Theta (m+n)$ when BFS and DFS is implemented using adjacency list, else it is $\Theta (n^2)$ when implemented using adjacency matrix.
7
Or we can say Max(m,n)
0
0 votes

To find the number of connected components using either BFS or DFS time complexity is θ(m+n).
Suppose if we are using Adjacency matrix means it takes θ(n2).

0 votes
BFS and DFS is used for traversing the graph which takes O(m+n).
–2 votes
Answer is C.

6 Comments

Question asks for connected components in an undirected graph, you have given reference for finding strongly connected components in a directed graph.
2
ok... but is it really effect ? i mean we cant use for undirected graph ...

see this ...

http://www.geeksforgeeks.org/strongly-connected-components/
1
This is an overkill for undirected graphs. For undirected graphs, you need to run DFS just once.
2
ok ok....  Thank you ... :)
0
what about the directed graphs???
0
Answer:

Related questions