Graph as Adjacency List
An adjacency list stores a graph as a map from each node to the nodes it points at. In JavaScript that is usually a Map of arrays, or a plain object when ids are strings.
Detailed Explanation
A graph is nodes plus edges. The adjacency-list representation answers "who are the neighbours of u?" in time proportional to that node's degree. That is the representation BFS, DFS, Dijkstra, and topological sort want.
For a directed graph, adj.get(u) is the list of v such that u → v. For an undirected graph you store both u → v and v → u. Weighted edges store pairs: adj.get(u) is [{ to, weight }, ...].
A Map is the default when node ids can be numbers, strings, or objects. An object ({ a: ["b", "c"] }) is fine when ids are safe strings. A 2D matrix (n by n booleans) is the adjacency matrix: O(1) edge test, O(n^2) space. Use a matrix when the graph is dense or n is tiny. Use a list when the graph is sparse, which is most application graphs (pages, users, packages).
Building from an edge list is one loop:
for (const [u, v] of edges) {
if (!adj.has(u)) adj.set(u, []);
adj.get(u).push(v);
}
Remember to insert nodes that have no outgoing edges, or BFS will never see them as keys. A separate nodes set is the usual fix.
Multigraphs (two edges from u to v) are allowed if you push twice. Self-loops are a neighbour equal to u. Cycles are walks that repeat a node; they are normal in graphs and fatal in naive recursion without a visited set.
What it costs you: neighbour arrays you must not mutate while iterating unless you meant to. Also, object-as-graph inherits all object-as-map bugs. Prefer Map. JSON of a Map needs a conversion; people often keep the original edge list as the source of truth and rebuild the adjacency list.
Reach for an adjacency list for almost every graph algorithm in JavaScript. Reach for an edge list when you store or transmit the graph. Reach for a matrix when you multiply graphs or when n is small and you test "is (u, v) an edge?" constantly.
Code Examples
function fromEdges(edges, directed = true) {
const adj = new Map();
function addNode(n) {
if (!adj.has(n)) adj.set(n, []);
}
for (const [u, v] of edges) {
addNode(u);
addNode(v);
adj.get(u).push(v);
if (!directed) adj.get(v).push(u);
}
return adj;
}
const g = fromEdges([
["A", "B"],
["A", "C"],
["B", "D"],
]);
console.log(g.get("A")); // => [ 'B', 'C' ]
console.log(g.get("D")); // => []const weighted = new Map([
["home", [{ to: "work", weight: 12 }, { to: "gym", weight: 8 }]],
["work", []],
["gym", []],
]);
console.log(weighted.get("home")[0].to); // => workconst matrix = [
[0, 1, 1],
[0, 0, 1],
[0, 0, 0],
];
console.log(matrix[0][2]); // => 1 (edge 0 → 2)Common Gotchas
Isolated nodes disappear if you only insert edge endpoints that have outgoing lists
add both ends, or keep a `Set` of all ids.
Undirected graphs need both directions
forgetting the reverse edge makes BFS look like the graph is directed.
Recursing on neighbours without `visited` loops forever on a cycle
every walk needs a seen set (or a recursion stack set for cycle detection).
Object keys stringify
a graph keyed `{ 1: [2] }` and `{ "1": [2] }` is the same object shape. Use a `Map` for numeric ids if you also have object nodes.