From 692c0dc91bd672347ca8c56ea31be1d468ce1166 Mon Sep 17 00:00:00 2001 From: algobytewise Date: Thu, 25 Feb 2021 15:22:35 +0530 Subject: [PATCH 1/7] add algorithm Breadth-first search --- Graphs/BreadthFirstSearch.js | 64 ++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 Graphs/BreadthFirstSearch.js diff --git a/Graphs/BreadthFirstSearch.js b/Graphs/BreadthFirstSearch.js new file mode 100644 index 000000000..a342cb5a9 --- /dev/null +++ b/Graphs/BreadthFirstSearch.js @@ -0,0 +1,64 @@ +/* +Breadth-first search is an algorithm for traversing a graph. It's discovers all nodes reachable from the starting position by exploring all of the neighbor nodes at the present depth prior to moving on to the nodes at the next depth level. +(description adapted from https://en.wikipedia.org/wiki/Breadth-first_search ) +(see also: https://www.koderdojo.com/blog/breadth-first-search-and-shortest-path-in-csharp-and-net-core ) +*/ + +/* +Doctests +> breadthFirstSearch(graph, "C") +[ 'C', 'D', 'A', 'B', 'E' ] +> breadthFirstSearch(graph, "A") +[ 'A', 'B', 'D', 'E' ] +> breadthFirstSearch(graph, "F") +[ 'F', 'G' ] +*/ + +function breadthFirstSearch (graph, startingNode) { + // visited keeps track of all nodes visited + const visited = [] + + // queue contains the nodes to be explored in the future + const queue = [startingNode] + + while (queue.length > 0) { + // start with the queue's first node + const node = queue.shift() + + if (!visited.includes(node)) { + // mark the node as visited + visited.push(node) + const neighbors = graph[node] + + // put all its neighbors into the queue + for (let i = 0; i < neighbors.length; i++) { + queue.push(neighbors[i]) + } + } + } + + return visited +} + +const graph = { + A: ['B', 'D'], + B: ['E'], + C: ['D'], + D: ['A'], + E: ['D'], + F: ['G'], + G: [] +} +/* + A <-> B + ʌ | + | | + v v +C --> D <-- E + +F --> G +*/ + +console.log(breadthFirstSearch(graph, 'C')) +console.log(breadthFirstSearch(graph, 'A')) +console.log(breadthFirstSearch(graph, 'F')) From d11df93cd0e3131ccbacc69753357e73ef974f5c Mon Sep 17 00:00:00 2001 From: algobytewise Date: Thu, 25 Feb 2021 15:28:52 +0530 Subject: [PATCH 2/7] Add algorithm Breadth-first shortest path --- Graphs/BreadthFirstShortestPath.js | 85 ++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 Graphs/BreadthFirstShortestPath.js diff --git a/Graphs/BreadthFirstShortestPath.js b/Graphs/BreadthFirstShortestPath.js new file mode 100644 index 000000000..ac7f27e86 --- /dev/null +++ b/Graphs/BreadthFirstShortestPath.js @@ -0,0 +1,85 @@ +/* +Breadth-first approach can be applied to determine the shortest path between two nodes in a graph. It searches the target node among all neighbors of the starting node. Then the process is repeated on the level of the neighbors of the neighbors and so on. +(See also: https://en.wikipedia.org/wiki/Breadth-first_search ) +(see also: https://www.koderdojo.com/blog/breadth-first-search-and-shortest-path-in-csharp-and-net-core ) +*/ + +/* +Doctests +> breadthFirstShortestPath(graph, 'C', 'E') +[ 'C', 'D', 'A', 'B', 'E' ] +> breadthFirstShortestPath(graph, 'E', 'B') +[ 'E', 'D', 'A', 'B' ] +> breadthFirstShortestPath(graph, 'F', 'G') +[ 'F', 'G' ] +> breadthFirstShortestPath(graph, 'A', 'G') +[] +*/ + +function breadthFirstShortestPath (graph, startNode, targetNode) { + // check if startNode & targetNode are identical + if (startNode === targetNode) { + return [startNode] + } + + // visited keeps track of all nodes visited + const visited = [] + + // queue contains the paths to be explored in the future + const initialPath = [startNode] + const queue = [initialPath] + + while (queue.length > 0) { + // start with the queue's first path + const path = queue.shift() + const node = path[path.length - 1] + + // explore this node if it hasn't been visited yet + if (!visited.includes(node)) { + // mark the node as visited + visited.push(node) + + const neighbors = graph[node] + + // create a new path in the queue for each neighbor + for (let i = 0; i < neighbors.length; i++) { + const newPath = path.concat([neighbors[i]]) + + // the first path to contain the target node is the shortest path + if (neighbors[i] === targetNode) { + return newPath + } + + // queue the new path + queue.push(newPath) + } + } + } + + // the target node was not reachable + return [] +} + +const graph = { + A: ['B', 'D'], + B: ['E'], + C: ['D'], + D: ['A'], + E: ['D'], + F: ['G'], + G: [] +} +/* + A <-> B + ʌ | + | | + v v +C --> D <-- E + +F --> G +*/ + +console.log(breadthFirstShortestPath(graph, 'C', 'E')) +console.log(breadthFirstShortestPath(graph, 'E', 'B')) +console.log(breadthFirstShortestPath(graph, 'F', 'G')) +console.log(breadthFirstShortestPath(graph, 'A', 'G')) From 853386fa97535c09edce4640be2399f6f0da06a7 Mon Sep 17 00:00:00 2001 From: algobytewise Date: Sat, 27 Feb 2021 09:44:33 +0530 Subject: [PATCH 3/7] use set to keep track of visited nodes, corresponding adjustments to doctest --- Graphs/BreadthFirstSearch.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Graphs/BreadthFirstSearch.js b/Graphs/BreadthFirstSearch.js index a342cb5a9..7546fd853 100644 --- a/Graphs/BreadthFirstSearch.js +++ b/Graphs/BreadthFirstSearch.js @@ -6,17 +6,17 @@ Breadth-first search is an algorithm for traversing a graph. It's discovers all /* Doctests -> breadthFirstSearch(graph, "C") +> Array.from(breadthFirstSearch(graph, "C")) [ 'C', 'D', 'A', 'B', 'E' ] -> breadthFirstSearch(graph, "A") +> Array.from(breadthFirstSearch(graph, "A")) [ 'A', 'B', 'D', 'E' ] -> breadthFirstSearch(graph, "F") +> Array.from(breadthFirstSearch(graph, "F")) [ 'F', 'G' ] */ function breadthFirstSearch (graph, startingNode) { // visited keeps track of all nodes visited - const visited = [] + const visited = new Set() // queue contains the nodes to be explored in the future const queue = [startingNode] @@ -25,9 +25,9 @@ function breadthFirstSearch (graph, startingNode) { // start with the queue's first node const node = queue.shift() - if (!visited.includes(node)) { + if (!visited.has(node)) { // mark the node as visited - visited.push(node) + visited.add(node) const neighbors = graph[node] // put all its neighbors into the queue From 60dd4069e8c5305547ce1aa91c2e4ed0768a109e Mon Sep 17 00:00:00 2001 From: algobytewise Date: Sat, 27 Feb 2021 09:51:51 +0530 Subject: [PATCH 4/7] use set to keep track of visited nodes, updated description --- Graphs/BreadthFirstShortestPath.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Graphs/BreadthFirstShortestPath.js b/Graphs/BreadthFirstShortestPath.js index ac7f27e86..a1704ee3a 100644 --- a/Graphs/BreadthFirstShortestPath.js +++ b/Graphs/BreadthFirstShortestPath.js @@ -1,5 +1,5 @@ /* -Breadth-first approach can be applied to determine the shortest path between two nodes in a graph. It searches the target node among all neighbors of the starting node. Then the process is repeated on the level of the neighbors of the neighbors and so on. +Breadth-first approach can be applied to determine the shortest path between two nodes in an unweighted graph. It searches the target node among all neighbors of the starting node. Then the process is repeated on the level of the neighbors of the neighbors and so on. (See also: https://en.wikipedia.org/wiki/Breadth-first_search ) (see also: https://www.koderdojo.com/blog/breadth-first-search-and-shortest-path-in-csharp-and-net-core ) */ @@ -23,7 +23,7 @@ function breadthFirstShortestPath (graph, startNode, targetNode) { } // visited keeps track of all nodes visited - const visited = [] + const visited = new Set() // queue contains the paths to be explored in the future const initialPath = [startNode] @@ -35,9 +35,9 @@ function breadthFirstShortestPath (graph, startNode, targetNode) { const node = path[path.length - 1] // explore this node if it hasn't been visited yet - if (!visited.includes(node)) { + if (!visited.has(node)) { // mark the node as visited - visited.push(node) + visited.add(node) const neighbors = graph[node] From 6735029d8ac9be9b2924a910ac32ad9308c51c79 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 28 Feb 2021 03:11:43 +0000 Subject: [PATCH 5/7] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index ac9f3c4ec..536701ff2 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -74,6 +74,7 @@ * [ZeroOneKnapsack](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/ZeroOneKnapsack.js) ## Graphs + * [BreadthFirstSearch](https://github.com/TheAlgorithms/Javascript/blob/master/Graphs/BreadthFirstSearch.js) * [ConnectedComponents](https://github.com/TheAlgorithms/Javascript/blob/master/Graphs/ConnectedComponents.js) * [Density](https://github.com/TheAlgorithms/Javascript/blob/master/Graphs/Density.js) * [DepthFirstSearchIterative](https://github.com/TheAlgorithms/Javascript/blob/master/Graphs/DepthFirstSearchIterative.js) From c1e0dcb0368b4962b71a1d1e6bf09778404ee240 Mon Sep 17 00:00:00 2001 From: Tapajyoti Bose <44058757+ruppysuppy@users.noreply.github.com> Date: Sun, 28 Feb 2021 08:44:38 +0530 Subject: [PATCH 6/7] fix: updated description comment --- Graphs/BreadthFirstShortestPath.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Graphs/BreadthFirstShortestPath.js b/Graphs/BreadthFirstShortestPath.js index a1704ee3a..18df52897 100644 --- a/Graphs/BreadthFirstShortestPath.js +++ b/Graphs/BreadthFirstShortestPath.js @@ -1,5 +1,8 @@ /* -Breadth-first approach can be applied to determine the shortest path between two nodes in an unweighted graph. It searches the target node among all neighbors of the starting node. Then the process is repeated on the level of the neighbors of the neighbors and so on. +Breadth-first approach can be applied to determine the shortest path between two nodes +in an equi-weighted graph. It searches the target node among all neighbors of the +starting node, then the process is repeated on the level of the neighbors of the +neighbors and so on. (See also: https://en.wikipedia.org/wiki/Breadth-first_search ) (see also: https://www.koderdojo.com/blog/breadth-first-search-and-shortest-path-in-csharp-and-net-core ) */ @@ -82,4 +85,4 @@ F --> G console.log(breadthFirstShortestPath(graph, 'C', 'E')) console.log(breadthFirstShortestPath(graph, 'E', 'B')) console.log(breadthFirstShortestPath(graph, 'F', 'G')) -console.log(breadthFirstShortestPath(graph, 'A', 'G')) +console.log(breadthFirstShortestPath(graph, 'A', 'G')) From 33549a345c652d8a1a7802a709834758c70b2b06 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 28 Feb 2021 03:18:51 +0000 Subject: [PATCH 7/7] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 536701ff2..67a02ded2 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -75,6 +75,7 @@ ## Graphs * [BreadthFirstSearch](https://github.com/TheAlgorithms/Javascript/blob/master/Graphs/BreadthFirstSearch.js) + * [BreadthFirstShortestPath](https://github.com/TheAlgorithms/Javascript/blob/master/Graphs/BreadthFirstShortestPath.js) * [ConnectedComponents](https://github.com/TheAlgorithms/Javascript/blob/master/Graphs/ConnectedComponents.js) * [Density](https://github.com/TheAlgorithms/Javascript/blob/master/Graphs/Density.js) * [DepthFirstSearchIterative](https://github.com/TheAlgorithms/Javascript/blob/master/Graphs/DepthFirstSearchIterative.js)