The Cartographer: Adj Matrix

EasyAcc. 95.2%
+20 XP 8

Mapping Connections

A graph is a set of nodes (vertices) connected by edges. An Adjacency Matrix is a 2D array where adj[i][j] = 1 means node i is connected to node j.

The Assignment

Your receives numNodes and an edge list edges.

  1. Initialize an n x n matrix filled with 0s.
  2. For each edge [u, v], set matrix[u][v] = 1 and matrix[v][u] = 1 (undirected).
  3. Print the matrix.

01EXAMPLE 1

InputnumNodes=3, edges=[[0,1], [1,2]]
Output0 1 0 1 0 1 0 1 0

Explanation: Node 1 is connected to 0 and 2.

Constraints

  • Nodes are indexed from 0 to n-1.
GraphsMatrix
JavaScript
Loading...
1 Hidden

Input Arguments

numNodes3
edges[[0,1],[1,2]]

Expected Output

0 1 0
1 0 1
0 1 0

Click RUN to test your solution