SabirSazzad

Adjacency list

Feb 23rd, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. class Graph {
  2.  
  3. private:
  4.  
  5.       bool** adjacencyMatrix;
  6.       int vertexCount;
  7.  
  8. public:
  9.       Graph(int vertexCount) {
  10.  
  11.             this->vertexCount = vertexCount;
  12.             adjacencyMatrix = new bool*[vertexCount];
  13.             for (int i = 0; i < vertexCount; i++) {
  14.                   adjacencyMatrix[i] = new bool[vertexCount];
  15.                   for (int j = 0; j < vertexCount; j++)
  16.                         adjacencyMatrix[i][j] = false;
  17.             }
  18.       }
  19.       void addEdge(int i, int j) {
  20.  
  21.             if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount) {
  22.  
  23.                   adjacencyMatrix[i][j] = true;
  24.                   adjacencyMatrix[j][i] = true;
  25.             }
  26.       }
  27.  
  28.       void removeEdge(int i, int j) {
  29.  
  30.             if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount) {
  31.  
  32.                   adjacencyMatrix[i][j] = false;
  33.                   adjacencyMatrix[j][i] = false;
  34.             }
  35.       }
  36.  
  37.       bool isEdge(int i, int j) {
  38.  
  39.             if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount)
  40.                   return adjacencyMatrix[i][j];
  41.             else
  42.                   return false;
  43.       }
  44.       ~Graph() {
  45.  
  46.             for (int i = 0; i < vertexCount; i++)
  47.                   delete[] adjacencyMatrix[i];
  48.  
  49.             delete[] adjacencyMatrix;
  50.  
  51.       }
  52.  
  53. };
  54.  
  55. int main()
  56. {
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment