Concepts

Graph adjacency list

Try the free JavaScript compiler

Adjacency-list representation stores neighbor lists per vertex. For undirected edges store both directions; define duplicates, self-loops and valid endpoint contracts.

Formal Syntax & Structure

javascript syntaxjavascript
const adj = Array.from({length: vertexCount}, () => []); adj[u].push(v);

Working Example

Example Snippetjavascript
const adj=Array.from({length:4},()=>[]);for(const [u,v] of [[0,1],[1,2]])adj[u].push(v);console.log(JSON.stringify(adj));
JavaScript Reference