nkb29597

DFS

Oct 22nd, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.54 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #define n 4
  4. int count, visited[n];
  5. void dfs(int g[n][n],int v)
  6. {
  7.     int u;
  8.     count=count+1;
  9.     visited[v]=count;
  10.     printf("\n %d",v);
  11.     for(u=0;u<n;u++)
  12.         if(g[v][u]!=0 && visited[u]==0)
  13.             dfs(g,u);
  14. }
  15. void DFST(int g[n][n])
  16. {
  17.     int v;
  18.     count=0;
  19.     for(v=0;v<n;v++)
  20.         visited[v]=0;
  21.     for(v=0;v<n;v++)
  22.         if(visited[v]==0)
  23.             dfs(g,v);
  24. }
  25. void main()
  26. {
  27.     int g[n][n]= {{0,0,1,1},{1,0,0,0},{1,1,0,0},{0,0,0,1}};
  28.     DFST(g);
  29.     getch();
  30. }
Advertisement
Add Comment
Please, Sign In to add comment