-
Notifications
You must be signed in to change notification settings - Fork 117
/
Graphs:All connected components
45 lines (45 loc) · 1.39 KB
/
Graphs:All connected components
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Queue;
// import java.io.*;
import java.util.*;
public class Solution{
public static void help(int edges[][],boolean visited[],ArrayList<Integer> arr,int start){
visited[start]=true;
arr.add(start);
int n=edges.length;
for(int j=0;j<n;j++){
if(edges[start][j]==1&&!visited[j]){
help(edges,visited,arr,j);
}
}
}
public static void helpp(int edges[][]){
boolean visited[]=new boolean[edges.length];
for(int i=0;i<edges.length;i++){
if(!visited[i]){
// this array list is creating again and again
ArrayList<Integer> arrans= new ArrayList<Integer>();
help(edges,visited,arrans,i);
Collections.sort(arrans);
for(int j=0;j<arrans.size();j++)
System.out.print(arrans.get(j)+" ");
System.out.println();
}
}
}
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int n = s.nextInt();
// total number of edges e
int e = s.nextInt();
int edges[][]=new int[n][n];
for(int i=0;i<e;i++){
int fv=s.nextInt();
int sv=s.nextInt();
edges[fv][sv]=1;
edges[sv][fv]=1;
}
helpp(edges);
}
}