Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Adjacency List to Adjacency Matrix Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Adjacency List to Adjacency Matrix

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given the nodes and adjacency list of a graph, calculate the adjacency matrix for it.

You have a graph with n nodes indexed from 0 to n-1. You are also given the adjacency list for each node.

You have to return the adjacency matrix for the given graph.

Approach

An Adjacency matrix is a way of representing graphs, where matrx[i][j] is 1 if there is an edge between i and j, and 0 otherwise.

  • Create a matrix of size n * n and initialize it with zero.
  • Traverse the given adjacency list and for every value j in a row i change matrix[i][j] to 1.

Analysis

  • Time Complexity: O(V + E)
  • Auxiliary Space Complexity: O(1)

Where V denotes the number of nodes and E denotes the number of edges.

Implementation

C++
vector<vector<int>> adjListToMatrix(int n, vector<vector<int>> adjList) {
    vector<vector<int>> matrix(n, vector<int> (n, 0));
	for(int i = 0; i < n; i++) {
		for (int j = 0; j < adjList[i].size();j++) {
			matrix[i][adjList[i][j]] = 1;
		}
	}
	return matrix;
}
Java
class Solution {
    int[][] adjListToMatrix(int n, ArrayList<Integer>[] adjList) {
        int[][] matrix = new int[n][n];
		for(int i = 0; i < n; i++) {
			for (int j = 0; j < adjList[i].size();j++) {
				matrix[i][adjList[i].get(j)] = 1;
			}
		}
		return matrix;
    }
}
Related Content
Adjacency Matrix to Adjacency List
BFS of a Graph
Capture Surrounded Regions
Clone a Graph
DFS of an Acyclic Graph
DFS of a Cyclic Graph
Edges to Adjacency List
Flood Fill Image
Knight's Journey On A Chessboard
M-Coloring Problem
Number of Islands
Stepping Numbers
Valid Path
Word Search Board
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet