Challenge - 5 Problems
Adjacency Matrix Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of adjacency matrix after adding edges
What is the printed adjacency matrix after adding edges between nodes 0-1, 1-2, and 2-0 in an undirected graph with 3 nodes?
DSA Rust
fn main() {
let mut graph = vec![vec![0; 3]; 3];
graph[0][1] = 1;
graph[1][0] = 1;
graph[1][2] = 1;
graph[2][1] = 1;
graph[2][0] = 1;
graph[0][2] = 1;
for row in &graph {
println!("{:?}", row);
}
}Attempts:
2 left
💡 Hint
Remember that for an undirected graph, edges are symmetric in the adjacency matrix.
✗ Incorrect
Since the graph is undirected, each edge between nodes i and j is represented by setting graph[i][j] and graph[j][i] to 1. The matrix is symmetric with zeros on the diagonal.
🧠 Conceptual
intermediateNumber of edges in adjacency matrix
Given a directed graph represented by an adjacency matrix of size 4x4, how many edges does the graph have if the matrix has exactly six 1s?
Attempts:
2 left
💡 Hint
Each 1 in the adjacency matrix represents one directed edge.
✗ Incorrect
In a directed graph, each 1 in the adjacency matrix corresponds to one edge from row node to column node. So six 1s mean six edges.
❓ Predict Output
advancedOutput after removing an edge in adjacency matrix
What is the adjacency matrix output after removing the edge from node 1 to node 2 in the following directed graph with 3 nodes?
DSA Rust
fn main() {
let mut graph = vec![
vec![0, 1, 0],
vec![0, 0, 1],
vec![1, 0, 0],
];
graph[1][2] = 0;
for row in &graph {
println!("{:?}", row);
}
}Attempts:
2 left
💡 Hint
Setting graph[1][2] to 0 removes the edge from node 1 to node 2.
✗ Incorrect
The original edge from node 1 to node 2 is removed by setting graph[1][2] = 0. The rest of the matrix remains unchanged.
🔧 Debug
advancedIdentify the error in adjacency matrix initialization
What error will this Rust code produce when trying to create an adjacency matrix for 3 nodes?
let graph = vec![vec![0; 3]; 3];
graph[0][1] = 1;
DSA Rust
fn main() {
let graph = vec![vec![0; 3]; 3];
graph[0][1] = 1;
}Attempts:
2 left
💡 Hint
Check if the variable is mutable before assigning to its elements.
✗ Incorrect
The variable `graph` is declared immutable with `let`. Trying to assign to graph[0][1] causes a compile-time error because the vector is not mutable.
🧠 Conceptual
expertMemory complexity of adjacency matrix for large sparse graph
What is the memory complexity of storing a graph with 10,000 nodes and 20,000 edges using an adjacency matrix representation?
Attempts:
2 left
💡 Hint
Adjacency matrix size depends on number of nodes squared, regardless of edges.
✗ Incorrect
An adjacency matrix always requires space proportional to n squared, where n is the number of nodes, because it stores a value for every possible edge.
