Advertisement
nkb29597

BFS

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