C++ Program to Represent Graph Using 2D Arrays



The graph can be represented using various ways. One of the technique is to use a 2D array, also known as adjacency matrix. The adjacency matrix is a square matrix of size V x V to represent a finite graph data structure using a 2D array, where V is the number of nodes/vertex of the graph.

In an undirected non-weighted graph, if there is an edge between vertex i and vertex j, the value at matrix[i][j] is 1 and if no edge exists, the value is 0. In this article, our task is to represent the graph using 2D arrays.

In the above diagram, the value of matrix[0][1] is 1, representing there is an edge between A and B. Similarly, the value of matrix[1][2] is 0 representing there is no edge between the nodes B and C.

Steps to Represent Graph Using 2D Arrays

The steps for 2D array representation of a graph is as follows:

  • First, we define the number of nodes and edges for the graph. We are representing an unweighted and undirected graph in a 2D array.
  • Then create an n*n matrix, here we are creating a 5*5 adjacency matrix as we have 5 nodes. We are using a dynamic array for creating the graph.
  • We have initialized the 2D matrix with zeroes(graph[i][j] = 0) using nested for loop. It represents that initially there is no edge between nodes.
  • Then, we have manually added the edges. You can add your own edge by changing the values.
  • Then we mark '1' for the edges that we have added in the previous step representing edges between the nodes.
  • The printMatrix() function is then called to print the adjacency matrix that we have created. It is the required 2D array representation of the given array.

Representing Graph Using 2D Arrays in C++

Here is the implementation of the above steps for representing a graph using an adjacency matrix.

#include <iostream>
#include <iomanip>
using namespace std;

void printMatrix(int **matrix, int n)
{
    int i, j;
    cout << "\n    ";
    for (i = 0; i < n; i++)
        cout << setw(4) << i + 1;
    cout << "\n\n";
    for (i = 0; i < n; i++)
    {
        cout << setw(4) << i + 1;
        for (j = 0; j < n; j++)
        {
            cout << setw(4) << matrix[i][j];
        }
        cout << "\n\n";
    }
}

// Printing the edges
void printEdges(int **matrix, int n)
{
    cout << "Edges in the graph:\n";
    for (int i = 0; i < n; i++)
    {
        for (int j = i + 1; j < n; j++)
        {
            if (matrix[i][j] == 1)
            {
                cout << "(" << i + 1 << ", " << j + 1 << ")\n";
            }
        }
    }
}

int main()
{
    int v = 5;
    int e = 6;

    // Allocate adjacency matrix
    int **graph = new int *[v];
    for (int i = 0; i < v; i++)
    {
        graph[i] = new int[v];
        for (int j = 0; j < v; j++)
            graph[i][j] = 0;
    }

    int edges[][2] = {
        {1, 2},
        {1, 3},
        {2, 4},
        {3, 5},
        {3, 4},
        {1, 5}};

    for (int i = 0; i < e; i++)
    {
        int v1 = edges[i][0];
        int v2 = edges[i][1];
        graph[v1 - 1][v2 - 1] = 1;
        graph[v2 - 1][v1 - 1] = 1;
    }
    printEdges(graph, v);
    cout << "2D Array representation of the graph:\n";
    printMatrix(graph, v);
    cout << "\n";

    for (int i = 0; i < v; i++)
        delete[] graph[i];
    delete[] graph;

    return 0;
}

The output of the above code is as follows:

Edges in the graph:
(1, 2)
(1, 3)
(1, 5)
(2, 4)
(3, 4)
(3, 5)
2D Array representation of the graph:

       1   2   3   4   5

   1   0   1   1   0   1

   2   1   0   0   1   0

   3   1   0   0   1   1

   4   0   1   1   0   0

   5   1   0   1   0   0

Complexity of 2D Array Representation of the Graph

  • Time Complexity: The time complexity to represent the graph using adjacency matrix is O(v^2).
  • Space Complexity: The space complexity to represent the graph using adjacency matrix is O(v^2).
Updated on: 2025-05-26T18:32:31+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements