📖 Scenario: You are working on a simple social network app. You want to represent friendships between people. You can use two ways: adjacency list or adjacency matrix. Each way has its own strengths depending on how many friendships there are.
🎯 Goal: Build two graph representations for the same small network: one using an adjacency list and one using an adjacency matrix. Then, learn when to choose each type based on the number of connections.
📋 What You'll Learn
Create an adjacency list for a graph with 4 people and their friendships
Create an adjacency matrix for the same graph
Add a variable to count total friendships
Write code to print both graph representations
💡 Why This Matters
🌍 Real World
Social networks, maps, and many apps use graphs to represent connections between things like people or places.
💼 Career
Understanding graph representations helps in roles like software development, data science, and network engineering where relationships between data points matter.
Progress0 / 4 steps
1
Create the adjacency list
Create a dictionary called adj_list representing friendships between 4 people: 'Alice', 'Bob', 'Charlie', and 'Diana'. Use these exact friendships: 'Alice' is friends with 'Bob' and 'Diana'; 'Bob' is friends with 'Alice' and 'Charlie'; 'Charlie' is friends with 'Bob'; 'Diana' is friends with 'Alice'.
DSA Python
Hint
Think of adj_list as a dictionary where each person has a list of their friends.
2
Create the adjacency matrix
Create a list of lists called adj_matrix representing the same friendships between 'Alice', 'Bob', 'Charlie', and 'Diana' in this order. Use 1 for friendship and 0 for no friendship. The matrix should be 4x4 where rows and columns represent people in the order: 'Alice', 'Bob', 'Charlie', 'Diana'.
DSA Python
Hint
Remember the order of people is 'Alice', 'Bob', 'Charlie', 'Diana'. Use 1 where there is a friendship, 0 otherwise.
3
Count total friendships
Create a variable called total_friendships and set it to 0. Then use a for loop with variable friends to iterate over the values of adj_list. For each friends list, add its length to total_friendships.
DSA Python
Hint
Use adj_list.values() to get all friends lists and add their lengths to total_friendships.
4
Print both graph representations and total friendships
Print the adj_list, then print the adj_matrix, and finally print total_friendships. Use three separate print statements exactly as: print(adj_list), print(adj_matrix), and print(total_friendships).
DSA Python
Hint
Use three print statements to show the adjacency list, adjacency matrix, and total friendships.
Practice
(1/5)
1. Which data structure is generally better for representing a graph with very few edges compared to the number of vertices?
easy
A. Adjacency List
B. Adjacency Matrix
C. Edge List
D. Incidence Matrix
Solution
Step 1: Understand graph density
A graph with very few edges compared to vertices is called a sparse graph.
Step 2: Choose efficient representation for sparse graphs
Adjacency lists store only existing edges, saving space for sparse graphs, unlike adjacency matrices which use space for all possible edges.
Final Answer:
Adjacency List -> Option A
Quick Check:
Sparse graph = Adjacency List [OK]
Hint: Sparse graphs use adjacency lists for space efficiency [OK]
Common Mistakes:
Choosing adjacency matrix for sparse graphs wastes space
Confusing edge list with adjacency matrix
Assuming adjacency matrix is always better
2. Which of the following is the correct way to initialize an adjacency matrix for a graph with 4 vertices in Python?
easy
A. matrix = [[0 for _ in range(4)] for _ in range(4)]
B. matrix = [[0]*4]*4
C. matrix = [0]*4
D. matrix = [[None]*4 for _ in range(4)]
Solution
Step 1: Understand matrix initialization
Using list comprehension with nested loops creates independent inner lists.
Step 2: Avoid shallow copies
matrix = [[0]*4]*4 creates shallow copies causing all rows to reference the same list, leading to bugs.
Final Answer:
matrix = [[0 for _ in range(4)] for _ in range(4)] -> Option A
Quick Check:
Independent rows = matrix = [[0 for _ in range(4)] for _ in range(4)] [OK]
Hint: Use nested list comprehension for independent rows [OK]
Common Mistakes:
Using [[0]*4]*4 causes all rows to be the same object
Initializing with single list for matrix
Using None instead of 0 for adjacency matrix
3. Consider the following Python code for a graph with 3 vertices using adjacency list representation:
D. [[0, 1, 0], [0, 1, 0], [0, 1, 0]]; Rows are shallow copies
Solution
Step 1: Understand matrix initialization
Using [[0]*3]*3 creates 3 references to the same inner list.
Step 2: Modify one element affects all rows
Setting matrix[0][1] = 1 changes the second element in all rows because they share the same list.
Final Answer:
[[0, 1, 0], [0, 1, 0], [0, 1, 0]]; Rows are shallow copies -> Option D
Quick Check:
Shallow copy rows cause all rows to change [OK]
Hint: Avoid [[0]*n]*n; use nested list comprehension [OK]
Common Mistakes:
Assuming only first row changes
Ignoring shallow copy behavior
Confusing IndexError with shallow copy bug
5. You have a graph with 1000 vertices and about 10,000 edges. You need to frequently check if an edge exists between two vertices. Which representation is best and why?
hard
A. Edge List, because it is simplest to implement
B. Adjacency Matrix, because it allows O(1) edge existence checks
C. Adjacency List, because it uses less memory for sparse graphs
D. Incidence Matrix, because it stores edges explicitly
Solution
Step 1: Analyze graph density and operations
Graph has 1000 vertices and 10,000 edges, which is moderately sparse but not very sparse.
Step 2: Consider operation speed for edge checks
Adjacency matrix allows checking edge existence in constant time O(1), while adjacency list requires searching neighbors.
Step 3: Balance memory and speed
Though adjacency matrix uses more memory, the speed benefit for frequent edge checks outweighs memory cost here.
Final Answer:
Adjacency Matrix, because it allows O(1) edge existence checks -> Option B
Quick Check:
Frequent edge check = Adjacency Matrix [OK]
Hint: For fast edge checks, adjacency matrix is best despite memory [OK]
Common Mistakes:
Choosing adjacency list only for memory without considering speed
Ignoring operation frequency in choice
Confusing edge list with adjacency matrix for fast checks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.