Skip to content

Commit 3c3cae6

Browse files
authored
Merge pull request #572 from algobytewise/add-BreadthFirstShortestPath
Add algorithm Breadth-first shortest path
2 parents 6735029 + c1e0dcb commit 3c3cae6

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

Graphs/BreadthFirstShortestPath.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
Breadth-first approach can be applied to determine the shortest path between two nodes
3+
in an equi-weighted graph. It searches the target node among all neighbors of the
4+
starting node, then the process is repeated on the level of the neighbors of the
5+
neighbors and so on.
6+
(See also: https://en.wikipedia.org/wiki/Breadth-first_search )
7+
(see also: https://www.koderdojo.com/blog/breadth-first-search-and-shortest-path-in-csharp-and-net-core )
8+
*/
9+
10+
/*
11+
Doctests
12+
> breadthFirstShortestPath(graph, 'C', 'E')
13+
[ 'C', 'D', 'A', 'B', 'E' ]
14+
> breadthFirstShortestPath(graph, 'E', 'B')
15+
[ 'E', 'D', 'A', 'B' ]
16+
> breadthFirstShortestPath(graph, 'F', 'G')
17+
[ 'F', 'G' ]
18+
> breadthFirstShortestPath(graph, 'A', 'G')
19+
[]
20+
*/
21+
22+
function breadthFirstShortestPath (graph, startNode, targetNode) {
23+
// check if startNode & targetNode are identical
24+
if (startNode === targetNode) {
25+
return [startNode]
26+
}
27+
28+
// visited keeps track of all nodes visited
29+
const visited = new Set()
30+
31+
// queue contains the paths to be explored in the future
32+
const initialPath = [startNode]
33+
const queue = [initialPath]
34+
35+
while (queue.length > 0) {
36+
// start with the queue's first path
37+
const path = queue.shift()
38+
const node = path[path.length - 1]
39+
40+
// explore this node if it hasn't been visited yet
41+
if (!visited.has(node)) {
42+
// mark the node as visited
43+
visited.add(node)
44+
45+
const neighbors = graph[node]
46+
47+
// create a new path in the queue for each neighbor
48+
for (let i = 0; i < neighbors.length; i++) {
49+
const newPath = path.concat([neighbors[i]])
50+
51+
// the first path to contain the target node is the shortest path
52+
if (neighbors[i] === targetNode) {
53+
return newPath
54+
}
55+
56+
// queue the new path
57+
queue.push(newPath)
58+
}
59+
}
60+
}
61+
62+
// the target node was not reachable
63+
return []
64+
}
65+
66+
const graph = {
67+
A: ['B', 'D'],
68+
B: ['E'],
69+
C: ['D'],
70+
D: ['A'],
71+
E: ['D'],
72+
F: ['G'],
73+
G: []
74+
}
75+
/*
76+
A <-> B
77+
ʌ |
78+
| |
79+
v v
80+
C --> D <-- E
81+
82+
F --> G
83+
*/
84+
85+
console.log(breadthFirstShortestPath(graph, 'C', 'E'))
86+
console.log(breadthFirstShortestPath(graph, 'E', 'B'))
87+
console.log(breadthFirstShortestPath(graph, 'F', 'G'))
88+
console.log(breadthFirstShortestPath(graph, 'A', 'G'))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy