This C program represents graph using incidence matrix.

An incidence matrix is a matrix where each column represents an edge connected to two vertices.

Here is the source code of the C program to represent graph using incidence matrix. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

`/*`

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

`*/`

`#include<stdio.h>`

`struct node`

`{`

int from, to;

}a[5], t;

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

`{`

int p, q;

a[in].from = i;

a[in].to = j;

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

`{`

for (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;

int am[5][5];

printf("Enter the no of vertices\n");

scanf("%d", &n);

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

`{`

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

`{`

am[i][j] = 0;

`}`

`}`

while (ch != -1)

`{`

printf("Enter the nodes between which you want to introduce edge\n");

scanf("%d%d", &x, &y);

addEdge(am, x, y, c);

`c++;`

printf("Press -1 to exit\n");

scanf("%d", &ch);

`}`

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

`{`

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

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

`}`

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

`{`

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

`{`

printf("%d\t" ,am[i][j]);

`}`

printf("\n");

`}`

`}`

$ gcc incidence_matrix.c -o incidence_matrix $ ./incidence_matrix Enter the no of vertices 5 Enter the nodes between which you want to introduce edge 0 1 Press -1 to exit 0 Enter the nodes between which you want to introduce edge 0 2 Press -1 to exit 0 Enter the nodes between which you want to introduce edge 2 3 Press -1 to exit 0 Enter the nodes between which you want to introduce edge 1 4 Press -1 to exit 0 Enter the nodes between which you want to introduce edge 0 3 Press -1 to exit -1 1 1 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 1 0 0 0 1 0

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

Here’s the list of Best Reference Books in C Programming, Data Structures and Algorithms.