# Answer to Question #36809 in C++ for sansia

Question #36809

plz tell me d error in this dfs program in c...

#include<stdio.h>

#include<conio.h>

int stack[20],top=-1;

int vis[20];

int a[20][20];

void dfs(int s,int n);

void push(int item);

int pop();

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]!=0)&&(vis[i]=0))

{

push(i);

vis[i]=1;

}

k=pop();

if(k!=0)

printf("\n%d",k);

}

for(i=1;i<=n;i++)

if(vis[i]==0)

dfs(i,n);

}

void push(int item)

{

if(top==19)

{

printf("\nstack overflow");

}

top++;

stack[top]=item;

}

int pop()

{

if(top==-1)

{

printf("\nstack is empty");

}

else

{

printf("%d element is popped out of the stack",stack[top--]);

}

}

int main()

{

int i,j,s,n;

printf("\nenter the no of vertices");

scanf("%d",&n);

for(i=1;i<=n;i++)

{

for(j=1;j<=n;j++)

{

printf("\nEnter 1 if %d has a node %d else 0",i,j);

scanf("%d",&a[i][j]);

}

}

printf("\nADJACENCY MATRIX:");

for(i=1;i<=n;i++)

{

for(j=1;j<=n;j++)

{

printf("\n%d",a[i][j]);

}

}

for(i=1;i<=n;i++)

vis[i]=0;

printf("\nEnter the source vertex");

scanf("%d",&s);

dfs(s,n);

getch();

return(0);

}

#include<stdio.h>

#include<conio.h>

int stack[20],top=-1;

int vis[20];

int a[20][20];

void dfs(int s,int n);

void push(int item);

int pop();

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]!=0)&&(vis[i]=0))

{

push(i);

vis[i]=1;

}

k=pop();

if(k!=0)

printf("\n%d",k);

}

for(i=1;i<=n;i++)

if(vis[i]==0)

dfs(i,n);

}

void push(int item)

{

if(top==19)

{

printf("\nstack overflow");

}

top++;

stack[top]=item;

}

int pop()

{

if(top==-1)

{

printf("\nstack is empty");

}

else

{

printf("%d element is popped out of the stack",stack[top--]);

}

}

int main()

{

int i,j,s,n;

printf("\nenter the no of vertices");

scanf("%d",&n);

for(i=1;i<=n;i++)

{

for(j=1;j<=n;j++)

{

printf("\nEnter 1 if %d has a node %d else 0",i,j);

scanf("%d",&a[i][j]);

}

}

printf("\nADJACENCY MATRIX:");

for(i=1;i<=n;i++)

{

for(j=1;j<=n;j++)

{

printf("\n%d",a[i][j]);

}

}

for(i=1;i<=n;i++)

vis[i]=0;

printf("\nEnter the source vertex");

scanf("%d",&s);

dfs(s,n);

getch();

return(0);

}

Expert's answer

## Comments

## Leave a comment