Complete the code to initialize an adjacency matrix for a graph with 4 nodes.
const size = 4; const adjacencyMatrix = Array(size).fill(null).map(() => Array(size).fill([1]));
The adjacency matrix should start with 0s indicating no edges between nodes.
Complete the code to add an edge from node 1 to node 3 in the adjacency matrix.
adjacencyMatrix[1][[1]] = 1;
Node indices start at 0, so node 3 is index 2.
Fix the error in the code to correctly check if there is an edge from node 2 to node 0.
if (adjacencyMatrix[[1]][0] === 1) { console.log('Edge exists'); }
Node 2 corresponds to index 2 in the matrix.
Fill both blanks to create a function that adds an undirected edge between two nodes in the adjacency matrix.
function addEdge(matrix, node1, node2) {
matrix[node1][[1]] = 1;
matrix[node2][[2]] = 1;
}For undirected edges, set both [node1][node2] and [node2][node1] to 1.
Fill all three blanks to create a function that returns all neighbors of a given node from the adjacency matrix.
function getNeighbors(matrix, node) {
const neighbors = [];
for (let i = 0; i < matrix[[1]].length; i++) {
if (matrix[node][i] === [2]) {
neighbors.push([3]);
}
}
return neighbors;
}Loop over the row for the given node, check if edge exists (value 1), then add the column index as neighbor.
