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
- char adj[125][125];
- char parent[125];
- int color[8];
- int dis[8];
- int path[8];
- int n,e;
- void bfs(char snode)
- {
- char 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<char>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<<"Enter no of node and edge:";
- cin>>n>>e;
- char u,v,snode;
- int i;
- cout<<"Enter nodes:\n";
- for(i=1;i<=e;i++)
- {
- cin>>u>>v;
- adj[u][v]=1;
- }
- cout<<"Enter source node:";
- cin>>snode;
- cout<<"path:\n";
- bfs(snode);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement