Advertisement
AshfaqFardin

Adjacency Matrix Graph

Aug 17th, 2021
1,290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int nodes; cin >> nodes;
  8.     int edges; cin >> edges;
  9.    
  10.     bool** sqr_matrix = new bool*[nodes];
  11.    
  12.     for(int i = 0; i < nodes; i++){
  13.         sqr_matrix[i] = new bool[nodes];
  14.     }
  15.    
  16.     for(int i = 0; i < nodes; i++){
  17.         for(int j = 0; j < nodes; j++){
  18.             sqr_matrix[i][j] = false;
  19.         }
  20.     }
  21.    
  22.     int p1, p2;
  23.     for(int i = 0; i < edges; i++){
  24.         cin >> p1 >> p2;
  25.         sqr_matrix[p1][p2] = true;
  26.         sqr_matrix[p2][p1] = true;
  27.     }
  28.    
  29.     //test
  30.     cout << endl;
  31.     for(int i = 0; i < nodes; i++){
  32.         for(int j = 0; j < nodes; j++){
  33.             cout << sqr_matrix[i][j] << " ";
  34.         }
  35.         cout << endl;
  36.     }
  37.    
  38.    
  39.     return 0;
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement