From d11df93cd0e3131ccbacc69753357e73ef974f5c Mon Sep 17 00:00:00 2001 From: algobytewise Date: Thu, 25 Feb 2021 15:28:52 +0530 Subject: [PATCH 1/3] 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 60dd4069e8c5305547ce1aa91c2e4ed0768a109e Mon Sep 17 00:00:00 2001 From: algobytewise Date: Sat, 27 Feb 2021 09:51:51 +0530 Subject: [PATCH 2/3] 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 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 3/3] 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'))