Files never leave your device

Breadth-first search calculator

Run BFS and get the minimum hop count from a starting vertex. Your inputs stay on your device.

Set up your task

Ready. Processing stays in this browser.

How to use Breadth-first search calculator

Run BFS and get the minimum hop count from a starting vertex. The pre-filled problem gives you a worked starting point. Compare the response with your own working before trying the self-check below.

  1. Check the example inputs: Vertices (space-separated), Edges (from and to, one per line), Start vertex, Edges are.
  2. Change the inputs for your problem. Results update as you type, so finish entering the values before copying an answer.
  3. Use the working to check an intermediate value or assumption. Then change the example as suggested in the self-check and compare the result.

Supported inputs and limits

List up to 20 vertices and 80 unweighted edges. Include isolated vertices explicitly. Edge input order breaks traversal ties.

How it works

Breadth-first search explores an unweighted graph layer by layer. It starts with the source, visits all neighbours one hop away, then moves to two hops and so on. A first visit assigns the smallest possible hop distance because every earlier layer has already been explored.

A queue keeps the frontier in discovery order. Marking a vertex when it enters the queue stops repeated edges or cycles from adding it again. An isolated vertex remains part of the graph even when it is unreachable from the chosen source. Switching direction changes which edges can be followed.

Example

Run the pre-filled example. Its first result is β€œBreadth-first order: A β†’ B β†’ C β†’ D”.

Vertices (space-separated)
A B C D
Edges (from and to, one per line)
A B
A C
B D
Start vertex
A
Edges are
Undirected
Breadth-first order: A β†’ B β†’ C β†’ D
A: 0
B: 1
C: 1
D: 2

Questions about Breadth-first search calculator

How do I enter an empty edge list?

List the vertices and leave the edge box blank. Only the starting vertex is reachable.

Does the order of edges change the distances?

It can change the visit order among vertices at the same distance. The minimum hop counts stay the same for an unchanged graph.

Remove the edge B D in the example.

D stays in the result but becomes unreachable. The vertices A, B and C still have distances zero, one and one.

Related tools

Year 2: Algorithms and theory

Last updated