From 54ca4480e57d12757fa934804cce255b4897fb7f Mon Sep 17 00:00:00 2001 From: Sahil Bansal Date: Mon, 11 May 2020 21:59:17 +0530 Subject: [PATCH 1/5] Add Graph BFS algorithm --- Data-Structures/Graph/Graph.js | 38 ++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/Data-Structures/Graph/Graph.js b/Data-Structures/Graph/Graph.js index ef1a952a86..8b7da5e43e 100644 --- a/Data-Structures/Graph/Graph.js +++ b/Data-Structures/Graph/Graph.js @@ -30,6 +30,31 @@ class Graph { console.log(i + ' -> ' + vertex) } } + + /** + * Prints the Breadth first traversal of the graph from source. + * + * @param {number} source The source vertex to start BFS. + */ + bfs (source) { + let queue = []; + let visited = new Set(); + queue.unshift([source, 0]); // level of source is 0 + visited.add(source); + while (queue.length) { + let front = queue[0]; + let node = front[0]; + let level = front[1]; + queue.shift(); // remove the front of the queue + console.log(`Visited node ${node} at level ${level}.`); + for (const next of this.adjacencyMap[node]) { + if (!visited.has(next)) { // not visited + queue.unshift([next, level + 1]); // level 1 more than current + visited.add(next); + } + } + } + } } const example = () => { @@ -37,8 +62,17 @@ const example = () => { g.addVertex(1) g.addVertex(2) g.addVertex(3) - g.addEdge(1, 2) - g.addEdge(1, 3) + g.addVertex(4); + g.addVertex(5); + g.addEdge(1, 2); + g.addEdge(1, 3); + g.addEdge(2, 4); + g.addEdge(2, 5); + console.log("Printing the adjacency list:\n"); g.printGraph() + + // perform a breadth first search + console.log("\nBreadth first search at node 1:\n"); + g.bfs(1); } example() From 5419d689315dace08219da56406e3bff79d91891 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 11 May 2020 16:30:16 +0000 Subject: [PATCH 2/5] updating DIRECTORY.md --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index cc807d5ff9..0ffeca5d2c 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -70,8 +70,8 @@ * [CycleSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/CycleSort.js) * [FlashSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/FlashSort.js) * [GnomeSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/GnomeSort.js) - * [Heapsort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/Heapsort.js) * [HeapSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/HeapSort.js) + * [Heapsort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/Heapsort.js) * [InsertionSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/InsertionSort.js) * [MergeSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/MergeSort.js) * [QuickSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/QuickSort.js) From cc90939bfb1e3f3c1b12667e06dbf737a68d1260 Mon Sep 17 00:00:00 2001 From: Sahil Bansal Date: Mon, 11 May 2020 22:08:55 +0530 Subject: [PATCH 3/5] Fix failing tests --- DIRECTORY.md | 2 +- Data-Structures/Graph/Graph.js | 38 +++++++++++++++++----------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 0ffeca5d2c..cc807d5ff9 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -70,8 +70,8 @@ * [CycleSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/CycleSort.js) * [FlashSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/FlashSort.js) * [GnomeSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/GnomeSort.js) - * [HeapSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/HeapSort.js) * [Heapsort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/Heapsort.js) + * [HeapSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/HeapSort.js) * [InsertionSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/InsertionSort.js) * [MergeSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/MergeSort.js) * [QuickSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/QuickSort.js) diff --git a/Data-Structures/Graph/Graph.js b/Data-Structures/Graph/Graph.js index 8b7da5e43e..ae9e5cb8f2 100644 --- a/Data-Structures/Graph/Graph.js +++ b/Data-Structures/Graph/Graph.js @@ -37,20 +37,20 @@ class Graph { * @param {number} source The source vertex to start BFS. */ bfs (source) { - let queue = []; - let visited = new Set(); - queue.unshift([source, 0]); // level of source is 0 - visited.add(source); + const queue = [] + const visited = new Set() + queue.unshift([source, 0]) // level of source is 0 + visited.add(source) while (queue.length) { - let front = queue[0]; - let node = front[0]; - let level = front[1]; + const front = queue[0] + const node = front[0] + const level = front[1] queue.shift(); // remove the front of the queue - console.log(`Visited node ${node} at level ${level}.`); + console.log(`Visited node ${node} at level ${level}.`) for (const next of this.adjacencyMap[node]) { if (!visited.has(next)) { // not visited - queue.unshift([next, level + 1]); // level 1 more than current - visited.add(next); + queue.unshift([next, level + 1]) // level 1 more than current + visited.add(next) } } } @@ -62,17 +62,17 @@ const example = () => { g.addVertex(1) g.addVertex(2) g.addVertex(3) - g.addVertex(4); - g.addVertex(5); - g.addEdge(1, 2); - g.addEdge(1, 3); - g.addEdge(2, 4); - g.addEdge(2, 5); - console.log("Printing the adjacency list:\n"); + g.addVertex(4) + g.addVertex(5) + g.addEdge(1, 2) + g.addEdge(1, 3) + g.addEdge(2, 4) + g.addEdge(2, 5) + console.log('Printing the adjacency list:\n') g.printGraph() // perform a breadth first search - console.log("\nBreadth first search at node 1:\n"); - g.bfs(1); + console.log('\nBreadth first search at node 1:\n') + g.bfs(1) } example() From f96b3dc8c32d823bf07956cd7efa35ed4cab1227 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 11 May 2020 16:39:15 +0000 Subject: [PATCH 4/5] updating DIRECTORY.md --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index cc807d5ff9..0ffeca5d2c 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -70,8 +70,8 @@ * [CycleSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/CycleSort.js) * [FlashSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/FlashSort.js) * [GnomeSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/GnomeSort.js) - * [Heapsort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/Heapsort.js) * [HeapSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/HeapSort.js) + * [Heapsort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/Heapsort.js) * [InsertionSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/InsertionSort.js) * [MergeSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/MergeSort.js) * [QuickSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/QuickSort.js) From c20ef4adffe4bff11d5fb9cac71d664fa8a2e781 Mon Sep 17 00:00:00 2001 From: Sahil Bansal Date: Mon, 11 May 2020 22:13:34 +0530 Subject: [PATCH 5/5] Fix further failing tests --- Data-Structures/Graph/Graph.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Data-Structures/Graph/Graph.js b/Data-Structures/Graph/Graph.js index ae9e5cb8f2..503d9b4981 100644 --- a/Data-Structures/Graph/Graph.js +++ b/Data-Structures/Graph/Graph.js @@ -33,7 +33,7 @@ class Graph { /** * Prints the Breadth first traversal of the graph from source. - * + * * @param {number} source The source vertex to start BFS. */ bfs (source) { @@ -45,7 +45,7 @@ class Graph { const front = queue[0] const node = front[0] const level = front[1] - queue.shift(); // remove the front of the queue + queue.shift() // remove the front of the queue console.log(`Visited node ${node} at level ${level}.`) for (const next of this.adjacencyMap[node]) { if (!visited.has(next)) { // not visited