← BACK TO KNOWLEDGE
KNOWLEDGE
Breadth-First Search: Layered Graph Traversal
— views— comments
Explore network graph topologies tier-by-tier with a FIFO queue to guarantee shortest path discovery in unweighted graphs.
INTERACTIVE ALGORITHM WORKBENCH
Breadth-First Search (BFS) Visualizer
MediumGraph & QueueTime O(V + E)Space O(V)
Graph Network Topology
BFS Execution State
FIFO Queue (Front → Back):
A ◀ Front
Visited Sequence:
A
Active Node—
Probed Neighbor—
Queue Size1
BFS StatusStarting
1def bfs(graph, start):▶2 visited = {start}3 queue = [start]4 order = []5 while queue:6 node = queue.pop(0)7 order.append(node)8 for neighbor in graph[node]:9 if neighbor not in visited:10 visited.add(neighbor)11 queue.append(neighbor)12 return orderWHY THIS STEP WORKS
Breadth-First Search discovers vertices layer by layer using a First-In, First-Out (FIFO) queue to ensure shortest hop distances.
- 1Mark start node "A" as visited
- 2Enqueue "A"
- 3Queue: [A]
Comments