Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. void DFS(int);
  4. int G[10][10],visited[10],n; //n is no of vertices and graph is sorted in array G[10][10]
  5.  
  6. void main()
  7. {
  8. int i,j;
  9. printf("Enter number of vertices:");
  10.  
  11. scanf("%d",&n);
  12.  
  13. //read the adjecency matrix
  14. printf("\nEnter adjecency matrix of the graph:");
  15.  
  16. for(i=0;i<n;i++)
  17. for(j=0;j<n;j++)
  18. scanf("%d",&G[i][j]);
  19.  
  20. //visited is initialized to zero
  21. for(i=0;i<n;i++)
  22. visited[i]=0;
  23.  
  24. DFS(0);
  25. }
  26.  
  27. void DFS(int i)
  28. {
  29. int j;
  30. printf("\n%d",i);
  31. visited[i]=1;
  32.  
  33. for(j=0;j<n;j++)
  34. if(!visited[j]&&G[i][j]==1)
  35. DFS(j);
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement