Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- using namespace std;
- #define MAX 10000
- vector <int> edgesIn[MAX], edgesOut[MAX];
- int level[10];
- bool visited[10];
- void bfs(int s)
- {
- queue <int> q;
- q.push(s);
- level[s] = 0;
- visited[s] = true;
- while(!q.empty())
- {
- int p = q.front();
- cout << p << " ";
- q.pop();
- for(int i = 0; i < edgesOut[p].size(); i++)
- {
- if(visited[edgesOut[p][i]] == false)
- {
- level[edgesOut[p][i]] = level[p] + 1;
- q.push(edgesOut[p][i]);
- visited[edgesOut[p][i]] = true;
- }
- }
- }
- }
- int main()
- {
- int nodeNo, edgeNo;
- cin >> nodeNo >> edgeNo;
- for(int i = 0; i < edgeNo; i++)
- {
- int x, y;
- cin >> x >> y;
- edgesOut[x].push_back(y);
- //edgesOut[y].push_back(x); //undirected
- // edgesIn[y].push_back(x);
- }
- /* for(int i = 0; i < nodeNo; i++) //counting indegree and outdegree
- {
- cout << "Indegree of node " << i << " : " << edgesIn[i].size() << endl;
- cout << "Outdegree of node " << i << " : " << edgesOut[i].size() << endl;
- }*/
- bfs(2); //source or starting node
- return 0;
- }
- /* Sample input
- 4 6
- 0 1
- 0 2
- 1 2
- 2 0
- 2 3
- 3 3
- Sample output: 2 0 3 1 */
Advertisement
Add Comment
Please, Sign In to add comment