Given an adjacency list representation of a **Graph**, the task is to convert the given **Adjacency List** to **Adjacency Matrix** representation.

**Examples:**

Input:adjList[] = {{0 –> 1 –> 3}, {1 –> 2}, {2 –> 3}}

Output:

0 1 0 1

0 0 1 0

0 0 0 1

0 0 0 0

Input:adjList[] = {{0 –> 1 –> 4}, {1 –> 0 –> 2 –> 3 –> 4}, {2 –> 1 –> 3}, {3 –> 1 –> 2 –> 4}, {4 –> 0 –> 1 –> 3}}

Output:

0 1 0 0 1

1 0 1 1 1

0 1 0 1 0

0 1 1 0 1

1 1 0 1 0

**Adjacency List:** An array of lists is used. The size of the array is equal to the number of vertices. Let the array be an array[]. An entry array[i] represents the list of vertices adjacent to the i^{th} Vertex.

**Adjacency Matrix:** Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a graph. Let the 2D array be adj[][], a slot adj[i][j] = 1 indicates that there is an edge from vertex i to vertex j.

Follow the steps below to convert an adjacency list to an adjacency matrix:

- Initialize a matrix with
**0**s. - Iterate over the vertices in the adjacency list
- For every
**j**vertex in the adjacency list, traverse its edges.^{th} - For each vertex
**i**with which the**j**vertex has an edge, set mat[i][j] = 1.^{th}

Below is the implementation of the above approach:

## C++

`// C++ Program to implement ` `// the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to insert vertices to adjacency list ` `void` `insert(vector<` `int` `> adj[], ` `int` `u, ` `int` `v) ` `{ ` ` ` `// Insert a vertex v to vertex u ` ` ` `adj[u].push_back(v); ` ` ` `return` `; ` `} ` ` ` `// Function to display adjacency list ` `void` `printList(vector<` `int` `> adj[], ` `int` `V) ` `{ ` ` ` `for` `(` `int` `i = 0; i < V; i++) { ` ` ` `cout << i; ` ` ` `for` `(` `auto` `j : adj[i]) ` ` ` `cout << ` `" --> "` `<< j; ` ` ` `cout << endl; ` ` ` `} ` ` ` `cout << endl; ` `} ` ` ` `// Function to convert adjacency ` `// list to adjacency matrix ` `vector<vector<` `int` `> > convert(vector<` `int` `> adj[], ` ` ` `int` `V) ` `{ ` ` ` `// Initialize a matrix ` ` ` `vector<vector<` `int` `> > matrix(V, ` ` ` `vector<` `int` `>(V, 0)); ` ` ` ` ` `for` `(` `int` `i = 0; i < V; i++) { ` ` ` `for` `(` `auto` `j : adj[i]) ` ` ` `matrix[i][j] = 1; ` ` ` `} ` ` ` `return` `matrix; ` `} ` ` ` `// Function to display adjacency matrix ` `void` `printMatrix(vector<vector<` `int` `> > adj, ` `int` `V) ` `{ ` ` ` `for` `(` `int` `i = 0; i < V; i++) { ` ` ` `for` `(` `int` `j = 0; j < V; j++) { ` ` ` `cout << adj[i][j] << ` `" "` `; ` ` ` `} ` ` ` `cout << endl; ` ` ` `} ` ` ` `cout << endl; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `V = 5; ` ` ` ` ` `vector<` `int` `> adjList[V]; ` ` ` ` ` `// Inserting edges ` ` ` `insert(adjList, 0, 1); ` ` ` `insert(adjList, 0, 4); ` ` ` `insert(adjList, 1, 0); ` ` ` `insert(adjList, 1, 2); ` ` ` `insert(adjList, 1, 3); ` ` ` `insert(adjList, 1, 4); ` ` ` `insert(adjList, 2, 1); ` ` ` `insert(adjList, 2, 3); ` ` ` `insert(adjList, 3, 1); ` ` ` `insert(adjList, 3, 2); ` ` ` `insert(adjList, 3, 4); ` ` ` `insert(adjList, 4, 0); ` ` ` `insert(adjList, 4, 1); ` ` ` `insert(adjList, 4, 3); ` ` ` ` ` `// Display adjacency list ` ` ` `cout << ` `"Adjacency List: \n"` `; ` ` ` `printList(adjList, V); ` ` ` ` ` `// Function call which returns ` ` ` `// adjacency matrix after conversion ` ` ` `vector<vector<` `int` `> > adjMatrix ` ` ` `= convert(adjList, V); ` ` ` ` ` `// Display adjacency matrix ` ` ` `cout << ` `"Adjacency Matrix: \n"` `; ` ` ` `printMatrix(adjMatrix, V); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Adjacency List: 0 --> 1 --> 4 1 --> 0 --> 2 --> 3 --> 4 2 --> 1 --> 3 3 --> 1 --> 2 --> 4 4 --> 0 --> 1 --> 3 Adjacency Matrix: 0 1 0 0 1 1 0 1 1 1 0 1 0 1 0 0 1 1 0 1 1 1 0 1 0

**Time Complexity: **O(N*M)

**Auxiliary Space:** O(N^{2})

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Convert Adjacency Matrix to Adjacency List representation of Graph
- Comparison between Adjacency List and Adjacency Matrix representation of Graph
- Add and Remove vertex in Adjacency Matrix representation of Graph
- Add and Remove Edge in Adjacency Matrix representation of a Graph
- Add and Remove vertex in Adjacency List representation of Graph
- Add and Remove Edge in Adjacency List representation of a Graph
- Prim's Algorithm (Simple Implementation for Adjacency Matrix Representation)
- Prim’s MST for Adjacency List Representation | Greedy Algo-6
- Dijkstra’s Algorithm for Adjacency List Representation | Greedy Algo-8
- C program to implement Adjacency Matrix of a given Graph
- DFS for a n-ary tree (acyclic graph) represented as adjacency list
- Implementation of BFS using adjacency matrix
- Kruskal's Algorithm (Simple Implementation for Adjacency Matrix)
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Graph Representation using Java ArrayList
- Convert undirected connected graph to strongly connected directed graph
- Convert Directed Graph into a Tree
- Minimum steps required to convert X to Y where a binary matrix represents the possible conversions
- Maximum number of edges that N-vertex graph can have such that graph is Triangle free | Mantel's Theorem
- Detect cycle in the graph using degrees of nodes of graph

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.