C++ Program to Implement Adjacency Matrix



The adjacency matrix of a graph is a square matrix of size V x V, that represent a finite graph data structure using a 2D array, where V is number of edges of the graph. The each non zero elements in the matrix represent an edges of the graph. For example, if the graph has some edges from i to j vertices, then in the adjacency matrix at i th row and j th column it will be 1 (or some non-zero value for weighted graph), otherwise that value will be 0. In this article, we will learn how to create an adjacency matrix for a graph using C++ program. First of all let's see an example of adjacency matrix.

Example of Adjacency Matrix

To understand what is an adjacency matrix is, first we need to take a graph data structure for reference. The image below represent a simple undirected graph with 6 vertices and 8 edges.

Graph

In the above graph there are edges between 0-3, 0-4, 1-2, 1-5, 1-4, 2-3, 2-5, 3-5, and 4-5. Hence the adjacency matrix at 0th row and 3rd column will be 1, similarly 0th row and 4th column will also be 1 and so on. The adjacency matrix will look like there in below.

Adjacency Matrix

The adjacency matrix of the above graph is shown below.


0
1
2
3
4
5
0
0
0
0
1
1
0
1
0
0
1
0
1
1
2
0
1
0
1
0
0
3
1
0
1
0
0
1
4
1
1
0
0
0
1
5
0
1
1
1
1
0

Steps to Implement Adjacency Matrix

The following is the algorithm/steps to represent a graph using an adjacency matrix.

  • Step 1: Start
  • Step 2: Input the number of vertices V and number of edges E in the graph
  • Step 3: Initialize a V x V matrix (adjMatrix) and set all values to 0.
  • Step 4: For each edge, repeat step 5 to step 7.
  • Step 5: Input the pair of vertices (u, v) that form an edge.
  • Step 6: Set adjMatrix[u][v] = 1
  • Step 7: If the graph is undirected, also set adjMatrix[v][u] = 1
  • Step 8: Display the adjacency matrix.
  • Step 9: End

C++ Program to Implement Adjacency Matrix

The following C++ program shows how to implement adjacency matrix for a graph. The program allows the user to input (we assumed user input in this case) the number of vertices and edges of the graph, and it virtually create a graph and displays it using the adjacency matrix.

#include <iostream>
using namespace std;

class Graph {
private:
    int vertices;
    int **adjMatrix;
    bool isDirected;

public:
    Graph(int v, bool directed = false) {
        vertices = v;
        isDirected = directed;

        // Allocate memory for adjacency matrix
        adjMatrix = new int*[vertices];
        for (int i = 0; i < vertices; ++i) {
            adjMatrix[i] = new int[vertices];
            for (int j = 0; j < vertices; ++j) {
                adjMatrix[i][j] = 0; // Initialize with 0
            }
        }
    }

    void addEdge(int i, int j) {
        adjMatrix[i][j] = 1;
        if (!isDirected) {
            adjMatrix[j][i] = 1;
        }
    }

    void display() {
        cout << "\nAdjacency Matrix:\n";
        for (int i = 0; i < vertices; ++i) {
            for (int j = 0; j < vertices; ++j) {
                cout << adjMatrix[i][j] << " ";
            }
            cout << endl;
        }
    }

    ~Graph() {
        for (int i = 0; i < vertices; ++i) {
            delete[] adjMatrix[i];
        }
        delete[] adjMatrix;
    }
};

int main() {
    // Assumed values
    int V = 5; // 5 vertices numbered 0 to 4
    int E = 6; // 6 edges

    cout << "Number of vertices: " << V << endl;
    cout << "Number of edges: " << E << endl;

    Graph g(V);  // Undirected graph (can change to Graph g(V, true); for directed)

    // Edges of Graph
    int edges[6][2] = {
        {0, 1},
        {0, 2},
        {1, 2},
        {1, 3},
        {2, 4},
        {3, 4}
    };

    cout << "Edges (from -> to):" << endl;
    for (int i = 0; i < E; ++i) {
        cout << edges[i][0] << " -> " << edges[i][1] << endl;
        g.addEdge(edges[i][0], edges[i][1]);
    }

    g.display();

    return 0;
}

The output of the above code will be:

Number of vertices: 5
Number of edges: 6
Edges (from -> to):
0 -> 1
0 -> 2
1 -> 2
1 -> 3
2 -> 4
3 -> 4

Adjacency Matrix:
0 1 1 0 0 
1 0 1 1 0 
1 1 0 0 1 
0 1 0 0 1 
0 0 1 1 0 

Complexity of Adjacency Matrix Representation

The adjacency matrix representation takes O(V2) amount of space while it is computed. When graph has maximum number of edges and minimum number of edges, in both cases the required space will be same.

Updated on: 2025-04-28T18:17:51+05:30

14K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements