Complete the code to initialize an empty adjacency list for a graph.
const adjacencyList: Record<number, number[]> = [1];We use an empty object {} to represent the adjacency list as a map from node to its neighbors.
Complete the code to add an edge from node 1 to node 2 in the adjacency list.
if (!adjacencyList[1]) adjacencyList[1] = []; adjacencyList[1].[1](2);
We use push to add node 2 to the neighbors of node 1.
Fix the error in the code to correctly add an undirected edge between nodes 3 and 4.
if (!adjacencyList[3]) adjacencyList[3] = []; adjacencyList[3].push(4); if (!adjacencyList[4]) adjacencyList[4] = []; adjacencyList[4].[1](3);
To add the edge back from node 4 to node 3, we use push to add 3 to adjacencyList[4].
Fill both blanks to create an adjacency list for nodes 1 to 3 with edges 1->2 and 2->3.
const adjacencyList = {
1: [[1]],
2: [[2]],
3: []
};Node 1 connects to 2, and node 2 connects to 3, so adjacencyList[1] = [2] and adjacencyList[2] = [3].
Fill all three blanks to add edges 5->6, 6->7, and 7->5 in the adjacency list.
const adjacencyList = {};
if (!adjacencyList[5]) adjacencyList[5] = [];
adjacencyList[5].push([1]);
if (!adjacencyList[6]) adjacencyList[6] = [];
adjacencyList[6].push([2]);
if (!adjacencyList[7]) adjacencyList[7] = [];
adjacencyList[7].push([3]);Edges are 5->6, 6->7, and 7->5, so we push 6 to adjacencyList[5], 7 to adjacencyList[6], and 5 to adjacencyList[7].