From f4b4db0d8b254678d1c71554c28289f38af1100b Mon Sep 17 00:00:00 2001 From: Manish Date: Thu, 14 Sep 2023 11:11:47 +1000 Subject: [PATCH] Data Structures: Incidence Matrix --- Data Structures/incidenceMatrix.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Data Structures/incidenceMatrix.js diff --git a/Data Structures/incidenceMatrix.js b/Data Structures/incidenceMatrix.js new file mode 100644 index 0000000..584640b --- /dev/null +++ b/Data Structures/incidenceMatrix.js @@ -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);