# Adjacency Matrix Graph In C

The given code have 4 major functions:

• One is creating the edge
• One is removing the edge
• One is checking the edge
• Other is printing the edge

## Adjacency Matrix Graph Code In C

``````#include<stdio.h>
#define V 5

void def(int arr[][V])
{
int i,j;
for(i = 0; i < V; i++)
for(j = 0; j < V; j++)
arr[i][j] = 0;
}

void addEdge(int arr[][V],int a, int b)
{
arr[a][b] = 1;
}

void print(int arr[][V])
{
int i, j;
for(i = 0; i < V; i++)
{
for(j = 0; j < V; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}
}

int hasEdge(int arr[][V], int a, int b)
{
if(arr[a][b] == 1)
return 1;

return 0;
}

void removeEdge(int arr[][V], int a, int b)
{
arr[a][b] = 0;
}

int main()
{

printf("Removing Edge(0, 1)\n");

printf("(0,1) has Edge?\n");
printf("Edge Found\n");
else

printf("(0,2) has Edge?\n");

return 0;
}``````

In the above code, we are doing following things:

• First we are creating a matrix and initializing 0 values to each rows and columns.
• After this if we want to add an edge between any two vertices then we will make that equal to 1.
• If we want to remove then we will again update the value to 0.
• So we are using two dimensional array concept to implement adjacency matrix graph. 