Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<conio.h>
- #define n 4
- int count, f=0, r=-1, visited[n], q[n];
- void bfs(int g[n][n],int v)
- {
- int u;
- count=count+1;
- visited[v]=count;
- q[++r]=v;
- while(f<=r)
- {
- for(u=0;u<n;u++)
- {
- if(g[v][u]!=0 && visited[u]==0)
- {
- count=count+1;
- visited[u]=count;
- q[++r]=u;
- }
- }
- printf("\n %d",q[f++]);
- }
- }
- void BFST(int g[n][n])
- {
- int v;
- count=0;
- for(v=0;v<n;v++)
- visited[v]=0;
- for(v=0;v<n;v++)
- if(visited[v]==0)
- bfs(g,v);
- }
- void main()
- {
- int g[n][n]= {{0,0,1,1},{1,0,0,0},{1,1,0,0},{0,0,0,1}};
- BFST(g);
- getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement