Data Structures: Adjacency Matrix
This commit is contained in:
parent
3783a249f9
commit
86459ce5c1
22
Data Structures/adjacencyMatrix.js
Normal file
22
Data Structures/adjacencyMatrix.js
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
// https://www.freecodecamp.org/learn/coding-interview-prep/data-structures/adjacency-matrix
|
||||||
|
|
||||||
|
const row = [];
|
||||||
|
for (let i = 0; i < 5; i++) {
|
||||||
|
row.push(0);
|
||||||
|
}
|
||||||
|
const adjMatUndirected = [];
|
||||||
|
for (let i = 0; i < 5; i++) {
|
||||||
|
adjMatUndirected.push([...row]);
|
||||||
|
}
|
||||||
|
const edges = [
|
||||||
|
[1, 4],
|
||||||
|
[1, 3],
|
||||||
|
[3, 5],
|
||||||
|
[4, 5],
|
||||||
|
];
|
||||||
|
edges.forEach((e) => {
|
||||||
|
adjMatUndirected[e[0] - 1][e[1] - 1] = 1;
|
||||||
|
adjMatUndirected[e[1] - 1][e[0] - 1] = 1;
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(adjMatUndirected);
|
Loading…
Reference in New Issue
Block a user