Adjacency Matrix Representation in DSA Go - Time & Space Complexity
We want to understand how the time to work with an adjacency matrix changes as the graph grows.
How does the number of nodes affect the operations on this matrix?
Analyze the time complexity of the following code snippet.
func printEdges(matrix [][]int, n int) {
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
if matrix[i][j] == 1 {
fmt.Printf("Edge from %d to %d\n", i, j)
}
}
}
}
This code prints all edges in a graph represented by an adjacency matrix of size n x n.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each cell in the n x n matrix.
- How many times: The inner check runs n * n times, once for each pair of nodes.
As the number of nodes increases, the total checks grow by the square of n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 100 checks |
| 100 | 10,000 checks |
| 1000 | 1,000,000 checks |
Pattern observation: Doubling the number of nodes roughly quadruples the work.
Time Complexity: O(n²)
This means the time to process the adjacency matrix grows with the square of the number of nodes.
[X] Wrong: "Since we only check edges, the time is O(n²)."
[OK] Correct: We must check every possible pair of nodes, not just existing edges, so the time depends on n squared.
Understanding adjacency matrix complexity helps you explain graph operations clearly and choose the right representation for different problems.
What if we used an adjacency list instead of a matrix? How would the time complexity change?
Practice
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 there is a connection (edge) between two nodes.Final Answer:
A 2D array showing connections between nodes -> Option AQuick Check:
Adjacency matrix = 2D array [OK]
- Confusing adjacency matrix with adjacency list
- Thinking it stores edges as a list
- Assuming it represents a tree only
Solution
Step 1: Understand Go array declaration
In Go, a fixed-size 2D array is declared as [rows][cols]type.Step 2: Check each option
var matrix [3][3]int declares a 3x3 int array correctly. matrix := make([]int, 3) is a 1D slice. var matrix [][]int = make([][]int, 3) is a slice of slices but incomplete. matrix := [3]int{3} is a 1D array with wrong initialization.Final Answer:
var matrix [3][3]int -> Option AQuick Check:
Fixed 2D array = [3][3]int [OK]
- Using slices instead of arrays for fixed size
- Incorrect initialization syntax
- Confusing 1D and 2D declarations
package main
import "fmt"
func main() {
var graph [3][3]int
graph[0][1] = 1
graph[1][2] = 1
graph[2][0] = 1
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
fmt.Print(graph[i][j], " ")
}
fmt.Println()
}
}Solution
Step 1: Trace assignments to the matrix
graph[0][1] = 1 means node 0 connects to 1; graph[1][2] = 1 means node 1 connects to 2; graph[2][0] = 1 means node 2 connects to 0.Step 2: Print matrix row by row
Row 0: 0 1 0; Row 1: 0 0 1; Row 2: 1 0 0.Final Answer:
0 1 0 0 0 1 1 0 0 -> Option CQuick Check:
Edges set match matrix rows [OK]
- Mixing row and column indices
- Assuming symmetric matrix for directed graph
- Forgetting zero initialization
var graph [][]int
for i := 0; i < 3; i++ {
graph[i] = make([]int, 3)
}Solution
Step 1: Check graph slice initialization
graph is declared but not initialized with length, so graph[i] access causes runtime error.Step 2: Fix initialization
graph should be initialized with length 3 before assigning inner slices, e.g., graph = make([][]int, 3).Final Answer:
graph is not initialized before assignment -> Option DQuick Check:
Initialize outer slice before indexing [OK]
- Forgetting to initialize outer slice
- Confusing slice length and capacity
- Incorrect loop ranges
Solution
Step 1: Understand undirected graph edge representation
In undirected graphs, edges are bidirectional, so adjacency matrix must be symmetric.Step 2: Update both directions in matrix
Set graph[1][3] = 1 and graph[3][1] = 1 to represent the edge correctly.Final Answer:
graph[1][3] = 1; graph[3][1] = 1 -> Option BQuick Check:
Undirected edge updates both matrix cells [OK]
- Updating only one direction for undirected graph
- Setting diagonal elements instead of edge cells
- Confusing node indices
Ready to prove it?
Pass the test and this pattern moves unknown
