
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
Convert Adjacency Matrix to Adjacency List Representation of Graph
Adjacency lists show a vertex's neighbours. Transposing an adjacency matrix into a list creates a more compact and efficient graph representation. Switching from an adjacency matrix to a list improves memory use, traversal to surrounding nodes, and edge information. This transformation enables a wide range of graph processing operations and algorithms.
Methods Used
Iterative approach
Recursive approach
Iterative Approach
The iterative method uses a nested loop to transform an adjacency matrix to a list. It adds the adjacency list edges to each matrix element. Simple and ideal for small to medium?sized graphs. O(V^2) is its temporal complexity.
Algorithm
Iterate over the complete matrix
Add all the elements to list
Return adjacency list containing all lists
Example
#include <iostream> #include <vector> using namespace std; vector<vector<int>> convertMatrixToListIterative(vector<vector<int>>& matrix) { int n = matrix.size(); vector<vector<int>> adjList(n); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (matrix[i][j] == 1) { adjList[i].push_back(j); } } } return adjList; } int main() { vector<vector<int>> matrix = {{0, 1, 1}, {1, 0, 1}, {1, 1, 0}}; vector<vector<int>> adjList = convertMatrixToListIterative(matrix); // Print the adjacency list for (int i = 0; i < adjList.size(); ++i) { cout << "Vertex " << i << ": "; for (int j = 0; j < adjList[i].size(); ++j) { cout << adjList[i][j] << " "; } cout << endl; } return 0; }
Output
Vertex 0: 1 2 Vertex 1: 0 2 Vertex 2: 0 1
Recursive Approach
Recursive functions convert adjacency matrices to lists. Starting with the matrix's first row, it checks each column for connections. It adds the vertex to the adjacency list for edges. Then, the function calls itself with each row until all rows are processed. This approach is compact and elegant, although recursion depth may restrict huge graphs.
Algorithm
Write a recursive function and give matrix and its rows as arguments and a list
Add all the elements to list
Return adjacency list containing all lists
Example
#include <iostream> #include <vector> using namespace std; void convertMatrixToListRecursive(vector<vector<int>>& matrix, vector<vector<int>>& adjList, int row) { int n = matrix.size(); for (int j = 0; j < n; ++j) { if (matrix[row][j] == 1) { adjList[row].push_back(j); } } if (row < n - 1) { convertMatrixToListRecursive(matrix, adjList, row + 1); } } int main() { vector<vector<int>> matrix = {{0, 1, 1}, {1, 0, 1}, {1, 1, 0}}; vector<vector<int>> adjList(matrix.size()); convertMatrixToListRecursive(matrix, adjList, 0); // Print the adjacency list for (int i = 0; i < adjList.size(); ++i) { cout << "Vertex " << i << ": "; for (int j = 0; j < adjList[i].size(); ++j) { cout << adjList[i][j] << " "; } cout << endl; } return 0; }
Output
Vertex 0: 1 2 Vertex 1: 0 2 Vertex 2: 0 1
Conclusion
Processing and studying graphs requires converting the graph's adjacency matrix into a list. Matrix to list conversion improves storage, graph manipulation, and neighbour access.When working with vast networks, converting the adjacency matrix to an adjacency list saves space and maintains just the most important information about vertex relationships. Adjacency lists simplify neighbour detection, degree computation, and graph exploration.