-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_dfs.c
95 lines (90 loc) · 1.86 KB
/
graph_dfs.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<stdio.h>
#include<stdlib.h>
#define infinity 99999
#define white 1
#define grey 2
#define black 3
#define nil -1
#define MAX 100
#define dir 1
#define undir 0
void push(int stack[100],int data,int *top);
int pop(int stack[100],int *top);
void creategraph(int arr[][MAX],int v,int d_un);
void display(int arr[][MAX],int v);
void dfs(int arr[][MAX],int v,int s,int stack[],int *top,int state[],int path_l[],int pred[]);
int main(){
int q[MAX],front = -1;
int graph[MAX][MAX] = {0}, pathlength[MAX], pred[MAX], state[MAX];
int v,s,des,d_un,i,c =0;
scanf("%d%d",&v,&d_un);
for(i = 0; i<v; i++){
pathlength[i] = 0;
pred[i] = nil;
state[i] = white;
}
creategraph(graph,v,d_un);
display(graph,v);
for(i = 0; i<v; i++){
if(state[i] == white){
dfs(graph,v,i,q,&front,state,pathlength,pred);
c++;
printf("\n");
}
}
printf("%d connected components in this graph\n",c);
return 0;
}
void dfs(int arr[][MAX],int v,int s,int stack[],int *top,int state[],int path_l[],int pred[]){
int i;
push(stack,s,top);
while(*top != -1){
s = pop(stack,top );
if(state[s] == white)
printf("%d->",s);
state[s] = black;
for(i = v-1; i>=0; i--){
if(arr[s][i] != 0 && state[i] == white)
push(stack,i,top);
}
}
printf("\n");
}
void creategraph(int arr[][MAX],int v,int d_un){
int i,j;
while(1){
scanf("%d%d",&i,&j);
if(i<v && j<v && i>=0 && j>=0){
arr[i][j] = 1;
if(d_un == undir)
arr[j][i] = 1;
}
else break;
}
}
void display(int arr[][MAX],int v){
int i,j;
for(i=0;i<v; i++){
for(j =0; j<v; j++)
printf("%d ",arr[i][j]);
printf("\n\n");
}
}
void push(int stack[100],int data,int *top) {
if(*top == 99){
printf("overflow\n");
return ;
}
(*top)++;
stack[*top] = data;
}
int pop(int stack[100],int *top) {
if(*top == -1) {
printf("underflow\n");
return -1;
}
int data ;
data = stack[*top];
(*top)--;
return data;
}