📖 Scenario: Imagine you are organizing a small social network of friends. You want to keep track of who is friends with whom. You can store this information in two ways: using an adjacency list or an adjacency matrix. Each way has its own advantages depending on how many friends each person has.
🎯 Goal: Build two simple graph representations for the same set of friends: one using an adjacency list and one using an adjacency matrix. Learn when to use each method by comparing their structures.
📋 What You'll Learn
Create an adjacency list for a graph with 4 friends and their connections
Create an adjacency matrix for the same graph
Use a variable to store the number of friends
Print both the adjacency list and adjacency matrix to compare
💡 Why This Matters
🌍 Real World
Social networks, maps, and computer networks use graphs to represent connections between people, places, or devices.
💼 Career
Understanding graph representations helps in software development, especially in areas like networking, recommendation systems, and data analysis.
Progress0 / 4 steps
1
Create the adjacency list for the graph
Create a variable called adj_list as a Vec> representing the friends and their connections exactly as: friend 0 connected to 1 and 2, friend 1 connected to 0 and 3, friend 2 connected to 0, friend 3 connected to 1.
DSA Rust
Hint
Use a vector of vectors where each inner vector lists the friends connected to that friend.
2
Create a variable for the number of friends
Add a variable called num_friends of type usize and set it to 4 to represent the total number of friends.
DSA Rust
Hint
Use let num_friends: usize = 4; to store the number of friends.
3
Create the adjacency matrix for the graph
Create a variable called adj_matrix as a Vec> representing the connections between friends using a 4x4 matrix. Use 1 for connected and 0 for not connected. The matrix rows and columns represent friends 0 to 3 in order, with connections matching the adjacency list.
DSA Rust
Hint
Use a 4x4 vector of vectors with 1s and 0s to show connections between friends.
4
Print both adjacency list and matrix
Print the adj_list and adj_matrix variables using println! to show their contents for comparison.
DSA Rust
Hint
Use println!("Adjacency List: {:?}", adj_list); and a for loop to print each row of adj_matrix.
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 sparse.
Step 2: Choose data structure for sparse graphs
Adjacency List stores only existing edges, saving space for sparse graphs.
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 Rust syntax to declare an adjacency matrix using a 2D vector for a graph with 3 vertices initialized to zero?
easy
A. let matrix = vec![vec![0; 2]; 3];
B. let matrix = [[0; 3]; 3];
C. let matrix = vec![0; 3];
D. let matrix = vec![vec![0; 3]; 3];
Solution
Step 1: Understand adjacency matrix structure
It is a 2D structure with size n x n, here 3 x 3.
Step 2: Check Rust syntax for 2D vector initialization
vec![vec![0; 3]; 3] creates a 3x3 matrix filled with zeros.
Final Answer:
let matrix = vec![vec![0; 3]; 3]; -> Option D
Quick Check:
3x3 zero matrix = vec![vec![0; 3]; 3] [OK]
Hint: Use vec![vec![value; cols]; rows] for 2D vectors [OK]
Common Mistakes:
Using single vector for 2D matrix
Wrong inner vector size
Using fixed-size array without mutability
3. Given the following Rust code for an adjacency list, what will be printed?
B. Index 3 is out of bounds; change to matrix[1][2] = 1;
C. Change vec![0; 3] to vec![0; 4];
D. No error; code runs fine
Solution
Step 1: Check matrix size and indices
Matrix is 3x3, valid indices are 0,1,2 for both rows and columns.
Step 2: Identify invalid index usage
matrix[1][3] accesses column 3 which is out of bounds.
Step 3: Fix the index
Change matrix[1][3] to matrix[1][2] to stay within bounds.
Final Answer:
Index 3 is out of bounds; change to matrix[1][2] = 1; -> Option B
Quick Check:
3x3 matrix max index = 2 [OK]
Hint: Indices start at 0; max index = size-1 [OK]
Common Mistakes:
Using index equal to size (out of bounds)
Assuming 1-based indexing
Ignoring Rust's strict bounds checking
5. You need to represent a dense graph with 1000 vertices where you frequently check if an edge exists between any two vertices. Which representation is best and why?
hard
A. Edge List, because it is fastest for dense graphs
B. Adjacency List, because it uses less memory for dense graphs
C. Adjacency Matrix, because it allows O(1) edge existence checks
D. Adjacency Matrix, because it uses less memory for sparse graphs
Solution
Step 1: Analyze graph density and operations
Dense graph means many edges, close to maximum possible.
Step 2: Choose data structure for fast edge checks
Adjacency matrix allows O(1) time to check if edge exists by direct index access.
Step 3: Consider memory tradeoff
Though adjacency matrix uses more memory, for dense graphs this is acceptable and efficient.
Final Answer:
Adjacency Matrix, because it allows O(1) edge existence checks -> Option C
Quick Check:
Dense graph + fast edge check = Adjacency Matrix [OK]