flowchart TD
A[Start Node enqueue] --> B[Queue pop left]
B --> C{Target found}
C -- Yes --> D[Return result]
C -- No --> E[Visit neighbors]
E --> F{Unvisited}
F -- Yes --> G[Mark visited and enqueue]
F -- No --> H[Skip]
G --> I{Queue empty}
H --> I
I -- No --> B
I -- Yes --> J[End not found]
sequenceDiagram
participant Q as Queue
participant V as Visited
participant G as Graph
Q->>Q: push start
loop while queue not empty
Q->>Q: pop
Q->>G: get neighbors
G-->>Q: neighbor list
Q->>V: check visited
V-->>Q: true false
Q->>Q: push unvisited
end
# [BFS 템플릿: 아키텍트 버전]# Use Case: 최단 거리, 최소 이동 횟수, 레벨별 탐색# Components: Queue (FIFO), Visited Set# Constraint: 큐에 넣을 때 방문 처리 필수 (중복 방지)defbfs(start_node,graph,target=None):# 1. 초기화 (Initialization Layer)# - 큐 생성, 시작점 삽입, 방문 처리queue=[start_node]visited={start_node}# 2. 메인 루프 (Process Loop)# - 큐가 빌 때까지 반복whilequeue:current=queue.pop(0)# 3. 비즈니스 로직 (Core Logic)# - (필요하다면) 여기서 정답 체크 혹은 데이터 가공iftargetandcurrent==target:returncurrent# 4. 확장 로직 (Expansion Layer)# - 연결된 노드 탐색 및 조건 필터링forneighboringraph[current]:ifneighbornotinvisited:visited.add(neighbor)queue.append(neighbor)returnNone# 탐색 실패