Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- using namespace std;
- class Graph
- {
- private:
- int _vertices;
- int **_incydentalArray;
- public:
- Graph(int size)
- {
- this->_vertices = size;
- _incydentalArray = AlocateMemory(size, size);
- }
- int **AlocateMemory(int rowCount, int colCount)
- {
- int **a = {};
- try
- {
- a = new int*[rowCount];
- for (int i = 0; i < rowCount; i++)
- a[i] = new int[colCount];
- return a;
- }
- catch (bad_alloc)
- {
- exit(0);
- }
- }
- void BFS(int Startingpoint)
- {
- queue <int> q;
- int firstInQueue;
- bool *visited = new bool[_vertices];
- for (int i = 0; i < _vertices; i++)
- {
- visited[i] = false;
- }
- q.push(Startingpoint);
- visited[Startingpoint] = true;
- while (!visited)
- {
- firstInQueue = q.front();
- q.pop();
- for (int i = 0; i < _vertices; i++)
- {
- if ((_incydentalArray[firstInQueue][i] == 1) && (visited[i] == false))
- {
- cout << i;
- q.push(i);
- visited[i] = true;
- }
- }
- }
- }
- void FillIncidanceArray()
- {
- for (int i = 0; i < _vertices; i++)
- {
- for (int j = 0; j < _vertices; j++)
- {
- cin >> _incydentalArray[i][j];
- }
- }
- }
- };
- int main()
- {
- Graph graph(6);
- graph.FillIncidanceArray();
- graph.BFS(3);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement