Provide a comprehensive example of BFS in Python:
- User
Provide a comprehensive example of BFS in Python:
Assistant
Here is an example implementation of breadth-first search (BFS) in Python:
pythonfrom collections import deque # Graph is represented as adjacency list graph = { 'A': ['B','C'], 'B': ['A','D','E'], 'C': ['A','F'], 'D': ['B'], 'E': ['B','F'], 'F': ['C','E'] } def bfs(graph, start): visited = [] queue = deque([start]) while queue: vertex = queue.popleft() if vertex not in visited: visited.append(vertex) neighbors = graph[vertex] for neighbor in neighbors: queue.append(neighbor) return visited print(bfs(graph, 'A')) # Prints ['A', 'B', 'C', 'D', 'E', 'F']
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.