970. Build Adjacency Matrix from Edge List

EasyGraphsRepresentationGraph

Given an undirected graph with n nodes labeled 0..n-1 and a list of edges, build its n x n adjacency matrix where entry [i][j] is 1 if an edge connects i and j, else 0. Return the matrix as a nested array. The input is JSON {n, edges}.

Input: JSON {n, edges}.

Output: Nested array — the n x n adjacency matrix.

Examples

Example 1
Input: {"n":3,"edges":[[0,1],[0,2]]}
Output: [[0,1,1],[1,0,0],[1,0,0]]
Explanation: Symmetric matrix for undirected edges.
Example 2
Input: {"n":1,"edges":[]}
Output: [[0]]
Explanation: Single node, no edges.
Example 3
Input: {"n":2,"edges":[[0,1]]}
Output: [[0,1],[1,0]]
Explanation: One undirected edge.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →