Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. class Graph
  8. {
  9. private:
  10.  
  11.     int _vertices;
  12.     int **_incydentalArray;
  13.  
  14. public:
  15.     Graph(int size)
  16.     {
  17.         this->_vertices = size;
  18.         _incydentalArray = AlocateMemory(size, size);
  19.  
  20.     }
  21.  
  22.     int **AlocateMemory(int rowCount, int colCount)
  23.     {
  24.         int **a = {};
  25.         try
  26.         {
  27.             a = new int*[rowCount];
  28.             for (int i = 0; i < rowCount; i++)
  29.                 a[i] = new int[colCount];
  30.             return a;
  31.         }
  32.  
  33.         catch (bad_alloc)
  34.         {
  35.             exit(0);
  36.         }
  37.     }
  38.  
  39.     void BFS(int Startingpoint)
  40.     {
  41.         queue <int> q;
  42.  
  43.         int firstInQueue;
  44.  
  45.         bool *visited = new bool[_vertices];
  46.  
  47.         for (int i = 0; i < _vertices; i++)
  48.         {
  49.             visited[i] = false;
  50.         }
  51.  
  52.         q.push(Startingpoint);
  53.  
  54.         visited[Startingpoint] = true;
  55.  
  56.         while (!visited)
  57.         {
  58.            
  59.             firstInQueue = q.front();
  60.             q.pop();
  61.  
  62.             for (int i = 0; i < _vertices; i++)
  63.             {
  64.                 if ((_incydentalArray[firstInQueue][i] == 1) && (visited[i] == false))
  65.                 {
  66.                     cout << i;
  67.                     q.push(i);
  68.                     visited[i] = true;
  69.                    
  70.                 }
  71.             }
  72.         }
  73.     }
  74.  
  75.     void FillIncidanceArray()
  76.     {  
  77.        
  78.         for (int i = 0; i < _vertices; i++)
  79.         {
  80.             for (int j = 0; j < _vertices; j++)
  81.             {
  82.                 cin >> _incydentalArray[i][j];
  83.             }
  84.         }
  85.        
  86.     }
  87. };
  88.  
  89.  
  90. int main()
  91. {
  92.     Graph graph(6);
  93.  
  94.     graph.FillIncidanceArray();
  95.     graph.BFS(3);
  96.  
  97.  
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement