Graph Search Visualizer
SlowFast
(7)(9)
0
[0]
source
1
[1]
2
[2]
3
[3]
4
[4]
target
5
[5]
6
[6]
Source
Target
Path Found
Frontier
Visited
Other
Operation Logs
No operations yet
Algorithm Info
BFS Time Complexity: O(V + E)
DFS Time Complexity: O(V + E)
Space Complexity: O(V)
Best Case: O(1) - source == target
Worst Case: O(V + E)
How it works: BFS explores level by level; DFS explores as deep as possible before backtracking.
When to use: BFS for shortest path; DFS for reachability and traversal.
Implementation
function bfs(graph, source, target) {
const visited = new Set();
const queue = [source];
visited.add(source);
while (queue.length) {
const curr = queue.shift();
if (curr === target) return true;
for (const neighbor of graph[curr]) {
if (!visited.has(neighbor)) {
visited.add(neighbor);
queue.push(neighbor);
}
}
}
return false;
}