Guest User

Untitled

a guest
Mar 24th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. #include<stdio.h>
  2. int a[20][20],visited[20],q[20],n,i,j,f=0,r=-1;
  3. void bfs(int v)
  4. {
  5. for(i=1;i<=n;i++)
  6. {
  7. if(a[v][i] && !visited[i])
  8. {
  9. q[++r]=i;
  10. }
  11. }
  12. if(f<=r)
  13. {
  14. visited[q[f]]=1;
  15. bfs(q[f++]);
  16. }
  17.  
  18. }
  19. void main()
  20. {
  21. int v;
  22. printf("Enter the number of vertices\n");
  23. scanf("%d",&n);
  24. for(i=1;i<=n;i++)
  25. {
  26. visited[i]=0;
  27. q[i]=0;
  28. }
  29. printf("Enter the adjacency matrix elements\n");
  30. for(i=1;i<=n;i++)
  31. {
  32. for(j=1;j<=n;j++)
  33. {
  34. scanf("%d",&a[i][j]);
  35. }
  36. }
  37. printf("Enter the vertex to begin from\n");
  38. scanf("%d",&v);
  39. bfs(v);
  40. printf("Reachable nodes are\n");
  41. for(i=1;i<=n;i++)
  42. if(visited[i])
  43. {
  44. printf("reachable node:- %d\n",i);
  45. }
  46. else
  47. {
  48. printf("not reachable node:- %d\n",i);
  49. }
  50. }
Add Comment
Please, Sign In to add comment