This C++ program displays the adjacency matrix structure of representation of a graph. An adjacency matrix is a matrix where each column and row correspond to an edge if the value is 1 at their intersection.

Here is the source code of the C++ program to display the adjacency matrix structure on taking the inputs as edges between two particular nodes. This C++ program is successfully compiled and run on DevCpp, a C++ compiler. The program output is given below.

`/*`

`* C++ Program to Describe the Representation of Graph using Adjacency Matrix`

`*/`

`#include<iostream>`

`#include<stdio.h>`

`#include<conio.h>`

using namespace std;

`struct node`

`{`

int from,to;

}a[5], t;

void addEdge(int am[][5],int i,int j,int in)

`{`

a[in].from = i;

a[in].to = j;

for (int p = 0; p <= in; p++)

`{`

for (int q = p + 1; q <= in; q++)

`{`

if (a[p].from > a[q].from)

`{`

t = a[p];

a[p] = a[q];

a[q] = t;

`}`

else if (a[p].from == a[q].from)

`{`

if (a[p].to > a[q].to)

`{`

t = a[p];

a[p] = a[q];

a[q] = t;

`}`

`}`

`else`

`{`

continue;

`}`

`}`

`}`

`}`

int main()

`{`

int n, c = 0, x, y, ch, i, j;

cout<<"Enter the no of vertices\n";

cin>>n;

int am[5][5];

for (int i = 0; i < 5; i++)

`{`

for (int j = 0; j < 5; j++)

`{`

am[i][j] = 0;

`}`

`}`

while (ch != -1)

`{`

cout<<"Enter the nodes between which you want to introduce edge\n";

cin>>x>>y;

addEdge(am,x,y,c);

c++;

cout<<"Press -1 to exit\n";

cin>>ch;

`}`

for (int j = 0; j < c; j++)

`{`

am[a[j].from][j] = 1;

am[a[j].to][j] = 1;

`}`

for (int i = 0; i < n; i++)

`{`

for (int j = 0; j < c; j++)

`{`

cout<<am[i][j]<<"\t";

`}`

cout<<endl;

`}`

getch();

`}`

Output Enter the no of vertices 5 0 1 0 0 1 1 0 1 1 1 0 1 0 1 0 0 1 1 0 1 1 1 0 1 0

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

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