A graph is a data structure having a set of vertices and a collection of edges that each connect a pair of vertices. There are several ways to represent a graph in computer memory, and one of them is using an adjacency matrix. An adjacency matrix is a 2D array with one row per vertex and one column per edge. In this article, we will discuss graph representation using an adjacency matrix in C programming language.
Adjacency Matrix Representation of Graph in CThe adjacency matrix is a way of representing a graph where rows represent vertices and columns represent edges. The elements of the matrix indicate the relationship between vertices and edges. The rows of the matrix represent the vertices and the columns of the matrix represent the edges of the graph.
- In C, we can create a 2D array of size V * V where V is the number of vertices.
- Initially, set all the elements of the matrix to 0.
- For each edge in the graph, if the graph is unweighted, set the corresponding element in the matrix to 1.
- If the graph is weighted, set the corresponding element in the matrix to the weight of the edge.
- If the graph is undirected, the matrix will be symmetric, meaning if there is an edge from node i to node j, there will also be an edge from node j to node i, so both matrix[i][j] and matrix[j][i] should be set.

Adjacency Matrix for the Above Undirected GraphFollowing is the incidence matrix to represent the above-undirected graph:
Vertex Name | A | B | C | D | E | F | G |
---|
A | 0 | 1 | 4 | 0 | 0 | 0 | 0 |
---|
B | 1 | 0 | 0 | 2 | 3 | 0 | 0 |
---|
C | 4 | 0 | 0 | 0 | 0 | 6 | 7 |
---|
D | 0 | 2 | 0 | 0 | 5 | 0 | 0 |
---|
E | 0 | 3 | 0 | 5 | 0 | 0 | 0 |
---|
F | 0 | 0 | 6 | 0 | 0 | 0 | 0 |
---|
G | 0 | 0 | 7 | 0 | 0 | 0 | 0 |
---|
In the above incidence matrix:
- Each column corresponds to an edge.
- A is connected to B (weight 1) and C (weight 4)
- B is connected to A (weight 1), D (weight 2), and E (weight 3)
- C is connected to A (weight 4), F (weight 6), and G (weight 7)
- D is connected to B (weight 2) and E (weight 5)
- E is connected to B (weight 3) and D (weight 5)
- F is connected to C (weight 6)
- G is connected to C (weight 7)
- In each column, there are two 1s indicating the vertices that are connected by the edge.
C Program to Represent Graph Using Adjacency MatrixThe following program represents how we can represent a graph using adjacency matrix in C:
C
// C Program to represent the graph using ajacency matrix
#include <stdio.h>
#include <stdlib.h>
// Structure to represent a graph
typedef struct {
int vertices;
int** adjMatrix;
} Graph;
// Function to create a graph
Graph* createGraph(int vertices)
{
Graph* graph = (Graph*)malloc(sizeof(Graph));
graph->vertices = vertices;
// Allocate memory for the adjacency matrix
graph->adjMatrix
= (int**)malloc(vertices * sizeof(int*));
for (int i = 0; i < vertices; i++) {
graph->adjMatrix[i]
= (int*)calloc(vertices, sizeof(int));
}
return graph;
}
// Function to add an edge to the graph (undirected)
void addEdge(Graph* graph, int src, int dest)
{
if (src >= graph->vertices || dest >= graph->vertices) {
printf("Invalid vertices!\n");
return;
}
graph->adjMatrix[src][dest] = 1;
// for a directed graph, set weight
graph->adjMatrix[dest][src] = 1;
}
// Function to display the adjacency matrix
void displayAdjMatrix(Graph* graph)
{
printf("Adjacency Matrix:\n");
for (int i = 0; i < graph->vertices; i++) {
for (int j = 0; j < graph->vertices; j++) {
printf("%d ", graph->adjMatrix[i][j]);
}
printf("\n");
}
}
// Function to free the allocated memory for the graph
void freeGraph(Graph* graph)
{
for (int i = 0; i < graph->vertices; i++) {
free(graph->adjMatrix[i]);
}
free(graph->adjMatrix);
free(graph);
}
int main()
{
int vertices = 5;
// Create a graph
Graph* graph = createGraph(vertices);
// Add edges to the graph
addEdge(graph, 0, 1);
addEdge(graph, 0, 4);
addEdge(graph, 1, 2);
addEdge(graph, 1, 3);
addEdge(graph, 1, 4);
addEdge(graph, 2, 3);
addEdge(graph, 3, 4);
// Display the adjacency matrix
displayAdjMatrix(graph);
// Free the allocated memory
freeGraph(graph);
return 0;
}
OutputIncidence Matrix:
1 1 1 0 0
1 0 0 1 0
0 1 0 0 1
0 0 1 1 1
Time Complexity : O(V x E), where V represents the number of vertices in the graph and E represents the edges. Auxiliary Space: O(V x E)
Related ArticlesYou can also go through the following articles to improve your understanding about the graph data structure:
|