971. Convert Adjacency Matrix to List

EasyGraphsRepresentationGraph

Given the n x n adjacency matrix of a graph, convert it to an adjacency list: for each node, the sorted list of nodes it connects to (indices j where matrix[i][j] is 1). Return the adjacency list as a nested array. The input is JSON {matrix}.

Input: JSON {matrix}.

Output: Nested array — sorted neighbors of each node.

Examples

Example 1
Input: {"matrix":[[0,1,1],[1,0,0],[1,0,0]]}
Output: [[1,2],[0],[0]]
Explanation: Neighbors read off each row.
Example 2
Input: {"matrix":[[0]]}
Output: [[]]
Explanation: Single node.
Example 3
Input: {"matrix":[[0,1],[1,0]]}
Output: [[1],[0]]
Explanation: One edge.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →