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 Matrix to Adjacency List Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Adjacency Matrix to Adjacency List

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

Problem Statement

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

You have a graph with n nodes indexed from 0 to n-1. You also have the adjacency matrix where each cell denotes whether two nodes are connected.

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

Approach

An adjacency list is a way of representing graphs, where each node corresponds to a list of nodes, to which it is connected.

  • Create a 2-D array having n rows.
  • Traverse the given matrix and if matrix[i][j] = 1 where i is the row number and j is the column number then add j in the ith row of the 2-D array.

Analysis

  • Time Complexity: O(n2)
  • Auxiliary Space Complexity: O(1)

Implementation

C++
vector<vector<int>> matrixToAdjList(int n, vector<vector<int>> matrix) {
    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;
}
Java
class Solution {
    ArrayList<Integer>[] matrixToAdjList(int n, int[][] matrix) {
        ArrayList<Integer>[] adjList = new ArrayList [n];
		for (int i = 0; i < n; i++) {
			adjList[i] = new ArrayList<Integer>();
		}
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				if(matrix[i][j] == 1) {
					adjList[i].add(j);
				}
			}
		}
		return adjList;
    }
}
Related Content
Adjacency List to Adjacency Matrix
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