Skip to content

Commit 414dcb3

Browse files
authored
Merge pull request #100 from ishita3513/patch-3
Create count-the-number-of-complete-components
2 parents 3cb4482 + 573875c commit 414dcb3

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
Just 3 main steps|| dfs || connected graph || check adjList
3+
a) Intuition
4+
1. Get the number of connected graphs.
5+
2. For each connected graph, each element should be present in other's adjacency list.
6+
3. If the 2nd step becomes correct, increment count by 1.
7+
b) Approach
8+
1. Make the adjacency list using map.
9+
2. dfs approach to get the list of connected nodes.
10+
3. Check whether each node has edges to other in the connected graph.
11+
-->take the adjency list and add itself to its adjency list.
12+
-->If adjency list == connectedNode list (for each node in the connected graph), count it.
13+
14+
*/
15+
16+
class Solution {
17+
List<Integer> dfs(List<Integer>list,Map<Integer,List<Integer>>map,int key,int[] vis){
18+
//visted again means traversal completed for a given connected chunk of nodes
19+
if(vis[key]==1)return list;
20+
//mark node as visted
21+
vis[key]=1;
22+
//store list in new list
23+
List<Integer>li=new ArrayList<>(list);
24+
li.add(key);
25+
for(int i=0;i<map.get(key).size();i++){
26+
//dfs call for each not visited adjacent node
27+
int adjNode=map.get(key).get(i);
28+
if(vis[adjNode]==0)li=dfs(li,map,adjNode,vis);
29+
}
30+
return li;
31+
}
32+
int conn(List<Integer>list,Map<Integer,List<Integer>>adjList){
33+
int cnt=0;
34+
for(Integer i:list){
35+
//if a node can visit to all other node
36+
//means adj list just lack itself
37+
//if we add it to its own adjacency list,
38+
//adj will be equal to list.
39+
List<Integer>adj=new ArrayList<>(adjList.get(i));
40+
adj.add(i);
41+
Collections.sort(adj);
42+
if(list.equals(adj))cnt++;
43+
}
44+
return cnt==list.size()?1:0;
45+
}
46+
public int countCompleteComponents(int n, int[][] edges) {
47+
//make adjacency list using map
48+
Map<Integer,List<Integer>>map=new HashMap<>();
49+
for(int i=0;i<edges.length;i++){
50+
//undirected graph so, store both nodes in each other
51+
map.putIfAbsent(edges[i][0],new ArrayList<>());
52+
map.get(edges[i][0]).add(edges[i][1]);
53+
map.putIfAbsent(edges[i][1],new ArrayList<>());
54+
map.get(edges[i][1]).add(edges[i][0]);
55+
}
56+
57+
for(int i=0;i<n;i++){
58+
//empty adjacency list for single nodes
59+
map.putIfAbsent(i,new ArrayList<>());
60+
}
61+
int[] vis=new int[map.size()];
62+
int sum=0;
63+
for(Map.Entry<Integer,List<Integer>>m:map.entrySet()){
64+
if(vis[m.getKey()]==0){
65+
List<Integer>list=new ArrayList<>();
66+
//get the list of connected nodes
67+
list=dfs(list,map,m.getKey(),vis);
68+
69+
if(list.size()==1)sum++;
70+
else{
71+
//sort it to match with the adjacency list of particular nodes.
72+
Collections.sort(list);
73+
sum+=conn(list,map);
74+
}
75+
}
76+
}
77+
return sum;
78+
}
79+
}

0 commit comments

Comments
 (0)