📖 Scenario: You are helping a friend organize their social network connections. Each person is a node, and their friendships are edges. You will represent this network using an adjacency list.
🎯 Goal: Build an adjacency list to represent a small social network graph. Then, add a new person with their connections, and finally print the adjacency list.
📋 What You'll Learn
Create a dictionary called graph with exact keys and list values
Add a new key-value pair to graph representing a new person and their friends
Use the adjacency list format where each key is a node and the value is a list of connected nodes
Print the final graph dictionary
💡 Why This Matters
🌍 Real World
Adjacency lists are used to represent networks like social connections, road maps, or computer networks efficiently.
💼 Career
Understanding adjacency lists is essential for roles in software development, data analysis, and network engineering where graph data structures are common.
Progress0 / 4 steps
1
Create the initial adjacency list
Create a dictionary called graph with these exact entries: 'Alice': ['Bob', 'Charlie'], 'Bob': ['Alice'], 'Charlie': ['Alice']
DSA Python
Hint
Use curly braces {} to create a dictionary. Each key is a person's name, and the value is a list of their friends.
2
Add a new person with connections
Add a new key 'David' to the graph dictionary with the value ['Alice', 'Charlie']
DSA Python
Hint
Use graph['David'] = ['Alice', 'Charlie'] to add a new entry to the dictionary.
3
Update connections for existing nodes
Add 'David' to the friend lists of 'Alice' and 'Charlie' in the graph dictionary
DSA Python
Hint
Use the append method to add 'David' to the lists of 'Alice' and 'Charlie'.
4
Print the final adjacency list
Print the graph dictionary to show the final adjacency list
DSA Python
Hint
Use print(graph) to display the adjacency list.
Practice
(1/5)
1. What does an adjacency list represent in graph data structures?
easy
A. A list of all nodes without connections
B. A matrix showing distances between nodes
C. A list of nodes sorted by their values
D. A list where each node stores its connected neighbors
Solution
Step 1: Understand adjacency list concept
An adjacency list stores each node with a list of nodes it connects to directly.
Step 2: Compare options with definition
Only A list where each node stores its connected neighbors correctly describes this structure as neighbors stored per node.
Final Answer:
A list where each node stores its connected neighbors -> Option D
Quick Check:
Adjacency list = neighbors per node [OK]
Hint: Adjacency list = nodes with their direct neighbors [OK]
Common Mistakes:
Confusing adjacency list with adjacency matrix
Thinking it stores distances instead of neighbors
Assuming it lists nodes without connections
2. Which of the following is the correct Python syntax to initialize an empty adjacency list for a graph with 3 nodes?
easy
A. graph = {0: [], 1: [], 2: []}
B. graph = [[], [], []]
C. graph = {0: None, 1: None, 2: None}
D. graph = [0, 1, 2]
Solution
Step 1: Understand adjacency list initialization
An adjacency list is usually a dictionary with keys as nodes and values as lists of neighbors.
Step 2: Check each option
graph = {0: [], 1: [], 2: []} correctly creates a dict with empty lists for each node. graph = [[], [], []] is a list of lists but lacks node keys. graph = {0: None, 1: None, 2: None} uses None instead of lists. graph = [0, 1, 2] is just a list of nodes, not adjacency lists.
Final Answer:
graph = {0: [], 1: [], 2: []} -> Option A
Quick Check:
Dict with empty lists = adjacency list init [OK]
Hint: Use dict with nodes as keys and empty lists as values [OK]