Convert Adjacency Matrix to Adjacency List representation of Graph (original) (raw)
Last Updated : 27 Oct, 2025
Given a adjacency matrix representation of a Graph. Convert the given Adjacency Matrix to Adjacency List representation.
**Adjacency List: A list of lists where each list[i] stores vertices adjacent to vertex i.
**Adjacency Matrix: A binary 2D array adj[V][V] where adj[i][j] = 1 if an edge exists from i to j, otherwise adj[i][j] = 0.
**Examples:
**Input: mat[][] = [[0, 1, 0, 1],
[0, 0, 1, 0],
[0, 0, 0, 0],
[0, 0, 1, 0]]**Output: [[1, 3], [2], [], [2]]
**Explanation: The adjacency matrix represents the graph below and hence the adjacency list is [[1, 3], [2], [], [2]].
**Input: mat[][] = [[0, 1, 1, 0, 0],
[1, 0, 1, 0, 0],
[1, 1, 0, 1, 1],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0]]**Output: [[1, 2], [0, 2], [0, 1, 3, 4], [2], [2]]
**Explanation: The adjacency matrix represents the graph below and hence the adjacency list is [[1, 2], [0, 2], [0, 1, 3, 4], [2], [2]].
[Approach] - Traversing the Adjacency Matrix- O(V2) Time and O(1) Space
In this approach, we iterate through each element in the adjacency matrix, and for every vertex j where mat[i][j] = 1, we add j to the adjacency list of vertex i, thereby representing all connected neighbors for each vertex.
Follow the steps below to convert an adjacency matrix to an adjacency list:
- Initialize a list of lists.
- Iterate over the vertices in the adjacency matrix.
- For each vertex i, traverse all vertices j in its row.
- If mat[i][j] == 1, add vertex j to the adjacency list of vertex i.
Below is the implementation of the above approach:
C++ `
//Driver Code Starts #include using namespace std;
//Driver Code Ends
vector<vector> matToAdj(vector<vector>& mat) { vector<vector> adj;
int V = mat.size();
for (int i = 0; i < V; i++) {
vector<int> row;
for (int j = 0; j < V; j++) {
// adding the vertices that
// are neighbours of vertex i
if (mat[i][j] == 1) row.push_back(j);
}
adj.push_back(row);
}
return adj;}
//Driver Code Starts
int main() { // no. of vertices int V = 3; vector<vector> mat = {{0, 0, 1}, {0, 0, 1}, {1, 1, 0}};
vector<vector<int>> adj = matToAdj(mat);
for (int i = 0; i < V; i++) {
// printing adjacent vertices
// to vertex i
cout << i << ": ";
for (int j : adj[i]) {
cout << j << " ";
}
cout << endl;
}}
//Driver Code Ends
Java
//Driver Code Starts import java.util.ArrayList;
public class GFG { //Driver Code Ends
static ArrayList<ArrayList<Integer>> matToAdj(int[][] mat) {
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
int V = mat.length;
for( int i = 0; i < V; i++ ) {
ArrayList<Integer> row = new ArrayList<>();
for( int j = 0; j < V; j++ ) {
// adding the vertices that
// are neighbours of vertex i
if( mat[i][j] == 1 ) row.add(j);
}
adj.add(row);
}
return adj;
}//Driver Code Starts
public static void main(String[] args) {
// no. of vertices
int V = 3;
int[][] mat = {{0, 0, 1},
{0, 0, 1},
{1, 1, 0}};
ArrayList<ArrayList<Integer>> adj = matToAdj(mat);
for( int i = 0; i < V; i++ ) {
// printing adjacent vertices
// to vertex i
System.out.print(i + ": " );
ArrayList<Integer> row = adj.get(i);
for( int j : row ) {
System.out.print(j + " ");
}
System.out.println();
}
}} //Driver Code Ends
Python
def matToAdj(mat): adj = []
V = len(mat)
for i in range(V):
row = []
for j in range(V):
# adding the vertices that
# are neighbours of vertex i
if mat[i][j] == 1:
row.append(j)
adj.append(row)
return adj#Driver Code Starts
no. of vertices
V = 3 mat = [[0, 0, 1], [0, 0, 1], [1, 1, 0]]
adj = matToAdj(mat)
for i in range(V): print(i, end = ": ") # printing adjacent vertices # to vertex i print(*adj[i]) #Driver Code Ends
C#
//Driver Code Starts using System; using System.Collections.Generic;
public class GFG { //Driver Code Ends
static List<List<int>> matToAdj(int[][] mat) {
List<List<int>> adj = new List<List<int>>();
int V = mat.Length;
for (int i = 0; i < V; i++) {
List<int> row = new List<int>();
for (int j = 0; j < V; j++) {
// adding the vertices that
// are neighbours of vertex i
if (mat[i][j] == 1) row.Add(j);
}
adj.Add(row);
}
return adj;
}//Driver Code Starts
public static void Main() {
// no. of vertices
int V = 3;
int[][] mat = {
new int[] {0, 0, 1},
new int[] {0, 0, 1},
new int[] {1, 1, 0}
};
List<List<int>> adj = matToAdj(mat);
for (int i = 0; i < V; i++) {
// printing adjacent vertices
// to vertex i
Console.Write(i + ": ");
foreach (int j in adj[i]) {
Console.Write(j + " ");
}
Console.WriteLine();
}
}} //Driver Code Ends
JavaScript
function matToAdj(mat) { let adj = [];
let V = mat.length;
for (let i = 0; i < V; i++) {
let row = [];
for (let j = 0; j < V; j++) {
// adding the vertices that
// are neighbours of vertex i
if (mat[i][j] === 1) row.push(j);
}
adj.push(row);
}
return adj;}
//Driver Code Starts // no. of vertices let V = 3; let mat = [ [0, 0, 1], [0, 0, 1], [1, 1, 0] ];
let adj = matToAdj(mat);
for (let i = 0; i < V; i++) {
// printing adjacent vertices
// to vertex i
process.stdout.write(i + ": ");
let row = adj[i];
for (let j of row) {
process.stdout.write(j + " ");
}
console.log();} //Driver Code Ends
`

