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