-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectedComponents.c
68 lines (64 loc) · 1.76 KB
/
ConnectedComponents.c
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<stdio.h>
#include<stdlib.h>
#include "graph.c"
struct Queue *Q;
// BFS code for a graph stored in an Adjacency List
int *bfs(Node **AdjLst, int n, int start, int *visited){
Node *temp=NULL;
int i, val;
Q=initQueue(n);
Enqueue(Q, start);
visited[start]=1;
while(length(Q)!=0){
val=Dequeue(Q);
temp=AdjLst[val];
while(temp!=NULL){
val=temp->val;
if(visited[val]==0){
Enqueue(Q, val);
visited[val]=1;
}
temp=temp->next;
}
}
return (visited);
}
// Code to input an Adjacency List
Node **Inp(Node **AdjLst, int n){
Node *newnode=NULL, *head, *top;
int i, j, val;
for(i=0; i<n; i++){
printf("Vertices adjacent to %d: ", i);
top=NULL;
head=NULL;
for(j=0; j<n; j++){
scanf("%d", &val);
if(val==-1) break;
newnode=getnode(val);
if(top==NULL) top=newnode;
else head->next=newnode;
head=newnode;
}
AdjLst[i]=top;
}
return (AdjLst);
}
void main(){
Node **AdjLst=NULL;
int n, *visited, i, j, count=0;
printf("Enter no. of nodes: ");
scanf("%d", &n);
AdjLst=(Node **)malloc(n*sizeof(Node *));
visited=(int *)malloc(n*sizeof(int));
for(i=0; i<n; i++) visited[i]=0;
AdjLst=Inp(AdjLst, n);
for(i=0; i<n; i++){
if(visited[i]==0){ // If the node has not been visited by one iteration of bfs,
visited=bfs(AdjLst, n, i, visited); // that means that it belongs to a different connected component of the graph
count+=1;
}
}
printf("Number of connected components = %d\n", count);
free(visited);
Free(AdjLst, n);
}