LESSON 28 / 75·2 min readIntermediate

Graphs in JavaScript

Should knowConcept lesson
01 Understand02 Recall03 Prepare

Graph representation

JavaScript
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

JavaScript
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);
      }
    }
  }
}

PAUSE & RECALL

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

CONNECT IT TO YOUR INTERVIEW

Turn this concept into a clear answer.

Practice this concept
YOUR NEXT STEP

Keep connecting the ideas.

Next lesson: Dynamic programming in JavaScript

Adapted from 16_DSA_JAVASCRIPT.md · “9. Graphs in JavaScript”. Examples are study fragments; framework behavior can depend on your version.