본문으로 건너뛰기
Chloe
chloe
글로그인
OverviewPostsPlayground

© 2026 Hyunjoo. Built with Next.js

← Playground 목록

그래프 순회 (BFS / DFS)

큐 기반 BFS와 스택 기반 DFS의 방문 순서를 같은 그래프 위에서 비교합니다.

graphbfsdfs
ABCDEFG
방문 순서아직 없음
큐 (front → back)
  • A
현재
방문
예정
BFS 시작 — A

소스 코드

BFS · JavaScript
1function bfs(graph, start) {
2 const visited = new Set();
3 const queue = [start];
4 while (queue.length > 0) {
5 const node = queue.shift();
6 if (visited.has(node)) continue;
7 visited.add(node);
8 for (const neighbor of graph[node]) {
9 if (!visited.has(neighbor)) {
10 queue.push(neighbor);
11 }
12 }
13 }
14 return [...visited];
15}

Step

1 / 16