Published by Dimitris Kougioumtzis · Updated
Expand complete BFS layers from node 0 and from the highest node on reversed edges. Weights are ignored.
Expand complete layers from the source and from the target on reversed edges, joining parent paths at a meeting vertex.
When to use Bidirectional breadth-first search
One minimum-hop route between known endpoints.
Choosing an alternative
One-sided BFS explores only from the source; Dijkstra incorporates weights.
Worked example: input and state changes
Example input
0,1,1;1,2,1;2,0,1;2,3,1;3,4,1;4,3,1
These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.
Step 0: Initialize graph state.
State at this checkpoint
{ "values": [ "-", "-", "-", "-", "-" ], "active": [], "kind": "graph", "chosen": [], "visited": [] }Step 3: Discover 1 from the source side.
State at this checkpoint
{ "values": [ "F:0 B:-", "F:1 B:-", "F:- B:-", "F:- B:-", "F:- B:0" ], "active": [ 0, 1 ], "kind": "graph", "chosen": [], "visited": [], "forward": [ 0, 1, -1, -1, -1 ], "backward": [ -1, -1, -1, -1, 0 ] }Step 5: Discover 3 from the target side.
State at this checkpoint
{ "values": [ "F:0 B:-", "F:1 B:-", "F:- B:-", "F:- B:1", "F:- B:0" ], "active": [ 4, 3 ], "kind": "graph", "chosen": [], "visited": [], "forward": [ 0, 1, -1, -1, -1 ], "backward": [ -1, -1, -1, 1, 0 ] }Step 8: Expand reverse BFS layer at 3.
State at this checkpoint
{ "values": [ "F:0 B:-", "F:1 B:-", "F:2 B:-", "F:- B:1", "F:- B:0" ], "active": [ 3 ], "kind": "graph", "chosen": [], "visited": [], "forward": [ 0, 1, 2, -1, -1 ], "backward": [ -1, -1, -1, 1, 0 ] }Step 10: Shortest path has 4 edges: 0 > 1 > 2 > 3 > 4.
State at this checkpoint
{ "values": [ "F:0 B:-", "F:1 B:-", "F:2 B:2", "F:- B:1", "F:- B:0" ], "active": [], "kind": "graph", "chosen": [ 0, 1, 3, 4 ], "visited": [ 0, 1, 2, 3, 4 ], "result": 4, "path": [ 0, 1, 2, 3, 4 ], "forward": [ 0, 1, 2, -1, -1 ], "backward": [ -1, -1, 2, 1, 0 ] }
Expected final state
Shortest path has 4 edges: 0 > 1 > 2 > 3 > 4.
Complexity and input bounds
Family: Graphs. Time complexity: O(V + VE) with edge-array scans.
Space complexity: O(V) frontiers, distances, and parents.
Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.
Directed edges as source,target,weight; separated by semicolons. Nodes 0–7, positive weights 1–99. Start node is 0. At most 16 edges. Weights are ignored. Vertices include every index from 0 to the highest endpoint.
The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.
Common mistakes and limitations
Reverse search must follow incoming original edges, and complete layers support the stopping argument.
Further reading
For background on algorithm analysis and the JavaScript function interface:
- MIT OpenCourseWare: algorithm design and analysis foundations
- MDN: JavaScript function parameters and return values
Editorial and testing approach · Browse all algorithm guides
Complete JavaScript implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | // Edge arrays from/to/weight; 1..8 vertices indexed 0..vertices-1, 0..16 edges, endpoints in bounds. Weights ignored. Output buffer at least 256 integers. Directed shortest unweighted path from 0 to vertices-1. Returns edge count or -1; writes path nodes including both endpoints. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation. function algorithm(from, to, weight, edges, vertices, output) { let forward = Array(256).fill(0); let backward = Array(256).fill(0); let parent = Array(256).fill(0); let next = Array(256).fill(0); let queueF = Array(256).fill(0); let queueB = Array(256).fill(0); let reversed = Array(256).fill(0); let u = 0; let v = 0; let index = 0; let headF = 0; let tailF = 1; let headB = 0; let tailB = 1; let endF = 0; let endB = 0; let meet = -1; let best = 1000000; let count = 0; for (u = 0; u < vertices; u++) { forward[u] = -1; backward[u] = -1; parent[u] = -1; next[u] = -1; } forward[0] = 0; backward[vertices - 1] = 0; queueF[0] = 0; queueB[0] = vertices - 1; while (headF < tailF && headB < tailB && meet < 0) { endF = tailF; endB = tailB; while (headF < endF) { u = queueF[headF++]; for (index = 0; index < edges; index++) { v = to[index]; if (from[index] == u && forward[v] < 0) { forward[v] = forward[u] + 1; parent[v] = u; queueF[tailF++] = v; } } } while (headB < endB) { u = queueB[headB++]; for (index = 0; index < edges; index++) { v = from[index]; if (to[index] == u && backward[v] < 0) { backward[v] = backward[u] + 1; next[v] = u; queueB[tailB++] = v; } } } for (u = 0; u < vertices; u++) { if (forward[u] >= 0 && backward[u] >= 0 && forward[u] + backward[u] < best) { meet = u; best = forward[u] + backward[u]; } } } if (meet < 0) { return -1; } u = meet; while (u >= 0) { reversed[count++] = u; u = parent[u]; } for (index = 0; index < count; index++) { output[index] = reversed[count - 1 - index]; } u = next[meet]; while (u >= 0) { output[count++] = u; u = next[u]; } return best; } |
Source
Early primary research on bidirectional and heuristic search. The playground uses complete BFS layers and directed-edge reversal.
- Bi-Directional and Heuristic Search in Path Problems, SLAC Report 104
Ira Pohl (1969). Research report.