Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define white 1
- #define gray 2
- #define black 3
- int adj[8][8];
- int parent[8];
- int color[8];
- int dis[8];
- int path[8];
- int n,e;
- void bfs(int snode)
- {
- int i,j,x;
- for(i=0;i<n;i++)
- {
- color[i]=white;
- parent[i]=-1;
- dis[i]=-1;
- }
- color[snode]=gray;
- parent[snode]=-1;
- dis[snode]=0;
- queue<int>q;
- q.push(snode);
- while(!q.empty())
- {
- x=q.front();
- q.pop();
- for(i=0;i<n;i++)
- {
- if(adj[x][i]==1)
- {
- if(color[i]==white)
- {
- parent[i]=x;
- dis[i]=dis[x]+1;
- q.push(i);
- }
- }
- }
- color[x]=black;
- }
- }
- int main()
- {
- cout<<"\nEnter no of node and edge:";
- cin>>n>>e;
- int u,v,i,j,snode;
- cout<<"\nEnter nodes:\n";
- for(i=1;i<=e;i++)
- {
- cin>>u>>v;
- adj[u][v]=1;
- }
- cout<<"\nEnter source node:";
- cin>>snode;
- cout<<"\npath:\n";
- bfs(snode);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement