Advertisement
Guest User

pt safu la bani

a guest
Oct 21st, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class AdjacencyMatrix {
  5.     private:
  6.         int n;
  7.         int **adj;
  8.  
  9.     public:
  10.         AdjacencyMatrix(int n) {
  11.             this->n = n;
  12.             adj = new int*[n];
  13.             for (int k = 0; k < n; k++) {
  14.                 adj[k] = new int[n];
  15.                 for(int j = 0; j < n; j++) {
  16.                     adj[k][j] = 0;
  17.                 }
  18.             }
  19.         }
  20.         //Add a new edge to the Graph
  21.         void addEdge(int orig, int dest) {
  22.             if( orig > n || dest > n || orig < 1 || dest < 1) {  
  23.                 cout << "Trying to add an invalid edge";
  24.                 cout << "(" << orig << ", " << dest << ")\n";
  25.             }
  26.             else {
  27.                 adj[orig - 1][dest - 1] = 1;
  28.             }
  29.         }
  30.          
  31.         //remove an edge
  32.         void removeEdge(int orig, int dest) {
  33.             if( orig > n || dest > n || orig < 1 || dest < 1) {  
  34.                 cout << "Trying to remove an invalid edge";
  35.                 cout << "(" << orig << ", " << dest << ")\n";
  36.             }
  37.             else {
  38.                 adj[orig - 1][dest - 1] = 0;
  39.             }
  40.         }
  41.          
  42.         //Print the graph
  43.         void display() {
  44.             for(int i = 0; i < n; i++) {
  45.                 for(int j = 0; j < n; j++)
  46.                     cout << adj[i][j] <<"  ";
  47.                 cout << endl;
  48.             }
  49.         }
  50. };
  51.  
  52. int main() {
  53.     AdjacencyMatrix M(5);
  54.     M.addEdge(2, 5);
  55.     M.addEdge(1, 5);
  56.     M.addEdge(5, 1);
  57.     M.addEdge(3, 3);
  58.     M.removeEdge(1, 5);
  59.      
  60.     M.display();
  61.      
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement