Guest User

Untitled

a guest
Jul 20th, 2015
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. /*
  2. Tjis file contain definition of functions declared in GraphClass.h ( http://pastebin.com/dgAgTq8D )
  3. */
  4.  
  5. #include <iostream>
  6. #include <queue>
  7. #include "GraphClass.h"
  8. #include <list>
  9. #include <cstdbool>
  10.  
  11. Graph :: Graph(int v)
  12. {
  13.     this ->v = v;           // create graph's vertices as v
  14.     adj = new list<int> [v];    // create an array of list of ints of size V
  15. }
  16.  
  17. void Grpah:: addEdge(int src,int dest)
  18. {
  19.     // because it is defined as list, simple push back
  20.     adj[src].push_back(dest);
  21. }
  22.  
  23. void Graph:: BFS(int src)
  24. {
  25.     // create a boolean visited array dynamically using tyhe value of class member v and mark all as false initially
  26.     bool *visited = new bool[v];
  27.     int i;
  28.     for (i = 0; i < v; ++i)
  29.         visited[i] = false;
  30.     // create a queue for pushing the children
  31.     queue<int> q;
  32.     // push the current node and make it visited as true
  33.     q.push(src);
  34.     visited [src] = true;
  35.     // do until the queue is empty i.e. NODES visited
  36.     while(!q.empty())
  37.     {
  38.         src = q.front();
  39.         q.pop();
  40.         cout<<src<<" "<<endl;
  41.         for (std::list<int>::iterator i = adj[i].begin(); i != adj[i].end(); ++i)
  42.         {
  43.             if(visited[*i]== false)
  44.             {
  45.                 visited[*i]= true;
  46.                 q.push(*i);
  47.             }
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment