Complete the code to create an adjacency list for a graph with 3 nodes.
const graph = {
0: [],
1: [],
2: []
};
graph[0].push([1]);
console.log(graph);We add node 1 as a neighbor to node 0 in the adjacency list.
Complete the code to initialize a 3x3 adjacency matrix with zeros.
const size = 3; const matrix = Array(size).fill(null).map(() => Array(size).fill([1])); console.log(matrix);
Adjacency matrices start with 0 to show no edges between nodes.
Fix the error in the code that adds an edge in the adjacency matrix.
const matrix = [ [0, 0, 0], [0, 0, 0], [0, 0, 0] ]; matrix[0][[1]] = 1; console.log(matrix);
Index 2 is the correct column to add an edge from node 0 to node 2.
Fill both blanks to create an adjacency list and add an edge from node 1 to node 2.
const graph = {
0: [],
1: [],
2: []
};
graph[[1]].push([2]);
console.log(graph);We add node 2 as a neighbor to node 1 in the adjacency list.
Fill all three blanks to add an edge in the adjacency matrix from node 2 to node 0.
const matrix = [ [0, 0, 0], [0, 0, 0], [0, 0, 0] ]; matrix[[1]][[2]] = [3]; console.log(matrix);
We set matrix[2][0] = 1 to show an edge from node 2 to node 0.
