Convert Adjacency List to Adjacency Matrix Representation of Graph (original) (raw)
Last Updated : 27 Oct, 2025
Given an adjacency list representation of a Graph, convert the given Adjacency List to Adjacency Matrix 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: **adj[][] = [[1, 3], [2], [], [2]]
**Output:
0 1 0 1
0 0 1 0
0 0 0 0
0 0 1 0**Input: **adj[][] = [[1, 2], [0, 2], [0, 1, 3, 4], [2], [2]]
**Output:
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
[Approach] - Traversing the Adjacency List - O(V2) Time and O(1) Space
We can simply traverse the adjacency list for each vertex, and for every neighboring node of that vertex, we mark the corresponding entry mat[i][j] = 1 in the adjacency matrix to represent an edge between them.
Follow the steps below to convert an adjacency list to an adjacency matrix:
- Initialize a matrix of size V x V with all entries set to 0.
- Iterate through each vertex i in the adjacency list.
- For every vertex j in the adjacency list of i, set mat[i][j] = 1.
Below is the implementation of the above approach:
C++ `
//Driver Code Starts #include using namespace std;
//Driver Code Ends
vector<vector> adjToMat(vector<vector>& adj, int V) { // Initialize a matrix vector<vector> matrix(V, vector(V, 0));
for (int i = 0; i < V; i++) {
for (int j : adj[i])
matrix[i][j] = 1;
}
return matrix;}
//Driver Code Starts
void addEdge(vector<vector>& adj, int u, int v, bool isUndirected) { if (isUndirected) { // undirected graph adj[u].push_back(v); adj[v].push_back(u); } else adj[u].push_back(v); }
int main() { int V = 5; vector<vector> adj(V);
// creating adjacency list
addEdge(adj, 0, 1, true);
addEdge(adj, 0, 2, true);
addEdge(adj, 1, 2, true);
addEdge(adj, 2, 3, true);
addEdge(adj, 2, 4, true);
// Get the converted adjacency matrix
vector<vector<int>> mat = adjToMat(adj, V);
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
cout << mat[i][j] << " ";
}
cout << endl;
}}
//Driver Code Ends
Java
import java.util.ArrayList;
class GFG{ static int[][] adjToMat(ArrayList<ArrayList> adj, int V) {
// Initialize a matrix
int [][]matrix = new int[V][V];
for(int i = 0; i < V; i++) {
for(int j : adj.get(i))
matrix[i][j] = 1;
}
return matrix;
}
static void addEdge(ArrayList<ArrayList<Integer>> adj, int u, int v, boolean isUndirected) {
if( isUndirected ) {
// undirected graph
adj.get(u).add(v);
adj.get(v).add(u);
}
else adj.get(u).add(v);
}
public static void main(String[] args) {
int V = 5;
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
// creating adjacency list
for (int i = 0; i < V; i++)
adj.add(new ArrayList<>());
addEdge(adj, 0, 1, true);
addEdge(adj, 0, 2, true);
addEdge(adj, 1, 2, true);
addEdge(adj, 2, 3, true);
addEdge(adj, 2, 4, true);
// Get the converted adjacency matrix
int[][] mat = adjToMat(adj, V);
for(int i = 0; i < V; i++) {
for(int j = 0; j < V; j++) {
System.out.print(mat[i][j] + " ");
}
System.out.println();
}
}}
Python
def adjToMat(adj, V): # Initialize a matrix matrix = [[0] * V for _ in range(V)]
for i in range(V):
for j in adj[i]:
matrix[i][j] = 1
return matrixdef addEdge(adj, u, v, isUndirected): if isUndirected: # undirected graph adj[u].append(v) adj[v].append(u) else: adj[u].append(v)
V = 5 adj = [[] for _ in range(V)]
creating adjacency list
addEdge(adj, 0, 1, True) addEdge(adj, 0, 2, True) addEdge(adj, 1, 2, True) addEdge(adj, 2, 3, True) addEdge(adj, 2, 4, True)
Get the converted adjacency matrix
mat = adjToMat(adj, V)
for i in range(V): print(*mat[i])
C#
using System; using System.Collections.Generic;
class GFG { static int[,] adjToMat(List<List> adj, int V) { // Initialize a matrix int[,] matrix = new int[V, V];
for (int i = 0; i < V; i++) {
foreach (int j in adj[i])
matrix[i, j] = 1;
}
return matrix;
}
static void addEdge(List<List<int>> adj, int u, int v, bool isUndirected) {
if (isUndirected) {
// undirected graph
adj[u].Add(v);
adj[v].Add(u);
} else adj[u].Add(v);
}
static void Main() {
int V = 5;
List<List<int>> adj = new List<List<int>>();
// creating adjacency list
for (int i = 0; i < V; i++)
adj.Add(new List<int>());
addEdge(adj, 0, 1, true);
addEdge(adj, 0, 2, true);
addEdge(adj, 1, 2, true);
addEdge(adj, 2, 3, true);
addEdge(adj, 2, 4, true);
// Get the converted adjacency matrix
int[,] mat = adjToMat(adj, V);
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
Console.Write(mat[i, j] + " ");
}
Console.WriteLine();
}
}}
JavaScript
function adjToMat(adj, V) { // Initialize a matrix let matrix = Array.from({ length: V }, () => Array(V).fill(0));
for (let i = 0; i < V; i++) {
for (let j of adj[i])
matrix[i][j] = 1;
}
return matrix;}
function addEdge(adj, u, v, isUndirected) { if (isUndirected) { // undirected graph adj[u].push(v); adj[v].push(u); } else adj[u].push(v); }
// Driver code
let V = 5; let adj = Array.from({ length: V }, () => []);
// creating adjacency list addEdge(adj, 0, 1, true); addEdge(adj, 0, 2, true); addEdge(adj, 1, 2, true); addEdge(adj, 2, 3, true); addEdge(adj, 2, 4, true);
// Get the converted adjacency matrix let mat = adjToMat(adj, V);
for (let i = 0; i < V; i++) { console.log(mat[i].join(" ")); }
`
Output
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

