-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGRAPH.C
211 lines (190 loc) · 3.48 KB
/
GRAPH.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* GRAPH- BFS and DFS */
#include<conio.h>
#include<stdio.h>
# define MAX 20
int q[ MAX ], top = -1, front = -1, rear = -1, a[ MAX ][ MAX ], vis[ MAX ], stack[ MAX ];
int delete();
void add ( int item );
void bfs( int s, int n );
void dfs( int s, int n );
void push( int item );
int pop();
int main()
{
int n, i, s, ch, j;
char c;
char dummy;
clrscr();
printf( "Enter the number of vertices: ");
scanf( "%d", &n );
printf( "Enter Adjacency Matrix: \n");
for ( i = 0;i < n;i++ )
{
for ( j = 0;j < n;j++ )
{
scanf( "%d",&a[i][j]);
}
}
printf( "The Matrix is ");
for ( i = 0;i < n;i++ )
{
printf("\n");
for ( j = 0;j < n;j++ )
{
printf( " %d", a[ i ][ j ] );
}
}
do
{
for ( i = 1;i <= n;i++ )
{
vis[ i ] = 0;
}
printf( "\nMENU" );
printf( "\n1.BFS\n2.DFS" );
printf( "\nEnter Your Choice: " );
scanf( "%d", &ch );
printf( "Enter the source vertex: " );
scanf( "%d", &s );
switch ( ch )
{
case 1:
bfs( s, n );
break;
case 2:
dfs( s, n );
break;
}
printf( "\nDo You Want To Continue? (Y/N)" );
scanf( "%c", &dummy );
scanf( "%c", &c );
} while ( (c == 'y' ) );
return 0;
}
void bfs( int s, int n )
{
int p, i;
add(s);
vis[s] = 1;
p = delete();
if ( p != 0 )
{
printf( " %d", p );
}
while ( p != 0 )
{
for ( i = 1;i <= n;i++ )
if ( ( a[ p ][ i ] == 1 ) && ( vis[ i ] == 0 ) )
{
add( i );
vis[ i ] = 1;
}
p = delete();
if ( p != 0 )
printf( " %d ", p );
}
for ( i = 1;i <= n;i++ )
if ( vis[ i ] == 0 )
bfs( i, n );
}
void add(int item)
{
if ( rear == MAX-1 )
printf( "\nQUEUE FULL" );
else
{
if ( rear == -1 )
{
q[ ++rear ] = item;
front++;
}
else
q[ ++rear ] = item;
}
}
int delete()
{
int k;
if ( ( front > rear ) || ( front == -1 ) )
return ( 0 );
else
{
k = q[ front++ ];
return ( k );
}
}
void dfs( int s, int n )
{
int i, k;
push( s );
vis[ s ] = 1;
k = pop();
if ( k != 0 )
printf( " %d ", k );
while ( k != 0 )
{
for ( i = 1;i <= n;i++ )
if ( ( a[ k ][ i ] == 1 ) && ( vis[ i ] == 0 ) )
{
push( i );
vis[ i ] = 1;
}
k = pop();
if ( k != 0 )
printf( " %d ", k );
}
for ( i = 1;i <= n;i++ )
{
if ( vis[ i ] == 0 )
dfs( i, n );
}
}
void push( int item )
{
if ( top == MAX-1 )
printf( "Stack overflow " );
else
stack[++top] = item;
}
int pop()
{
int k;
if ( top == -1 )
{
return ( 0 );
}
else
{
k = stack[top--];
return(k);
}
}
/* OUTPUT
Enter the number of vertices: 5
Enter Adjacency Matrix:
0 0 0 1 0
0 0 0 1 1
0 0 0 0 1
1 1 0 0 1
0 1 1 1 0
The Matrix is
0 0 0 1 0
0 0 0 1 1
0 0 0 0 1
1 1 0 0 1
0 1 1 1 0
MENU
1.BFS
2.DFS
Enter Your Choice: 1
Enter the source vertex: 1
1 3 4 2 5
Do You Want To Continue? (Y/N)y
MENU
1.BFS
2.DFS
Enter Your Choice: 2
Enter the source vertex: 1
1 4 2 3 5
Do You Want To Continue? (Y/N)n
*/