From 86459ce5c1defe15b05d3aa5c9428b8f516fcd33 Mon Sep 17 00:00:00 2001 From: Manish Date: Wed, 13 Sep 2023 13:36:39 +1000 Subject: [PATCH] Data Structures: Adjacency Matrix --- Data Structures/adjacencyMatrix.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Data Structures/adjacencyMatrix.js diff --git a/Data Structures/adjacencyMatrix.js b/Data Structures/adjacencyMatrix.js new file mode 100644 index 0000000..1bc7998 --- /dev/null +++ b/Data Structures/adjacencyMatrix.js @@ -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);