class Graph { adjacencyList: Map<number, number[]> = new Map(); addVertex(v: number) { if (!this.adjacencyList.has(v)) { this.adjacencyList.set(v, []); } } addEdge(v: number, w: number) { this.adjacencyList.get(v)?.push(w); this.adjacencyList.get(w)?.push(v); } printGraph() { this.adjacencyList.forEach((neighbors, vertex) => { console.log(`${vertex} -> ${neighbors.join(' -> ')} -> null`); }); } } const g = new Graph(); g.addVertex(1); g.addVertex(2); g.addVertex(3); g.addEdge(1, 2); g.addEdge(1, 3); g.printGraph();
The graph has vertices 1, 2, and 3. Edges connect 1 with 2 and 3. So, vertex 1's neighbors are 2 and 3. Vertices 2 and 3 each have 1 as their neighbor.
class Graph { adjacencyList: Map<number, number[]> = new Map(); addVertex(v: number) { if (!this.adjacencyList.has(v)) { this.adjacencyList.set(v, []); } } addEdge(v: number, w: number) { this.adjacencyList.get(v)?.push(w); this.adjacencyList.get(w)?.push(v); } removeVertex(v: number) { this.adjacencyList.delete(v); this.adjacencyList.forEach((neighbors) => { const index = neighbors.indexOf(v); if (index !== -1) { neighbors.splice(index, 1); } }); } printGraph() { this.adjacencyList.forEach((neighbors, vertex) => { console.log(`${vertex} -> ${neighbors.join(' -> ')} -> null`); }); } } const g = new Graph(); g.addVertex(1); g.addVertex(2); g.addVertex(3); g.addEdge(1, 2); g.addEdge(2, 3); g.addEdge(1, 3); g.removeVertex(2); g.printGraph();
Vertex 2 is removed. So, it no longer appears as a vertex or neighbor. Vertices 1 and 3 remain connected.
class Graph { adjacencyList: Map<number, number[]> = new Map(); addVertex(v: number) { if (!this.adjacencyList.has(v)) { this.adjacencyList.set(v, []); } } addEdge(v: number, w: number) { this.adjacencyList.get(v).push(w); this.adjacencyList.get(w).push(v); } } const g = new Graph(); g.addVertex(1); g.addEdge(1, 2);
Vertex 2 was never added, so adjacencyList.get(2) returns undefined. Calling push on undefined causes a TypeError.
Adjacency lists keep only neighbors for each vertex, so they use memory proportional to edges. Adjacency matrices allocate space for all vertex pairs, even if no edge exists.
class Graph { adjacencyList: Map<number, number[]> = new Map(); addVertex(v: number) { if (!this.adjacencyList.has(v)) { this.adjacencyList.set(v, []); } } addEdge(v: number, w: number) { this.adjacencyList.get(v)?.push(w); this.adjacencyList.get(w)?.push(v); } removeEdge(v: number, w: number) { this.adjacencyList.set(v, this.adjacencyList.get(v)?.filter(x => x !== w) || []); this.adjacencyList.set(w, this.adjacencyList.get(w)?.filter(x => x !== v) || []); } printGraph() { this.adjacencyList.forEach((neighbors, vertex) => { console.log(`${vertex} -> ${neighbors.join(' -> ')} -> null`); }); } } const g = new Graph(); g.addVertex(1); g.addVertex(2); g.addVertex(3); g.addVertex(4); g.addEdge(1, 2); g.addEdge(1, 3); g.addEdge(2, 4); g.addEdge(3, 4); g.removeEdge(1, 3); g.printGraph();
Edges added: (1,2), (1,3), (2,4), (3,4). Then edge (1,3) is removed. So 1's neighbors are only 2, 3's neighbors no longer include 1.
