Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Graph
- {
- int **adjacentMatrix;
- int _vertexCount;
- void init();
- public:
- Graph(int);
- ~Graph();
- void addEdgeUndirected(int, int);
- void addEdgeUndirected(int, int, int);
- void addEdgeDirected(int, int);
- void addEdgeDirected(int, int, int);
- void removeEdgeUndirected(int, int);
- void removeEdgeDirected(int, int);
- bool hasEdgeBetween(int, int);
- int getVertexCount();
- Graph* getTranspose_Matrix();
- void display();
- };
- Graph::Graph(int count)
- {
- this->_vertexCount = count;
- init();
- }
- Graph::~Graph()
- {
- for (int i = 0; i < _vertexCount; ++i)
- {
- delete[] this->adjacentMatrix[i];
- }
- delete[] this->adjacentMatrix;
- }
- void Graph::init()
- {
- adjacentMatrix = new int*[this->_vertexCount];
- for (int i = 0; i < _vertexCount; ++i)
- {
- adjacentMatrix[i] = new int[this->_vertexCount]{ 0 };
- }
- }
- void Graph::addEdgeUndirected(int i, int j, int cost)
- {
- this->adjacentMatrix[i][j] = cost;
- this->adjacentMatrix[j][i] = cost;
- }
- void Graph::addEdgeUndirected(int i, int j)
- {
- this->addEdgeUndirected(i, j, 1);
- }
- void Graph::removeEdgeUndirected(int i, int j)
- {
- this->addEdgeUndirected(i, j, 0);
- }
- void Graph::addEdgeDirected(int i, int j, int cost)
- {
- this->adjacentMatrix[i][j] = cost;
- }
- void Graph::addEdgeDirected(int i, int j)
- {
- this->addEdgeDirected(i, j, 1);
- }
- void Graph::removeEdgeDirected(int i, int j)
- {
- this->addEdgeDirected(i, j, 0);
- }
- bool Graph::hasEdgeBetween(int i, int j)
- {
- return this->adjacentMatrix[i][j] == 1;
- }
- int Graph::getVertexCount()
- {
- return this->_vertexCount;
- }
- Graph* Graph::getTranspose_Matrix()
- {
- Graph* gT = new Graph(this->_vertexCount);
- for (int i = 0; i < this->_vertexCount; ++i)
- {
- for (int j = 0; j < _vertexCount; ++j)
- {
- gT->adjacentMatrix[i][j] = this->adjacentMatrix[j][i];
- }
- }
- return gT;
- }
- void Graph::display()
- {
- cout << "\nGraph in Matrix:\n----------------\n";
- for (int i = 0; i < this->_vertexCount; ++i)
- {
- for (int j = 0; j < this->_vertexCount; ++j)
- {
- cout << this->adjacentMatrix[i][j] << " ";
- }
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement