Medium Graphs

BFS Graph

Traverses a graph breadth-first starting from node 0, using an explicit FIFO queue: it visits the start node, then all of its direct neighbors, then their unvisited neighbors, expanding outward one ring at a time — the same building block used to find shortest paths in unweighted graphs. Receives the graph as a square adjacency matrix, where a nonzero value at row i, column j marks an edge between nodes i and j (0 means no edge). Returns the node indices in the order they were visited; nodes unreachable from node 0 are never visited.

Visualization

Input

Algorithm code

Custom input

Saved inputs

References