Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Find Inverse of a Graph Matrix
Every graph can be represented by a matrix called the adjacency matrix, in which each element indicates whether pairs of vertices have an edge between them. In this article, we will learn to find the inverse of a adjacency matrix of a graph using C++. First of all, let's understand what the inverse of a matrix means.
Inverse of a Matrix
The inverse of a matrix A is another matrix, denoted as A-1, such that the result of matrix multiplication A.A-1 will be an identity matrix I. The identity matrix is a square matrix with ones on the diagonal and zeros on all other places. The inverse of a matrix can only be defined for square matrices (matrices with the same number of rows and columns) that are non-singular (i.e., they have a non-zero determinant).
We have mathematical formula to find the inverse of a matrix: $$ \text{Inverse}(M) = \frac{1}{\det(M)} \cdot \text{Adjoint}(M) $$
- det(M) is the determinant of matrix M.
- Adjoint(M) is the adjoint of matrix M, which is the transpose of the cofactor matrix.
- Cofactor for each element is found by taking the determinant of the small matrix formed by removing the row and column of that element.
Example: Finding Inverse of a Matrix
Let's consider a simple example of a 2x2 matrix: $$ M = \begin{bmatrix} 1 & 2 \ 3 & 4 \end{bmatrix}, \quad $$ To find the inverse of this matrix, we can use the formula mentioned above. First, we need to calculate the determinant and the cofactor matrix.
det(M) = (1 * 4) - (2 * 3) = 4 - 6 = -2
Next, we find the cofactor for each element: cofactor(1) = 4, cofactor(2) = -3, cofactor(3) = -2, cofactor(4) = 1. Now, make the cofactor matrix $$ M = \begin{bmatrix} 4 & -3 \ -2 & 1 \end{bmatrix} $$ Then, transpose the cofactor matrix to get the adjoint matrix: $$ adjoint(M) = \begin{bmatrix} 4 & -2 \ -3 & 1 \end{bmatrix} $$ Finally, calculate the inverse: $$ inverse(M) = \frac{1}{-2} \cdot \begin{bmatrix} 4 & -2 \ -3 & 1 \end{bmatrix} = \begin{bmatrix} -2 & 1 \ 1.5 & -0.5 \end{bmatrix} $$
Steps to Find Inverse of Adjacency Matrix of a Graph
To find the inverse of an adjacency matrix of a graph, we can follow these steps:
- Define the adjacency matrix of the graph as a 2D array or vector.
- If the matrix is not square matrix, return "Inverse not possible".
- Create a recursive function to calculate the determinant of the matrix, which take the matrix as input and return the determinant as float.
- Create a function to calculate the cofactor matrix, call the determinant function for each element to find the cofactor.
- Transpose the cofactor matrix to get the adjoint matrix.
- Finally, calculate the inverse using the formula mentioned above.
- Visualize the inverse matrix using an adjacency list.
C++ Program to Find Inverse of Adjacency Matrix of a Graph
The C++ code below implements all the steps mentioned above to find the inverse of an adjacency matrix of a graph. It also visualizes the original and inverse graphs using adjacency lists.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
#define N 5
#define THRESHOLD 0.05 // Threshold to consider non-zero in inverse
void getCofactor(int M[N][N], int temp[N][N], int p, int q, int n) {
int i = 0, j = 0;
for (int row = 0; row < n; row++) {
for (int col = 0; col < n; col++) {
if (row != p && col != q) {
temp[i][j++] = M[row][col];
if (j == n - 1) {
j = 0;
i++;
}
}
}
}
}
int determinant(int M[N][N], int n) {
int D = 0;
if (n == 1)
return M[0][0];
int temp[N][N];
int sign = 1;
for (int f = 0; f < n; f++) {
getCofactor(M, temp, 0, f, n);
D += sign * M[0][f] * determinant(temp, n - 1);
sign = -sign;
}
return D;
}
void adjoint(int M[N][N], int adj[N][N]) {
if (N == 1) {
adj[0][0] = 1;
return;
}
int sign, temp[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
getCofactor(M, temp, i, j, N);
sign = ((i + j) % 2 == 0) ? 1 : -1;
adj[j][i] = sign * determinant(temp, N - 1); // Transpose
}
}
}
bool inverse(int M[N][N], float inv[N][N]) {
int det = determinant(M, N);
if (det == 0) {
cout << "Singular matrix, can't find its inverse." << endl;
return false;
}
int adj[N][N];
adjoint(M, adj);
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
inv[i][j] = adj[i][j] / float(det);
return true;
}
template <typename T>
void printMatrix(T A[N][N]) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
cout << setw(10) << fixed << setprecision(4) << A[i][j] << " ";
cout << endl;
}
}
// For int matrix (original graph)
void visualizeAdjList(int M[N][N]) {
cout << "\nGraph Visualization (Adjacency List):" << endl;
for (int i = 0; i < N; i++) {
cout << "Node " << i << " -> ";
for (int j = 0; j < N; j++) {
if (M[i][j] != 0)
cout << j << " ";
}
cout << endl;
}
}
// For float matrix (inverse graph)
void visualizeAdjList(float M[N][N]) {
cout << "\nInverse Graph Visualization (Thresholded Adjacency List):" << endl;
for (int i = 0; i < N; i++) {
cout << "Node " << i << " -> ";
for (int j = 0; j < N; j++) {
if (i != j && fabs(M[i][j]) > THRESHOLD)
cout << j << " ";
}
cout << endl;
}
}
int main() {
int M[N][N] = {
{1, 1, 0, 0, 1},
{1, 0, 1, 1, 0},
{0, 1, 0, 1, 0},
{0, 1, 1, 0, 1},
{1, 0, 0, 1, 0}
};
float inv[N][N];
cout << "Original Graph Matrix:" << endl;
printMatrix(M);
visualizeAdjList(M);
cout << "\nInverse of Graph Matrix:" << endl;
if (inverse(M, inv)) {
printMatrix(inv);
visualizeAdjList(inv);
}
return 0;
}
The output of above code will be:
Original Graph Matrix:
1 1 0 0 1
1 0 1 1 0
0 1 0 1 0
0 1 1 0 1
1 0 0 1 0
Graph Visualization (Adjacency List):
Node 0 -> 0 1 4
Node 1 -> 0 2 3
Node 2 -> 1 3
Node 3 -> 1 2 4
Node 4 -> 0 3
Inverse of Graph Matrix:
1.0000 1.0000 0.0000 -1.0000 -1.0000
1.0000 1.0000 1.0000 -1.0000 -2.0000
0.0000 1.0000 0.0000 0.0000 -1.0000
-1.0000 -1.0000 0.0000 1.0000 2.0000
-1.0000 -2.0000 -1.0000 2.0000 3.0000
Inverse Graph Visualization (Thresholded Adjacency List):
Node 0 -> 1 3 4
Node 1 -> 0 2 3 4
Node 2 -> 1 4
Node 3 -> 0 1 4
Node 4 -> 0 1 2 3
Time and Space Complexity
Time Complexity: The complexity of determinant calculation is O(N!) for an NxN matrix, to find cofactor matrix is O(N * N!), and O(N^2) for inverse calculation. Thus, the overall time complexity is O(N * N!).
Space Complexity: The space complexity is O(N^2) for storing the matrices and cofactor matrix.