Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d11df93

Browse files
Add algorithm Breadth-first shortest path
1 parent 4bc2b28 commit d11df93

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

‎Graphs/BreadthFirstShortestPath.js‎

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /