Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Graph representations (adjacency matrix vs list)
📖 Scenario: You are helping a friend understand how to represent a simple network of cities connected by roads. Each city is a point, and roads connect pairs of cities. You will create two common ways to show these connections: an adjacency matrix and an adjacency list.
🎯 Goal: Build two data structures for the same graph: one using an adjacency matrix and one using an adjacency list. This will help you see how the same information can be stored differently.
📋 What You'll Learn
Create a graph with exactly 4 cities named 'A', 'B', 'C', and 'D'.
Represent the connections between cities as given: A connected to B and C, B connected to C, and C connected to D.
Create an adjacency matrix showing these connections with 1 for connected and 0 for not connected.
Create an adjacency list showing the same connections with each city listing its connected neighbors.
💡 Why This Matters
🌍 Real World
Graph representations are used in maps, social networks, and computer networks to show how points connect.
💼 Career
Understanding graph data structures is important for software developers, data scientists, and network engineers to model and analyze relationships.
Progress0 / 4 steps
1
Create the list of cities
Create a list called cities with the exact city names: 'A', 'B', 'C', and 'D'.
Data Structures Theory
Hint
Use a Python list with the city names as strings inside square brackets.
2
Set up the adjacency matrix
Create a variable called adjacency_matrix that is a 4x4 list of lists filled with zeros. This will hold connection info between cities.
Data Structures Theory
Hint
Use a nested list comprehension to create a 4 by 4 matrix filled with zeros.
3
Fill the adjacency matrix with connections
Update adjacency_matrix to show these connections: A connected to B and C, B connected to C, and C connected to D. Use 1 for connected pairs. Use the city indexes in cities to set the right matrix cells.
Data Structures Theory
Hint
Use the indexes of cities in the list to mark connections with 1 in the matrix.
4
Create the adjacency list
Create a dictionary called adjacency_list where each city key maps to a list of connected cities as strings. Use the same connections: A connected to B and C, B connected to C, and C connected to D.
Data Structures Theory
Hint
Use a dictionary with city names as keys and lists of connected city names as values.
Practice
(1/5)
1. Which graph representation uses a 2D grid to show connections between nodes?
easy
A. Incidence matrix
B. Adjacency matrix
C. Edge list
D. Adjacency list
Solution
Step 1: Understand adjacency matrix structure
An adjacency matrix is a 2D grid where rows and columns represent nodes, and cells show if an edge exists.
Step 2: Compare with other representations
Adjacency lists store neighbors in lists, not grids. Edge lists and incidence matrices differ in format.
Final Answer:
Adjacency matrix -> Option B
Quick Check:
2D grid = adjacency matrix [OK]
Hint: Matrix means grid; list means neighbors [OK]
Common Mistakes:
Confusing adjacency list with matrix
Thinking edge list is a grid
Mixing incidence matrix with adjacency matrix
2. Which of the following is the correct way to represent an adjacency list in Python?
easy
A. graph = [[1, 2], 0, [0, 1]]
B. graph = [[0,1,0],[1,0,1],[0,1,0]]
C. graph = [(0,1), (1,2), (2,0)]
D. graph = {0: [1, 2], 1: [0], 2: [0, 1]}
Solution
Step 1: Identify adjacency list format
An adjacency list maps each node to a list of its neighbors, often using a dictionary in Python.
Step 2: Check each option
graph = {0: [1, 2], 1: [0], 2: [0, 1]} uses a dictionary with keys as nodes and values as neighbor lists, which is correct. graph = [[0,1,0],[1,0,1],[0,1,0]] is a matrix, C is an edge list, D incorrectly uses an integer 0 for node 1 instead of a list.