BFS
Breadth-first search explores a graph in layers from a chosen source vertex.
Input
An unweighted graph and a source vertex.
Output
The distance in edge count from the source and the BFS predecessor tree.
Behavior
It uses a queue, discovering all vertices at distance k before any at distance k + 1.
Best Use
Use it for shortest paths in unweighted graphs or level-by-level reachability.
Test Example
Shortest Hops In A Social Graph
Starting at s, an exam may ask for each vertex's shortest unweighted distance and BFS parent.
The important move is to group vertices by discovery level. Every edge from the current frontier is checked before moving to the next distance.
BFS discovers s, then {a, b}, then {c, d}; the level number is the shortest number of edges.
How to use this visualizer
- Which graph algorithms can I visualize?
- Use BFS or DFS for traversal, Kruskal or Prim for minimum spanning trees, Bellman–Ford or Dijkstra for single-source shortest paths, Floyd–Warshall for all-pairs shortest paths, and Ford–Fulkerson or Push–Relabel for maximum flow.
- How do I enter a graph?
- Write one vertex per line, followed by its neighbors. Add a weight as neighbor:weight, for example B:4. For Floyd–Warshall, use a square weight matrix and enter ∞ for missing edges. You can also start from a built-in example.
- What can I see at each step?
- Start the algorithm to highlight the current line of pseudocode and inspect the graph and algorithm state. The workspace shows the relevant distances, predecessors, queues, stacks, matrices, or flows as you step forward and backward.