-
Notifications
You must be signed in to change notification settings - Fork 0
/
DFS_NITMAS.c
64 lines (58 loc) · 899 Bytes
/
DFS_NITMAS.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
#include<stdio.h>
#include<stdlib.h>
int ar[20][20];
int q[20];//not queue but stack
int top=-1;
int pop(){
if(top==-1) return -1;
else{
top--;
return q[top+1];
}
}
void push(int num){
top++;
q[top]=num;
}
void traverse(int n,int src){
int del,i;
int *p=(int*)calloc(n,sizeof(p));
push(src); p[src]=1;
for(;top!=-1;){
del=pop();
printf("%d ",del);
for(i=0;i<n;i++){
if(ar[del][i]==1 && p[i]==0){
push(i);
p[i]=1;
}
}
}
}
int main(){
int i=20,j,n,t,a,b,src;
while(i--) for(j=0;j<20;j++) ar[i][j]=0;
printf("Enter number of Vertices: ");
scanf("%d",&n);
printf("Enter number of Edges: ");
scanf("%d",&t);
printf("Enter the Edges:\n");
while(t--){
scanf("%d%d",&a,&b);
ar[a][b]=ar[b][a]=1;
}
printf("Enter the Source Vertex: ");
scanf("%d",&src);
printf("The D.F.S. traversal is: ");
traverse(n,src);
return 0;
}
/*
6
5
0 1
1 2
0 2
3 4
4 4
*/