0
0
DSA Typescriptprogramming~10 mins

Adjacency List Representation in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Adjacency List Representation
Start
Create empty adjacency list
For each edge
Add neighbor to source node list
Add neighbor to destination node list (if undirected)
Repeat for all edges
Adjacency list ready
Build an adjacency list by adding neighbors for each node from the edges, storing connections efficiently.
Execution Sample
DSA Typescript
const graph = new Map<number, number[]>();
graph.set(1, [2, 3]);
graph.set(2, [1, 4]);
graph.set(3, [1]);
graph.set(4, [2]);
Create an adjacency list for a graph with 4 nodes and edges between them.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Initialize empty Map{}None{}
2Add edge 1-2{1: [], 2: []}Add 2 to 1's list, 1 to 2's list{1: [2], 2: [1]}
3Add edge 1-3{1: [2], 2: [1], 3: []}Add 3 to 1's list, 1 to 3's list{1: [2, 3], 2: [1], 3: [1]}
4Add edge 2-4{1: [2, 3], 2: [1], 3: [1], 4: []}Add 4 to 2's list, 2 to 4's list{1: [2, 3], 2: [1, 4], 3: [1], 4: [2]}
5All edges processed{1: [2, 3], 2: [1, 4], 3: [1], 4: [2]}No changes{1: [2, 3], 2: [1, 4], 3: [1], 4: [2]}
💡 All edges added, adjacency list fully built
Variable Tracker
VariableStartAfter 1After 2After 3Final
graph{}{1: [2], 2: [1]}{1: [2, 3], 2: [1], 3: [1]}{1: [2, 3], 2: [1, 4], 3: [1], 4: [2]}{1: [2, 3], 2: [1, 4], 3: [1], 4: [2]}
Key Moments - 3 Insights
Why do we add neighbors to both nodes in an undirected graph?
Because edges connect two nodes both ways, so each node's list must include the other. See steps 2-4 in execution_table where both nodes update their lists.
What happens if a node has no edges?
It will have an empty list or no entry in the adjacency list. In this example, all nodes have edges, but an isolated node would just have an empty array or be missing.
Why use a Map instead of an array for adjacency list?
A Map allows flexible node keys (like numbers or strings) and easy insertion. Arrays require numeric continuous indices. This is shown in variable_tracker where graph is a Map.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what neighbors does node 1 have?
A[2]
B[2, 3]
C[3]
D[1, 3]
💡 Hint
Check the 'Visual State' column at step 3 in execution_table.
At which step does node 4 first appear in the adjacency list?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Nodes in List' column to see when node 4 is added.
If the graph was directed and edges only added from source to destination, how would node 2's list change after step 2?
A[]
B[4]
C[1]
D[1, 4]
💡 Hint
In a directed graph, only source node's list updates. Check pointer changes in execution_table step 2.
Concept Snapshot
Adjacency List Representation:
- Use a Map or array to store nodes and their neighbors.
- For each edge, add destination to source's list.
- For undirected graphs, add source to destination's list too.
- Efficient for sparse graphs.
- Allows quick neighbor lookup.
Full Transcript
This visualization shows how to build an adjacency list for a graph. We start with an empty Map. For each edge, we add the connected nodes to each other's neighbor lists if the graph is undirected. The execution table tracks each step, showing how the adjacency list grows. The variable tracker shows the graph Map's state after each edge is added. Key moments clarify why neighbors are added both ways, what happens with isolated nodes, and why a Map is used. The quiz tests understanding of the adjacency list state at different steps and the difference if the graph was directed.