Graphs in JavaScript
Graph representation
const graph = {
A: ['B', 'C'],
B: ['D'],
C: ['E'],
};Problems to practice
- Number of Islands
- BFS/DFS traversal
- Course Schedule
- Clone Graph
- Shortest Path in Unweighted Graph
- Topological Sort
Example: BFS
function bfs(graph, start) {
const visited = new Set([start]);
const queue = [start];
while (queue.length) {
const node = queue.shift();
console.log(node);
for (const neighbor of graph[node] || []) {
if (!visited.has(neighbor)) {
visited.add(neighbor);
queue.push(neighbor);
}
}
}
}Can you explain it without the notes?
Graphs in JavaScript
Use your own words. Explain an example and one limitation before checking the explanation.
Check your explanation
Graph representation
Turn this concept into a clear answer.
Practice this conceptAdapted from 16_DSA_JAVASCRIPT.md · “9. Graphs in JavaScript”. Examples are study fragments; framework behavior can depend on your version.