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.
- Initialize an
n x nmatrix filled with 0s. - For each edge
[u, v], setmatrix[u][v] = 1andmatrix[v][u] = 1(undirected). - Print the matrix.
01EXAMPLE 1
Input
numNodes=3, edges=[[0,1], [1,2]]Output
0 1 0
1 0 1
0 1 0Explanation: Node 1 is connected to 0 and 2.
Constraints
- Nodes are indexed from 0 to n-1.
GraphsMatrix
JavaScriptSystem handles I/O — write your function only
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