Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Tjis file contain definition of functions declared in GraphClass.h ( http://pastebin.com/dgAgTq8D )
- */
- #include <iostream>
- #include <queue>
- #include "GraphClass.h"
- #include <list>
- #include <cstdbool>
- Graph :: Graph(int v)
- {
- this ->v = v; // create graph's vertices as v
- adj = new list<int> [v]; // create an array of list of ints of size V
- }
- void Grpah:: addEdge(int src,int dest)
- {
- // because it is defined as list, simple push back
- adj[src].push_back(dest);
- }
- void Graph:: BFS(int src)
- {
- // create a boolean visited array dynamically using tyhe value of class member v and mark all as false initially
- bool *visited = new bool[v];
- int i;
- for (i = 0; i < v; ++i)
- visited[i] = false;
- // create a queue for pushing the children
- queue<int> q;
- // push the current node and make it visited as true
- q.push(src);
- visited [src] = true;
- // do until the queue is empty i.e. NODES visited
- while(!q.empty())
- {
- src = q.front();
- q.pop();
- cout<<src<<" "<<endl;
- for (std::list<int>::iterator i = adj[i].begin(); i != adj[i].end(); ++i)
- {
- if(visited[*i]== false)
- {
- visited[*i]= true;
- q.push(*i);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment