1function bfs(graph, start) {
2 const visited = new Set();
3 const queue = [start];
4 visited.add(start);
5 while (queue.length > 0) {
6 const node = queue.shift();
7 for (const neighbor of graph[node]) {
8 if (!visited.has(neighbor)) {
9 visited.add(neighbor);
10 queue.push(neighbor);
11 }
12 }
13 }
14}
15
16function dfs(graph, node, visited = new Set()) {
17 visited.add(node);
18 for (const neighbor of graph[node]) {
19 if (!visited.has(neighbor)) {
20 dfs(graph, neighbor, visited);
21 }
22 }
23}