# Graph Representation using 2D Arrays in C++

«
»

This is a C++ program to represent graph using 2D arrays.

Problem Description

1. This algorithm represents a graph using 2D arrays.
2. This method of representing graphs is not efficient.
3. The time complexity of this algorithm is O(v*v).

Problem Solution

1. This algorithm takes the input of the number of vertex and edges.
2. Take the input of connected vertex pairs.
3. Print the graph using 2D arrays.
4. Exit.

Program/Source Code

C++ program to represent graph using 2D arrays.
This program is successfully run on Dev-C++ using TDM-GCC 4.9.2 MinGW compiler on a Windows system.

```#include<iostream>
#include<iomanip>

using namespace std;

// A function to print the matrix.
void PrintMat(int **mat, int n)
{
int i, j;

cout<<"\n\n"<<setw(4)<<"";
for(i = 0; i < n; i++)
cout<<setw(3)<<"("<<i+1<<")";
cout<<"\n\n";

// Print 1 if the corresponding vertexes are connected otherwise 0.
for(i = 0; i < n; i++)
{
cout<<setw(3)<<"("<<i+1<<")";
for(j = 0; j < n; j++)
{
cout<<setw(4)<<mat[i][j];
}
cout<<"\n\n";
}
}

int main()
{
int i, v, e, j, v1, v2;

// take the input of the number of edges.
cout<<"Enter the number of vertexes of the graph: ";
cin>>v;

// Dynamically declare graph.
int **graph;
graph = new int*[v];

for(i = 0; i < v; i++)
{
graph[i] = new int[v];
for(j = 0; j < v; j++)graph[i][j] = 0;
}

cout<<"\nEnter the number of edges of the graph: ";
cin>>e;

// Take the input of the adjacent vertex pairs of the given graph.
for(i = 0; i < e; i++)
{
cout<<"\nEnter the vertex pair for edge "<<i+1;
cout<<"\nV(1): ";
cin>>v1;
cout<<"V(2): ";
cin>>v2;

graph[v1-1][v2-1] = 1;
graph[v2-1][v1-1] = 1;
}

// Print the 2D array representation of the graph.
PrintMat(graph, v);
}```
Program Explanation

1. Take the input of the number of vertex ‘v’ and edges ‘e’.
2. Dynamically assign memory to the graph[][] matrix.
3. Take the input of ‘e’ pairs of vertexes of the given graph in graph[][].
4. For each pair of connected vertex(v1, v2), store 1 int the graph[][] at the index (v1,v2) and (v2, v1).
5. Print the matrix using PrintMat().
6. Exit.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Runtime Test Cases
```Case 1:
Enter the number of vertexes of the graph: 5

Enter the number of edges of the graph: 8

Enter the vertex pair for edge 1
V(1): 1
V(2): 3

Enter the vertex pair for edge 2
V(1): 1
V(2): 4

Enter the vertex pair for edge 3
V(1): 1
V(2): 5

Enter the vertex pair for edge 4
V(1): 2
V(2): 3

Enter the vertex pair for edge 5
V(1): 2
V(2): 5

Enter the vertex pair for edge 6
V(1): 3
V(2): 4

Enter the vertex pair for edge 7
V(1): 3
V(2): 5

Enter the vertex pair for edge 8
V(1): 4
V(2): 5

(1)  (2)  (3)  (4)  (5)

(1)   0   0   1   1   1

(2)   0   0   1   0   1

(3)   1   1   0   1   1

(4)   1   0   1   0   1

(5)   1   1   1   1   0

Case 2:
Enter the number of vertexes of the graph: 4

Enter the number of edges of the graph: 4

Enter the vertex pair for edge 1
V(1): 1
V(2): 2

Enter the vertex pair for edge 2
V(1): 1
V(2): 4

Enter the vertex pair for edge 3
V(1): 2
V(2): 3

Enter the vertex pair for edge 4
V(1): 3
V(2): 4

(1)  (2)  (3)  (4)

(1)   0   1   0   1

(2)   1   0   1   0

(3)   0   1   0   1

(4)   1   0   1   0```

Sanfoundry Global Education & Learning Series – C++ Algorithms.
To practice all C++ Algorithms, here is complete set of 1000 C++ Algorithms.