From 80fc69ef41152761a4ec589b656befc54e562439 Mon Sep 17 00:00:00 2001 From: Rodrigo Date: Wed, 6 Jan 2021 17:10:09 -0300 Subject: [PATCH 1/6] Added Node Neighbors --- Graphs/NodeNeighbors.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Graphs/NodeNeighbors.js diff --git a/Graphs/NodeNeighbors.js b/Graphs/NodeNeighbors.js new file mode 100644 index 0000000000..cd160c6d24 --- /dev/null +++ b/Graphs/NodeNeighbors.js @@ -0,0 +1,39 @@ +class Graph{ + // Generic graph: the algorithm works regardless of direction or weight + constructor(){ + this.edges = [] + } + + addEdge(node1, node2){ + // Adding edges to the graph + this.edges.push({ + node1, + node2 + }) + } + + nodeNeighbors(node){ + // Returns an array with all of the node neighbors + let neighbors = [] + for (let i = 0; i < this.edges.length; i++) { + // Checks if they have an edge between them and if the neighbor is not + // already in the neighbors array + if (this.edges[i].node1 === node && !(neighbors.includes(this.edges[i].node2))) { + neighbors.push(this.edges[i].node2) + } + else if (this.edges[i].node2 === node && !(neighbors.includes(this.edges[i].node1))) { + neighbors.push(this.edges[i].node1) + } + } + return neighbors + } +} + +(() => { + let graph = new Graph() + graph.addEdge(1, 2) + graph.addEdge(2, 3) + graph.addEdge(3, 5) + graph.addEdge(1, 5) + console.log(graph.nodeNeighbors(1)) +})() From 5946f987a968f5cc00f7a08345d4c039e3a6ad4c Mon Sep 17 00:00:00 2001 From: Rodrigo Date: Wed, 6 Jan 2021 17:13:52 -0300 Subject: [PATCH 2/6] Added wikipedia link --- Graphs/NodeNeighbors.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Graphs/NodeNeighbors.js b/Graphs/NodeNeighbors.js index cd160c6d24..54ec4b63a7 100644 --- a/Graphs/NodeNeighbors.js +++ b/Graphs/NodeNeighbors.js @@ -1,3 +1,5 @@ +// https://en.wikipedia.org/wiki/Neighbourhood_(graph_theory) + class Graph{ // Generic graph: the algorithm works regardless of direction or weight constructor(){ From 7b15f66a8695dc654072ce368c9166ffe2244eca Mon Sep 17 00:00:00 2001 From: Rodrigo Date: Wed, 6 Jan 2021 17:37:19 -0300 Subject: [PATCH 3/6] Added JS standard --- Graphs/NodeNeighbors.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/Graphs/NodeNeighbors.js b/Graphs/NodeNeighbors.js index 54ec4b63a7..473cdf0f15 100644 --- a/Graphs/NodeNeighbors.js +++ b/Graphs/NodeNeighbors.js @@ -1,12 +1,12 @@ // https://en.wikipedia.org/wiki/Neighbourhood_(graph_theory) -class Graph{ +class Graph { // Generic graph: the algorithm works regardless of direction or weight - constructor(){ + constructor () { this.edges = [] } - addEdge(node1, node2){ + addEdge (node1, node2) { // Adding edges to the graph this.edges.push({ node1, @@ -14,16 +14,15 @@ class Graph{ }) } - nodeNeighbors(node){ + nodeNeighbors (node) { // Returns an array with all of the node neighbors - let neighbors = [] + const neighbors = [] for (let i = 0; i < this.edges.length; i++) { // Checks if they have an edge between them and if the neighbor is not // already in the neighbors array if (this.edges[i].node1 === node && !(neighbors.includes(this.edges[i].node2))) { neighbors.push(this.edges[i].node2) - } - else if (this.edges[i].node2 === node && !(neighbors.includes(this.edges[i].node1))) { + } else if (this.edges[i].node2 === node && !(neighbors.includes(this.edges[i].node1))) { neighbors.push(this.edges[i].node1) } } @@ -32,7 +31,7 @@ class Graph{ } (() => { - let graph = new Graph() + const graph = new Graph() graph.addEdge(1, 2) graph.addEdge(2, 3) graph.addEdge(3, 5) From 1d72cbb27329376f4998a0d7ce3bedff2c49153e Mon Sep 17 00:00:00 2001 From: Rodrigo Morais <58960796+rodigu@users.noreply.github.com> Date: Sun, 17 Jan 2021 01:11:13 -0300 Subject: [PATCH 4/6] Update Graphs/NodeNeighbors.js Co-authored-by: Tapajyoti Bose <44058757+ruppysuppy@users.noreply.github.com> --- Graphs/NodeNeighbors.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Graphs/NodeNeighbors.js b/Graphs/NodeNeighbors.js index 473cdf0f15..74d5825250 100644 --- a/Graphs/NodeNeighbors.js +++ b/Graphs/NodeNeighbors.js @@ -16,7 +16,7 @@ class Graph { nodeNeighbors (node) { // Returns an array with all of the node neighbors - const neighbors = [] + const neighbors = new Set() for (let i = 0; i < this.edges.length; i++) { // Checks if they have an edge between them and if the neighbor is not // already in the neighbors array From 686ba7e8f985e71fd0662292ad0d4100bf96f116 Mon Sep 17 00:00:00 2001 From: rodigu Date: Mon, 18 Jan 2021 20:54:44 -0300 Subject: [PATCH 5/6] Added suggestions and updated for loop --- Graphs/NodeNeighbors.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Graphs/NodeNeighbors.js b/Graphs/NodeNeighbors.js index 74d5825250..7ab988fd12 100644 --- a/Graphs/NodeNeighbors.js +++ b/Graphs/NodeNeighbors.js @@ -17,13 +17,13 @@ class Graph { nodeNeighbors (node) { // Returns an array with all of the node neighbors const neighbors = new Set() - for (let i = 0; i < this.edges.length; i++) { + for (let edge of this.edges) { // Checks if they have an edge between them and if the neighbor is not // already in the neighbors array - if (this.edges[i].node1 === node && !(neighbors.includes(this.edges[i].node2))) { - neighbors.push(this.edges[i].node2) - } else if (this.edges[i].node2 === node && !(neighbors.includes(this.edges[i].node1))) { - neighbors.push(this.edges[i].node1) + if (edge.node1 === node && !(neighbors.has(edge.node2))) { + neighbors.add(edge.node2) + } else if (edge.node2 === node && !(neighbors.has(edge.node1))) { + neighbors.add(edge.node1) } } return neighbors From 3a1d21a9874be17bd32a2c4276c34e91f34fb5c1 Mon Sep 17 00:00:00 2001 From: rodigu Date: Mon, 18 Jan 2021 20:56:58 -0300 Subject: [PATCH 6/6] Corrected let to const --- Graphs/NodeNeighbors.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Graphs/NodeNeighbors.js b/Graphs/NodeNeighbors.js index 7ab988fd12..c22b9905a7 100644 --- a/Graphs/NodeNeighbors.js +++ b/Graphs/NodeNeighbors.js @@ -17,7 +17,7 @@ class Graph { nodeNeighbors (node) { // Returns an array with all of the node neighbors const neighbors = new Set() - for (let edge of this.edges) { + for (const edge of this.edges) { // Checks if they have an edge between them and if the neighbor is not // already in the neighbors array if (edge.node1 === node && !(neighbors.has(edge.node2))) {