SEE Practical Exam
- Due No Due Date
- Points 0
- Submitting a file upload
Write missing code for DFS, highlighted with comments.
Give input for given graph (Start from 0).
#include<stdio.h>
#include<conio.h>
//Give input
int stack[20];
//declare array
int top=-1;
void dfs(int s);
void push(int item);
int pop();
void main()
{
int i,j,s;
printf("\n\n THE ADJACENCY MATRIX IS \n\n");
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf(" %d ",a[i][j]);
}
printf("\n");
}
printf("\n\n ENTER THE SOURCE VERTEX : ");
scanf("%d",&s);
printf("\n\n DFS TRAVERSAL IS : ");
dfs(s);
getch();
}
void dfs(int s)
{
int i,k;
push(s);
visit[s]=1;
k=pop();
if(k!=-1)
{
printf(" %d ",k);
visit[k]=1;
}
while(k!=-1)
{
for(i=n-1;i>=0;i--)
{
if(a[k][i]==1 && visit[i]==0)
{
push(i);
}
}
k=pop();
if(k!=-1)
{
if(visit[k]==0)
{
printf(" %d ",k);
visit[k]=1;
getch();
}
}
}
}
//Define push() function
//Define pop() function