Adjacency Matrix Representation in DSA C# - Time & Space Complexity
We want to understand how the time to check connections in a graph grows as the graph gets bigger.
Specifically, how fast can we find if two points are connected using an adjacency matrix?
Analyze the time complexity of the following code snippet.
// Check if there is an edge between node u and node v
bool HasEdge(int[,] matrix, int u, int v) {
return matrix[u, v] == 1;
}
This code checks if two nodes in a graph are connected by looking up the adjacency matrix.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Single direct access to the matrix cell.
- How many times: Exactly once per check.
Checking one connection always takes the same time, no matter how big the graph is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time stays the same because we just look up one spot in the matrix.
Time Complexity: O(1)
This means checking if two nodes are connected takes the same small amount of time no matter how big the graph is.
[X] Wrong: "Checking connections takes longer as the graph grows because the matrix is bigger."
[OK] Correct: We only look at one cell in the matrix, so size does not affect the time for a single check.
Knowing that adjacency matrix lookups are constant time helps you explain why this method is good for quick connection checks in graphs.
"What if we want to check all connections for one node? 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 an edge exists between two nodes (1 for edge, 0 for no edge).Final Answer:
A 2D array showing edges between nodes -> Option CQuick Check:
Adjacency matrix = 2D array of edges [OK]
- Confusing adjacency matrix with adjacency list
- Thinking it stores node values instead of edges
- Mixing up matrix with traversal order
Solution
Step 1: Identify correct 2D array syntax in C#
In C#, a 2D array is declared as int[,] with size [rows, columns].Step 2: Check each option
int[,] matrix = new int[4,4]; uses int[,] with size [4,4], correct for adjacency matrix of 4 nodes.Final Answer:
int[,] matrix = new int[4,4]; -> Option BQuick Check:
2D array syntax = int[,] [OK]
- Using jagged array syntax instead of 2D array
- Declaring 1D array for adjacency matrix
- Missing comma in array declaration
int[,] matrix = new int[,] {
{0, 1, 0},
{1, 0, 1},
{0, 1, 0}
};What is the output of
Console.WriteLine(matrix[1,2]);?Solution
Step 1: Locate matrix element at row 1, column 2
Matrix rows and columns start at 0, so matrix[1,2] is second row, third column.Step 2: Check value at matrix[1,2]
From the matrix, second row is {1, 0, 1}, third element is 1.Final Answer:
1 -> Option AQuick Check:
matrix[1,2] = 1 [OK]
- Confusing row and column indices
- Assuming 1-based indexing
- Misreading matrix values
int[] matrix = new int[3]; matrix[0,1] = 1;
Solution
Step 1: Analyze array declaration
int[] matrix = new int[3]; declares a 1D array of 3 integers.Step 2: Examine the assignment
matrix[0,1] = 1; attempts to use 2D indexing on a 1D array, which is invalid in C#.Final Answer:
Cannot assign value to matrix[0,1] -> Option DQuick Check:
1D array indexing mismatch [OK]
- Using 2D indexing on 1D array
- Assuming jagged array syntax for 2D array
- Ignoring compiler error messages
Solution
Step 1: Understand directed edges in adjacency matrix
For directed graph, matrix[i,j] = 1 means edge from node i to node j.Step 2: Check edges from node 0 to 1 and 2
Row 0 should have 1s at columns 1 and 2.Step 3: Check edge from node 3 to 0
Row 3 should have 1 at column 0.Step 4: Verify options
int[,] matrix = new int[,] { {0, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {1, 0, 0, 0} }; matches all conditions exactly.Final Answer:
{{0, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {1, 0, 0, 0}} -> Option AQuick Check:
Directed edges = matrix[i,j] = 1 [OK]
- Confusing rows and columns for direction
- Using symmetric matrix for directed graph
- Missing edges or adding extra edges
Ready to prove it?
Pass the test and this pattern moves unknown
