Graphs / Beginner
Breadth-First Search Visualizer
Visit the start, then all its neighbors, then their neighbors. A queue keeps nodes in the order they were found.
Start the Breadth-First Search simulation → Step forward and back, change the input, follow the code line by line in Java, Python, C++, C or JavaScript. Free, no sign-up.
Time and space complexity
| Best time | O(V+E) |
|---|---|
| Average time | O(V+E) |
| Worst time | O(V+E) |
| Extra space | O(V) |
Worked example, step by step
The same steps the simulator shows, on a small input.
- Start at A. Mark it seen and put it in the queue.
- Take A from the front of the queue.
- Visit A.
- B is new. Mark it seen and add it to the back of the queue.
- C is new. Mark it seen and add it to the back of the queue.
- Take B from the front of the queue.
- Visit B.
- A was already seen. Skip it.
- C was already seen. Skip it.
- D is new. Mark it seen and add it to the back of the queue.
- Take C from the front of the queue.
- Visit C.
- A was already seen. Skip it.
- B was already seen. Skip it.
- D was already seen. Skip it.
- E is new. Mark it seen and add it to the back of the queue.
- Take D from the front of the queue.
- Visit D.
- B was already seen. Skip it.
- C was already seen. Skip it.
- … 8 more steps …
- Visit F.
- D was already seen. Skip it.
- E was already seen. Skip it.
- Done. Visit order: A, B, C, D, E, F.
Breadth-first search in Java, Python, C++, C and JavaScript
void bfs(int start) {
Queue<Integer> q = new ArrayDeque<>();
boolean[] seen = new boolean[n];
q.add(start); seen[start] = true;
while (!q.isEmpty()) {
int u = q.poll();
visit(u);
for (int v : adj[u]) {
if (!seen[v]) {
seen[v] = true;
q.add(v);
}
}
}
}
from collections import deque
def bfs(adj, start):
q = deque()
seen = [False] * len(adj)
q.append(start); seen[start] = True
while q:
u = q.popleft()
visit(u)
for v in adj[u]:
if not seen[v]:
seen[v] = True
q.append(v)
void bfs(const vector<vector<int>>& adj, int start) {
queue<int> q;
vector<bool> seen(adj.size(), false);
q.push(start); seen[start] = true;
while (!q.empty()) {
int u = q.front(); q.pop();
visit(u);
for (int v : adj[u]) {
if (!seen[v]) {
seen[v] = true;
q.push(v);
}
}
}
}
#define MAXN 100
int n, adj[MAXN][MAXN], deg[MAXN]; /* adj[u][0..deg[u]-1] are u's neighbours */
void bfs(int start) {
int q[MAXN], head = 0, tail = 0;
int seen[MAXN] = {0};
q[tail++] = start; seen[start] = 1;
while (head < tail) {
int u = q[head++];
visit(u);
for (int k = 0; k < deg[u]; k++) {
int v = adj[u][k];
if (!seen[v]) {
seen[v] = 1;
q[tail++] = v;
}
}
}
}
function bfs(adj, start) {
const q = [];
const seen = new Array(adj.length).fill(false);
q.push(start); seen[start] = true;
let head = 0;
while (head < q.length) {
const u = q[head++];
visit(u);
for (const v of adj[u]) {
if (!seen[v]) {
seen[v] = true;
q.push(v);
}
}
}
}In an interview
BFS finds the shortest path when every edge costs the same. Think "fewest moves" problems.
More graphs
- Depth-First Search visualizer
- Dijkstra's Shortest Path visualizer
- Bellman-Ford Shortest Path visualizer
- Floyd-Warshall All-Pairs Shortest Paths visualizer
- Topological Sort (Kahn) visualizer
- Detect a Cycle in a Directed Graph visualizer
- Union-Find (Disjoint Sets) visualizer
- Kruskal's Minimum Spanning Tree visualizer