Advertisement
Guest User

Untitled

a guest
Jul 31st, 2015
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. struct stack
  4. {
  5. int a[10];
  6. int top;
  7. };
  8. struct stack s;
  9. int a[10][10], visited[10],n;
  10. void dfs(int);
  11. void push(int);
  12. void pop();
  13. void push(int x)
  14. {
  15. if(s.top==9)
  16. printf("Overflow");
  17. else
  18. {
  19. s.top++;
  20. s.a[s.top]=x;
  21. }
  22. }
  23. void pop()
  24. {
  25. if(s.top==-1)
  26. printf("Empty");
  27. else
  28. s.top--;
  29. }
  30. void main()
  31. {
  32. int i,j;
  33.  
  34. s.top=-1;
  35. printf("Enter no of nodes");
  36. scanf("%d", &n);
  37. printf("Enter adjacency Matrix");
  38. for(i=0;i<n;i++)
  39. {
  40. for(j=0;j<n;j++)
  41. scanf("%d",&a[i][j]);
  42. visited[i]=0;
  43. }
  44. printf("\n");
  45. for(i=0;i<n;i++)
  46. {
  47. for(j=0;j<n;j++)
  48. printf("%d\t",a[i][j]);
  49. printf("\t%d",visited[i]);
  50. printf("\n");
  51. }
  52. push(0);
  53. printf("DFS traversal is 0\n");
  54. dfs(0);
  55. getch();
  56. }
  57. void dfs(int v)
  58. {
  59. int x,i,e;
  60. visited[v]=1;
  61. while(s.top!=-1)
  62. {
  63. x=1;
  64. for(i=0;i<n && x==1; i++)
  65. {
  66. if(a[v][i]==1 && visited[i]==0)
  67. {
  68. push(i);
  69. printf("%d\n",i);
  70. x=0;
  71. }
  72. }
  73. i--;
  74. if(x==1)
  75. {
  76. pop();
  77. return;
  78. }
  79. else
  80. e=i;
  81. dfs(e);
  82. }
  83. return;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement