forked from laviii123/Btecky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DFSTraversal
46 lines (44 loc) · 861 Bytes
/
DFSTraversal
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
#include <stdio.h>
int stack[100];
int a[10][10],visited[10];
int n;
int top=-1;
void push(int x){
top++;
stack[top]=x;
}
int pop(){
return stack[top--];
}
void DFS(int v)
{
int i;
push(v);
while(top!=-1)
{
v = pop();
if(visited[v]==0)
{
printf("%d ",v);
visited[v]=1;
}
for(i=1; i<=n; i++)
{
if(a[v][i]==1 && visited[i]==0)
push(i);
}
}
}
int main()
{
printf("give no of vertices:");
scanf("%d",&n);
printf("give matrix:\n");
for(int i=1;i<=n;i++){
visited[i]=0;
for(int j=1;j<=n;j++){
scanf("%d",&a[i][j]);
}
}
DFS(2);
}