Graph questions look different on the surface but often reduce to one decision:

  • BFS when you need minimum number of edges
  • DFS when you need structure checks, components, or exhaustive traversal
  • Topological sort when edges represent prerequisites

Build a graph first

Most inputs can be converted into adjacency lists.

export function buildAdjList(n: number, edges: Array<[number, number]>): number[][] {
  const graph = Array.from({ length: n }, () => [] as number[]);
  for (const [u, v] of edges) {
    graph[u]!.push(v);
  }
  return graph;
}

For undirected graphs, add both directions.

BFS template (shortest path in unweighted graph)

export function shortestPathUnweighted(
  n: number,
  edges: Array<[number, number]>,
  start: number,
  target: number,
): number {
  const graph = Array.from({ length: n }, () => [] as number[]);
  for (const [u, v] of edges) {
    graph[u]!.push(v);
    graph[v]!.push(u);
  }

  const dist = Array.from({ length: n }, () => -1);
  const queue: number[] = [start];
  dist[start] = 0;

  for (let head = 0; head < queue.length; head++) {
    const node = queue[head]!;
    if (node === target) return dist[node]!;
    for (const next of graph[node]!) {
      if (dist[next] !== -1) continue;
      dist[next] = dist[node]! + 1;
      queue.push(next);
    }
  }

  return -1;
}

DFS template (components and reachability)

export function countComponents(n: number, edges: Array<[number, number]>): number {
  const graph = Array.from({ length: n }, () => [] as number[]);
  for (const [u, v] of edges) {
    graph[u]!.push(v);
    graph[v]!.push(u);
  }

  const visited = Array.from({ length: n }, () => false);
  let components = 0;

  const dfs = (node: number) => {
    visited[node] = true;
    for (const next of graph[node]!) {
      if (!visited[next]) dfs(next);
    }
  };

  for (let i = 0; i < n; i++) {
    if (visited[i]) continue;
    components++;
    dfs(i);
  }

  return components;
}

Topological sort (Kahn's algorithm)

Use when graph is directed acyclic and represents dependencies.

export function topoSort(n: number, edges: Array<[number, number]>): number[] {
  const graph = Array.from({ length: n }, () => [] as number[]);
  const indegree = Array.from({ length: n }, () => 0);

  for (const [u, v] of edges) {
    graph[u]!.push(v);
    indegree[v]++;
  }

  const queue: number[] = [];
  for (let i = 0; i < n; i++) {
    if (indegree[i] === 0) queue.push(i);
  }

  const order: number[] = [];
  for (let head = 0; head < queue.length; head++) {
    const node = queue[head]!;
    order.push(node);
    for (const next of graph[node]!) {
      indegree[next]--;
      if (indegree[next] === 0) queue.push(next);
    }
  }

  return order.length === n ? order : [];
}

If result length is less than n, a cycle exists.

Directed cycle detection with DFS colors

Track states:

  • 0 unvisited
  • 1 visiting (in current recursion stack)
  • 2 done

Back edge to a visiting node means cycle.

Interview decision map

  • Shortest edges count in unweighted graph -> BFS
  • Connectivity or component count -> DFS/BFS
  • Prerequisite ordering -> topological sort
  • Directed cycle check -> DFS color or Kahn length check

Common mistakes

  • Marking visited too late in BFS (causes duplicates)
  • Forgetting to reverse edges or add both directions properly
  • Using recursion DFS on very deep graphs without considering stack limits
  • Confusing tree traversal rules with general graph traversal

Graph interviews are mostly pattern recognition. If you identify graph type and traversal goal early, implementation is usually straightforward.