Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create connected-components.md #269

Closed
wants to merge 8 commits into from
89 changes: 89 additions & 0 deletions tutorials/graph-theory/connected-components.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
title: 'Connected-Components'
description: 'Author: @adityabisht02'
hide_table_of_contents: true
---

<TutorialAuthors names="Aditya Bisht"/>

## Overview
In a graph, sometimes all nodes might not be connected with each other. Let's take an example of a graph with 5 nodes-
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's take an example of a graph with 5 nodes.



![image](https://user-images.githubusercontent.com/89146189/195919108-2e06dbbe-717a-4f86-9ba1-90cbdf5c119c.png)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer a better image


Here you can see that 1-2-3 are connected and 4-5 are connected.
Here , 1-2-3 is called one connected component.

## How to determine if a connected component is present ?
1. Start a BFS or DFS from any node of the graph.
2. If you are able to visit all the other nodes from that single node that means the all nodes in the graph are connected.
3. If you are unable to reach a particular node or a group of nodes, then that group of nodes might be a different connected component.

## How to find connected components?
1. Let's an adjacency list based graph is given.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's create an adjacency list based on the given graph

2. Traverse the adjacency list and for every node do a BFS or DFS on the node and mark all the nodes you visit in this way as visited.
3. While traversing the list do not do BFS on the nodes which have already been visited.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or DFS

4. Hence everytime you do a BFS/DFS you are traversing a connected component so you can count the number of times you do a BFS/DFS.


## Sample question:
https://leetcode.com/problems/number-of-provinces/

Solution:

class Solution {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

format the code following K&R Coding style


//bfs function
public void bfs(int isConnected[][],int visited[],int node){

Queue<Integer> q=new LinkedList<>();
q.offer(node);

while(!q.isEmpty()){
int current=q.poll();
visited[current]=1;

for(int i=0;i<isConnected[0].length;i++){
if(isConnected[current][i]!=0 && visited[i]!=1){
q.offer(i);
visited[i]=1;
}
}
}


}

public int findCircleNum(int[][] isConnected) {

int i,j,provincecount=0;
int visited[]=new int[isConnected.length];


//traverse through the adjacency array
for(i=0;i<isConnected.length;i++){

if(visited[i]==1){
continue;
}
for(j=0;j<isConnected[0].length;j++){

//if not visited and is connected
if(visited[j]==0 && isConnected[i][j]==1){
//everytime u do a bfs u complete one connected component
bfs(isConnected,visited,j);
provincecount++;
}

}
}

return provincecount;
}
}

## Similar problems on leetcode:
1. Number of Islands - https://leetcode.com/problems/number-of-islands/
2. Number of Connected Components- https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/