Skip to content
DeepForgeby svx
All problems
EasyGraph Algorithmsgraph-002

Adjacency Matrix Build

Problem

Build the n by n adjacency matrix of an undirected graph from a list of edges.

edges is a list of [u, v] pairs. Return an n by n matrix of 0/1 values where both [u][v] and [v][u] are 1 for every edge.

Hint

Set both directions for each edge on a fresh zero matrix.

Test cases

case 1
in: [3,[[0,1],[1,2]]]
expected: [[0,1,0],[1,0,1],[0,1,0]]
case 2
in: [3,[[0,1],[0,2],[1,2]]]
expected: [[0,1,1],[1,0,1],[1,1,0]]
case 3
in: [1,[]]
expected: [[0]]
case 4
in: [4,[]]
expected: [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
Starter codePython
def build_adjacency_matrix(n, edges):
    # Your code here
    pass

4 test cases · run your code against them in the editor