C++ Program to Implement Adjacency Matrix

This C++ Program demonstrates the implementation of Adjacency Matrix.

Here is source code of the C++ Program to demonstrate the implementation of Adjacency Matrix. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Implement Adjacency Matrix
  3.  */
  4. #include <iostream>
  5. #include <cstdlib>
  6. using namespace std;
  7. #define MAX 20
  8. /*
  9.  * Adjacency Matrix Class
  10.  */
  11. class AdjacencyMatrix
  12. {
  13.     private:
  14.         int n;
  15.         int **adj;
  16.         bool *visited;
  17.     public:
  18.         AdjacencyMatrix(int n)
  19.         {
  20.             this->n = n;
  21.             visited = new bool [n];
  22.             adj = new int* [n];
  23.             for (int i = 0; i < n; i++)
  24.             {
  25.                 adj[i] = new int [n];
  26.                 for(int j = 0; j < n; j++)
  27.                 {
  28.                     adj[i][j] = 0;
  29.                 }
  30.             }
  31.         }
  32.         /*
  33.          * Adding Edge to Graph
  34.          */ 
  35.         void add_edge(int origin, int destin)
  36.         {
  37.             if( origin > n || destin > n || origin < 0 || destin < 0)
  38.             {   
  39.                 cout<<"Invalid edge!\n";
  40.             }
  41.             else
  42.             {
  43.                 adj[origin - 1][destin - 1] = 1;
  44.             }
  45.         }
  46.         /*
  47.          * Print the graph
  48.          */ 
  49.         void display()
  50.         {
  51.             int i,j;
  52.             for(i = 0;i < n;i++)
  53.             {
  54.                 for(j = 0; j < n; j++)
  55.                     cout<<adj[i][j]<<"  ";
  56.                 cout<<endl;
  57.             }
  58.         }
  59. };
  60. /*
  61.  * Main
  62.  */ 
  63. int main()
  64. {
  65.     int nodes, max_edges, origin, destin;
  66.     cout<<"Enter number of nodes: ";
  67.     cin>>nodes;
  68.     AdjacencyMatrix am(nodes);
  69.     max_edges = nodes * (nodes - 1);
  70.     for (int i = 0; i < max_edges; i++)
  71.     {
  72.         cout<<"Enter edge (-1 -1 to exit): ";
  73.         cin>>origin>>destin;
  74.         if((origin == -1) && (destin == -1))
  75.             break;
  76.         am.add_edge(origin, destin);
  77.     }
  78.     am.display();
  79.     return 0;
  80. }

$ g++ adjacency_matrix.cpp
$ a.out
 
Enter number of nodes: 5
Enter edge (-1 -1 to exit): 1 2
Enter edge (-1 -1 to exit): 1 4
Enter edge (-1 -1 to exit): 1 5
Enter edge (-1 -1 to exit): 2 3
Enter edge (-1 -1 to exit): 2 5
Enter edge (-1 -1 to exit): 3 1
Enter edge (-1 -1 to exit): 5 2
Enter edge (-1 -1 to exit): 4 3
Enter edge (-1 -1 to exit): -1 -1
0  1  0  1  1
0  0  1  0  1
1  0  0  0  0
0  0  1  0  0
0  1  0  0  0
 
------------------
(program exited with code: 1)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.