Skip to content

Latest commit

 

History

History
32 lines (19 loc) · 1.43 KB

05.ConnectedComponentsIngraph.md

File metadata and controls

32 lines (19 loc) · 1.43 KB

Connected Components in the Graph

If this figure of graph is given in a single question, then you can't say that there are 3 graphs, rather you'll say that this is a disconnected single graph having 3 components.

Screenshot from 2021-08-27 22-11-15

This is a single graph having 4 different components

Screenshot from 2021-08-27 22-13-42

Even a single node can be called as component

Next we'll know the traversal techniques(DFS and bFS), but before starting that let us see what are the prerequisites, like something which will be used in every code.

  • You'll always have to take a visited array and initialize it with 0
  • You have to traverse through all the components of the graph, so you'll write:
    for(i=1; i<=n; i++){
          if(!vis[i]){
              //write your code here
              //bfs or  dfs whatever you want
          }
      }

// this for loop and visited array will make sure that all the disconnected components should be visited.

You have to always take care of this, you cannot just write the function, you have to write your function for all the components so always take care of this, unless or until the question mentions that the graph has a single component.