Advertisement
fueanta

Graph [Matrix Representation]

Nov 29th, 2017
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. class Graph
  2. {
  3.     int **adjacentMatrix;
  4.     int _vertexCount;
  5.  
  6.     void init();
  7.  
  8. public:
  9.     Graph(int);
  10.     ~Graph();
  11.  
  12.     void addEdgeUndirected(int, int);
  13.     void addEdgeUndirected(int, int, int);
  14.  
  15.     void addEdgeDirected(int, int);
  16.     void addEdgeDirected(int, int, int);
  17.  
  18.     void removeEdgeUndirected(int, int);
  19.     void removeEdgeDirected(int, int);
  20.  
  21.     bool hasEdgeBetween(int, int);
  22.     int getVertexCount();
  23.     Graph* getTranspose_Matrix();
  24.     void display();
  25. };
  26.  
  27. Graph::Graph(int count)
  28. {
  29.     this->_vertexCount = count;
  30.     init();
  31. }
  32.  
  33. Graph::~Graph()
  34. {
  35.     for (int i = 0; i < _vertexCount; ++i)
  36.     {
  37.         delete[] this->adjacentMatrix[i];
  38.     }
  39.     delete[] this->adjacentMatrix;
  40. }
  41.  
  42. void Graph::init()
  43. {
  44.     adjacentMatrix = new int*[this->_vertexCount];
  45.     for (int i = 0; i < _vertexCount; ++i)
  46.     {
  47.         adjacentMatrix[i] = new int[this->_vertexCount]{ 0 };
  48.     }
  49. }
  50.  
  51. void Graph::addEdgeUndirected(int i, int j, int cost)
  52. {
  53.     this->adjacentMatrix[i][j] = cost;
  54.     this->adjacentMatrix[j][i] = cost;
  55. }
  56.  
  57. void Graph::addEdgeUndirected(int i, int j)
  58. {
  59.     this->addEdgeUndirected(i, j, 1);
  60. }
  61.  
  62. void Graph::removeEdgeUndirected(int i, int j)
  63. {
  64.     this->addEdgeUndirected(i, j, 0);
  65. }
  66.  
  67. void Graph::addEdgeDirected(int i, int j, int cost)
  68. {
  69.     this->adjacentMatrix[i][j] = cost;
  70. }
  71.  
  72. void Graph::addEdgeDirected(int i, int j)
  73. {
  74.     this->addEdgeDirected(i, j, 1);
  75. }
  76.  
  77. void Graph::removeEdgeDirected(int i, int j)
  78. {
  79.     this->addEdgeDirected(i, j, 0);
  80. }
  81.  
  82. bool Graph::hasEdgeBetween(int i, int j)
  83. {
  84.     return this->adjacentMatrix[i][j] == 1;
  85. }
  86.  
  87. int Graph::getVertexCount()
  88. {
  89.     return this->_vertexCount;
  90. }
  91.  
  92. Graph* Graph::getTranspose_Matrix()
  93. {
  94.     Graph* gT = new Graph(this->_vertexCount);
  95.    
  96.     for (int i = 0; i < this->_vertexCount; ++i)
  97.     {
  98.         for (int j = 0; j < _vertexCount; ++j)
  99.         {
  100.             gT->adjacentMatrix[i][j] = this->adjacentMatrix[j][i];
  101.         }
  102.     }
  103.     return gT;
  104. }
  105.  
  106. void Graph::display()
  107. {
  108.     cout << "\nGraph in Matrix:\n----------------\n";
  109.     for (int i = 0; i < this->_vertexCount; ++i)
  110.     {
  111.         for (int j = 0; j < this->_vertexCount; ++j)
  112.         {
  113.             cout << this->adjacentMatrix[i][j] << " ";
  114.         }
  115.         cout << endl;
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement