curriculum/challenges/english/blocks/lab-adjacency-list-to-matrix-converter-js/698b42492a7f5f737895892e.md
In this lab, you will build a function that converts an adjacency list representation of a graph into an adjacency matrix. An adjacency list is an object where each key represents a node, and the corresponding value is an array of nodes that the key node is connected to. An adjacency matrix is a 2D array where the entry at position [i][j] is 1 if there's an edge from node i to node j, and 0 otherwise.
For example, given the adjacency list:
{
0: [1, 2],
1: [2],
2: [0, 3],
3: [2]
}
The corresponding adjacency matrix would be:
[
[0, 1, 1, 0],
[0, 0, 1, 0],
[1, 0, 0, 1],
[0, 0, 1, 0]
]
Objective: Fulfill the user stories below and get all the tests to pass to complete the lab.
User Stories:
adjacencyListToMatrix to convert an adjacency list to an adjacency matrix.For example, adjacencyListToMatrix({0: [2], 1: [2, 3], 2: [0, 1, 3], 3: [1, 2]}) should print:
[0, 0, 1, 0]
[0, 0, 1, 1]
[1, 1, 0, 1]
[0, 1, 1, 0]
and return [[0, 0, 1, 0], [0, 0, 1, 1], [1, 1, 0, 1], [0, 1, 1, 0]].
You should define a function named adjacencyListToMatrix.
assert.isFunction(adjacencyListToMatrix);
The adjacencyListToMatrix function should have one parameter.
assert.lengthOf(adjacencyListToMatrix, 1);
The function should correctly determine the number of nodes from the adjacency list.
const adjList = {0: [1], 1: [0], 2: []};
const result = adjacencyListToMatrix(adjList);
assert.lengthOf(result, 3);
result.forEach(arr => assert.lengthOf(arr, 3));
The function should correctly set matrix values to 1 for existing edges.
const adjList = {0: [1], 1: [0]};
const result = adjacencyListToMatrix(adjList);
const expected = [[0, 1], [1, 0]];
assert.sameDeepOrderedMembers(result, expected);
The function should print each row of the matrix.
const spy = __helpers.spyOn(console, "log");
try {
const adjList = {0: [1], 1: []}
adjacencyListToMatrix(adjList);
const args = spy.calls;
assert.sameDeepOrderedMembers(args[0], [ [0, 1] ]);
assert.sameDeepOrderedMembers(args[1], [ [0, 0] ]);
} catch (e) {
assert.fail(e);
} finally {
spy.restore();
}
The function should return the adjacency matrix.
const adjList = {0: [1], 1: []};
const result = adjacencyListToMatrix(adjList);
assert.sameDeepOrderedMembers(result, [[0, 1], [0, 0]]);
When given the adjacency list {0: [1, 2], 1: [2], 2: [0, 3], 3: [2]}, the function should return [[0, 1, 1, 0], [0, 0, 1, 0], [1, 0, 0, 1], [0, 0, 1, 0]].
const adjList = {0: [1, 2], 1: [2], 2: [0, 3], 3: [2]};
const result = adjacencyListToMatrix(adjList);
const expected = [[0, 1, 1, 0], [0, 0, 1, 0], [1, 0, 0, 1], [0, 0, 1, 0]];
assert.sameDeepOrderedMembers(result, expected);
When given the adjacency list {0: [1], 1: [0]}, the function should return [[0, 1], [1, 0]].
const adjList = {0: [1], 1: [0]};
const result = adjacencyListToMatrix(adjList);
const expected = [[0, 1], [1, 0]];
assert.sameDeepOrderedMembers(result, expected);
When given the adjacency list {0: [], 1: [], 2: []}, the function should return [[0, 0, 0], [0, 0, 0], [0, 0, 0]].
const adjList = {0: [], 1: [], 2: []};
const result = adjacencyListToMatrix(adjList);
const expected = [[0, 0, 0], [0, 0, 0], [0, 0, 0]];
assert.sameDeepOrderedMembers(result, expected);
function adjacencyListToMatrix(adjList) {
const n = Object.keys(adjList).length;
const adjMatrix = Array.from({ length: n }, () => Array(n).fill(0));
for (const [srcStr, neighbors] of Object.entries(adjList)) {
const srcNode = Number(srcStr);
for (const destNode of neighbors) {
adjMatrix[srcNode][destNode] = 1;
}
}
for (const row of adjMatrix) {
console.log(row);
}
return adjMatrix;
}
const adjList = {
0: [1, 2],
1: [2],
2: [0, 3],
3: [2]
};
const matrix = adjacencyListToMatrix(adjList);