0
0
DynamoDBquery~10 mins

Adjacency list pattern in DynamoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Adjacency list pattern
Start with a node
Store node with unique ID
Store references (IDs) of connected nodes
To find neighbors, query by stored references
Repeat for each node
Traverse graph by following references
Each node stores IDs of its connected nodes. To find neighbors, we look up these IDs. This builds a graph using references.
Execution Sample
DynamoDB
Node1 = {"id": "1", "name": "A", "neighbors": ["2", "3"]}
Node2 = {"id": "2", "name": "B", "neighbors": ["1"]}
Node3 = {"id": "3", "name": "C", "neighbors": ["1"]}
Three nodes with IDs and neighbor lists showing connections.
Execution Table
StepActionNode IDNeighbors StoredQuery ResultNotes
1Create node1["2", "3"]NoneNode A created with neighbors 2 and 3
2Create node2["1"]NoneNode B created with neighbor 1
3Create node3["1"]NoneNode C created with neighbor 1
4Query neighbors of node 11["2", "3"][Node 2, Node 3]Lookup neighbors by IDs
5Query neighbors of node 22["1"][Node 1]Lookup neighbor by ID
6Query neighbors of node 33["1"][Node 1]Lookup neighbor by ID
7Traversal example1["2", "3"][Node 2, Node 3]Traverse from node 1 to neighbors
8Traversal example2["1"][Node 1]Traverse from node 2 back to node 1
9Traversal example3["1"][Node 1]Traverse from node 3 back to node 1
10EndAll nodes created and neighbors queried
💡 All nodes created and neighbor queries completed, traversal demonstrated
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Node1None{"id": "1", "name": "A", "neighbors": ["2", "3"]}{"id": "1", "name": "A", "neighbors": ["2", "3"]}{"id": "1", "name": "A", "neighbors": ["2", "3"]}{"id": "1", "name": "A", "neighbors": ["2", "3"]}
Node2NoneNone{"id": "2", "name": "B", "neighbors": ["1"]}{"id": "2", "name": "B", "neighbors": ["1"]}{"id": "2", "name": "B", "neighbors": ["1"]}
Node3NoneNoneNone{"id": "3", "name": "C", "neighbors": ["1"]}{"id": "3", "name": "C", "neighbors": ["1"]}
QueryResultNoneNoneNoneNone[Node 2, Node 3] after querying neighbors of Node 1
Key Moments - 3 Insights
Why do we store neighbor IDs instead of full neighbor data inside each node?
Storing only neighbor IDs avoids data duplication and keeps nodes small. We fetch full neighbor data by querying these IDs as shown in execution_table rows 4-6.
How do we find neighbors of a node?
We look at the 'neighbors' list of IDs in the node, then query the database for those IDs. This is shown in execution_table rows 4-6.
What happens if a node has no neighbors?
Its 'neighbors' list is empty. Querying neighbors returns an empty list, meaning no connected nodes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What nodes are neighbors of node 1?
ANodes 2 and 3
BNode 1 only
CNo neighbors
DNodes 1, 2, and 3
💡 Hint
Check the 'Query Result' column at step 4 in execution_table
At which step do we create node 3 with its neighbors?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' and 'Node ID' columns in execution_table
If node 2 had no neighbors, how would the 'Neighbors Stored' column change at step 2?
A["1"]
B[]
C["2"]
Dnull
💡 Hint
Neighbors list is empty if no neighbors, see variable_tracker for neighbors format
Concept Snapshot
Adjacency list pattern stores each node with a unique ID and a list of neighbor IDs.
To find neighbors, query nodes by these IDs.
This avoids data duplication and models graph connections clearly.
Common in DynamoDB for graph-like data.
Traversal follows neighbor references step-by-step.
Full Transcript
The adjacency list pattern stores graph nodes with unique IDs and lists of neighbor IDs. Each node references connected nodes by their IDs, not by embedding full data. This keeps data small and avoids duplication. To find neighbors, we query the database for nodes matching the stored neighbor IDs. The execution table shows creating three nodes and querying their neighbors. Traversal follows these references to move through the graph. Key moments clarify why IDs are stored, how neighbors are found, and what happens if no neighbors exist. The visual quiz tests understanding of neighbor queries and node creation steps. This pattern is common in DynamoDB for representing graphs efficiently.