📖 Scenario: You are working on a simple network of computers. Each computer can be connected directly to some others. We want to represent these connections clearly.
🎯 Goal: Build an adjacency matrix to show which computers are connected to which. Then print the matrix to see the connections.
📋 What You'll Learn
Create a 4x4 adjacency matrix with exact connections
Add a variable for the number of computers
Fill the matrix with 1 for connected computers and 0 for no connection
Print the adjacency matrix row by row
💡 Why This Matters
🌍 Real World
Adjacency matrices are used to represent networks like social connections, computer networks, or city maps.
💼 Career
Understanding adjacency matrices helps in roles involving graph algorithms, network analysis, and software development for connected systems.
Progress0 / 4 steps
1
Create the adjacency matrix
Create a variable called adj_matrix as a 4x4 two-dimensional vector of u8 filled with zeros.
DSA Rust
Hint
Use vec![value; size] to create a vector filled with zeros.
2
Add the number of computers
Create a variable called num_computers and set it to 4.
DSA Rust
Hint
Just create a variable with the exact name and value.
3
Fill the adjacency matrix with connections
Update adj_matrix so that computer 0 is connected to 1 and 2, computer 1 is connected to 0 and 3, computer 2 is connected to 0, and computer 3 is connected to 1. Use adj_matrix[row][col] = 1 for connections.
DSA Rust
Hint
Remember to make adj_matrix mutable with mut to change values.
4
Print the adjacency matrix
Use a for loop with variable row to iterate over adj_matrix and print each row using println!("{:?}", row).
DSA Rust
Hint
Use for row in &adj_matrix and print each row with println!("{:?}", row).
Practice
(1/5)
1. What does an adjacency matrix represent in graph theory?
easy
A. A list of nodes in the graph
B. A 2D array showing edges between nodes
C. A sequence of visited nodes
D. A stack used for graph traversal
Solution
Step 1: Understand adjacency matrix concept
An adjacency matrix is a 2D array where rows and columns represent nodes.
Step 2: Identify what the matrix stores
Each cell shows if an edge exists between two nodes (1 for edge, 0 for no edge).
Final Answer:
A 2D array showing edges between nodes -> Option B
Quick Check:
Adjacency matrix = 2D edge array [OK]
Hint: Adjacency matrix is always a 2D grid of edges [OK]
Common Mistakes:
Confusing adjacency matrix with adjacency list
Thinking it stores node values instead of edges
Mixing it with traversal order
2. Which Rust code snippet correctly declares a 3x3 adjacency matrix using Vec initialized with zeros?
easy
A. let matrix = vec![vec![0; 3]; 3];
B. let matrix = [0; 3][0; 3];
C. let matrix = [[0; 3]; 3];
D. let matrix = [[0, 0, 0], [0, 0, 0]];
Solution
Step 1: Understand Rust 2D vector initialization
Rust uses Vec for dynamic 2D arrays; vec![vec![0; 3]; 3] creates 3 rows of 3 zeros.
Step 2: Check each option for correctness
A: [[0; 3]; 3] creates fixed-size array [[i32;3]; 3], not Vec. B: [0; 3][0; 3] is invalid syntax. D: [[0, 0, 0], [0, 0, 0]] has only 2 rows.
Final Answer:
let matrix = vec![vec![0; 3]; 3]; -> Option A
Quick Check:
vec![vec![0; 3]; 3] creates 3x3 zero matrix [OK]
Hint: Use vec![vec![0; size]; size] for 2D matrix in Rust [OK]
Common Mistakes:
Using invalid syntax for 2D arrays
Confusing fixed arrays with vectors
Incorrect row or column count
3. Given the following Rust code, what is the printed adjacency matrix?
let mut matrix = vec![vec![0; 3]; 3];
matrix[0][1] = 1;
matrix[1][2] = 1;
for row in &matrix {
println!("{:?}", row);
}
medium
A. [1, 0, 0]\n[0, 1, 0]\n[0, 0, 1]
B. [0, 0, 1]\n[1, 0, 0]\n[0, 1, 0]
C. [0, 1, 0]\n[0, 0, 1]\n[0, 0, 0]
D. [0, 1, 1]\n[0, 0, 1]\n[1, 0, 0]
Solution
Step 1: Initialize 3x3 zero matrix
matrix starts as [[0,0,0],[0,0,0],[0,0,0]].
Step 2: Update edges as per code
matrix[0][1] = 1 sets edge from node 0 to 1; matrix[1][2] = 1 sets edge from node 1 to 2.
Step 3: Print matrix rows
Rows print as [0,1,0], [0,0,1], [0,0,0].
Final Answer:
[0, 1, 0]\n[0, 0, 1]\n[0, 0, 0] -> Option C
Quick Check:
Edges at (0,1) and (1,2) = 1 [OK]
Hint: Track each assignment to matrix cells carefully [OK]
Common Mistakes:
Mixing row and column indices
Assuming symmetric edges in directed graph
Printing matrix incorrectly
4. Identify the error in this Rust code for creating an adjacency matrix:
let mut matrix = vec![vec![0; 3]; 3];
matrix[3][0] = 1;
medium
A. Index out of bounds error
B. Type mismatch error
C. Syntax error in vector initialization
D. No error, code runs fine
Solution
Step 1: Check vector size and indices
matrix is 3x3 with valid indices 0,1,2 for rows and columns.
Step 2: Analyze matrix[3][0] access
Index 3 is out of bounds for rows (max index 2), causing runtime panic.
Final Answer:
Index out of bounds error -> Option A
Quick Check:
Accessing index 3 in size 3 vector causes error [OK]
Hint: Check indices are within 0 to size-1 [OK]
Common Mistakes:
Using invalid indices beyond matrix size
Confusing compile-time and runtime errors
Ignoring Rust's panic on out-of-bounds
5. You have a directed graph with 4 nodes. Using an adjacency matrix in Rust, how would you represent edges from node 0 to 1 and 2, and from node 3 to 0? Which matrix correctly shows this?