Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<conio.h>
- struct stack
- {
- int a[10];
- int top;
- };
- struct stack s;
- int a[10][10], visited[10],n;
- void dfs(int);
- void push(int);
- void pop();
- void push(int x)
- {
- if(s.top==9)
- printf("Overflow");
- else
- {
- s.top++;
- s.a[s.top]=x;
- }
- }
- void pop()
- {
- if(s.top==-1)
- printf("Empty");
- else
- s.top--;
- }
- void main()
- {
- int i,j;
- s.top=-1;
- printf("Enter no of nodes");
- scanf("%d", &n);
- printf("Enter adjacency Matrix");
- for(i=0;i<n;i++)
- {
- for(j=0;j<n;j++)
- scanf("%d",&a[i][j]);
- visited[i]=0;
- }
- printf("\n");
- for(i=0;i<n;i++)
- {
- for(j=0;j<n;j++)
- printf("%d\t",a[i][j]);
- printf("\t%d",visited[i]);
- printf("\n");
- }
- push(0);
- printf("DFS traversal is 0\n");
- dfs(0);
- getch();
- }
- void dfs(int v)
- {
- int x,i,e;
- visited[v]=1;
- while(s.top!=-1)
- {
- x=1;
- for(i=0;i<n && x==1; i++)
- {
- if(a[v][i]==1 && visited[i]==0)
- {
- push(i);
- printf("%d\n",i);
- x=0;
- }
- }
- i--;
- if(x==1)
- {
- pop();
- return;
- }
- else
- e=i;
- dfs(e);
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement