What is an adjacency matrix in graph representation?
An adjacency matrix is a 2D array used to represent a graph where each cell at row i and column j indicates if there is an edge from vertex i to vertex j.
Click to reveal answer
beginner
How do you represent no edge between two vertices in an adjacency matrix?
You represent no edge by placing a 0 (zero) in the cell corresponding to those vertices in the adjacency matrix.
Click to reveal answer
beginner
What is the size of an adjacency matrix for a graph with n vertices?
The adjacency matrix size is n x n, where n is the number of vertices in the graph.
Click to reveal answer
intermediate
How does an adjacency matrix differ for directed and undirected graphs?
In a directed graph, the matrix may not be symmetric because edges have direction. In an undirected graph, the matrix is symmetric because edges go both ways.
Click to reveal answer
intermediate
What is a disadvantage of using adjacency matrices for sparse graphs?
Adjacency matrices use a lot of memory (n² space) even if the graph has few edges, making them inefficient for sparse graphs.
Click to reveal answer
What value is stored in an adjacency matrix cell if there is an edge between vertex 2 and vertex 3?
AThe weight of the edge if weighted, otherwise 1
B0
C-1
D1
✗ Incorrect
If the graph is weighted, the cell stores the edge weight; if unweighted, it stores 1 to indicate an edge.
What is the time complexity to check if an edge exists between two vertices using an adjacency matrix?
AO(1)
BO(n)
CO(log n)
DO(n²)
✗ Incorrect
Checking an edge in an adjacency matrix is direct access to a cell, so it takes constant time O(1).
Which graph type always has a symmetric adjacency matrix?
ADirected graph
BWeighted graph
CCyclic graph
DUndirected graph
✗ Incorrect
Undirected graphs have symmetric adjacency matrices because edges go both ways.
What is the space complexity of an adjacency matrix for a graph with n vertices?
AO(n)
BO(n log n)
CO(n²)
DO(1)
✗ Incorrect
An adjacency matrix requires space proportional to n squared, where n is the number of vertices.
Which of the following is a disadvantage of adjacency matrices?
ASlow edge lookup
BUses too much memory for sparse graphs
CCannot represent weighted graphs
DCannot represent directed graphs
✗ Incorrect
Adjacency matrices use a lot of memory even if the graph has few edges, making them inefficient for sparse graphs.
Explain how an adjacency matrix represents edges in a graph and how it differs for directed and undirected graphs.
Think about how edges are shown as 1 or 0 and how direction affects symmetry.
You got /4 concepts.
Describe the advantages and disadvantages of using an adjacency matrix for graph representation.
Consider speed versus memory use.
You got /4 concepts.
Practice
(1/5)
1. What does an adjacency matrix represent in graph theory?
easy
A. A 2D array showing connections between nodes
B. A list of nodes in the graph
C. A sequence of edges in the graph
D. A tree structure of nodes
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 yes, 0 for no).
Final Answer:
A 2D array showing connections between nodes -> Option A
Quick Check:
Adjacency matrix = 2D array of connections [OK]
Hint: Adjacency matrix is always a 2D grid of node links [OK]
Common Mistakes:
Confusing adjacency matrix with adjacency list
Thinking it stores node values instead of connections
Assuming it is a list or sequence
2. Which of the following is the correct way to declare a 3x3 adjacency matrix in Python initialized with zeros?
easy
A. matrix = [0,0,0]
B. matrix = [[0]*3]*3
C. matrix = [0]*3
D. matrix = [[0 for _ in range(3)] for _ in range(3)]
Solution
Step 1: Understand correct 2D list initialization
Using list comprehension creates independent inner lists for each row.
Step 2: Analyze each option
matrix = [[0]*3]*3 creates shallow copies causing all rows to reference the same list, which is incorrect. matrix = [[0 for _ in range(3)] for _ in range(3)] correctly creates separate lists. Options C and D create 1D lists, not 2D.
Final Answer:
matrix = [[0 for _ in range(3)] for _ in range(3)] -> Option D
Quick Check:
Use list comprehension for independent rows [OK]
Hint: Use nested list comprehension for 2D matrix [OK]
Common Mistakes:
Using [[0]*3]*3 causes all rows to be same object
Creating 1D list instead of 2D
Forgetting to nest loops for rows and columns
3. What will be the output of the following code?
graph = [[0,1,0],[1,0,1],[0,1,0]]
for i in range(3):
for j in range(3):
if graph[i][j] == 1:
print(f"Edge between {i} and {j}")
medium
A. Edge between 0 and 0
Edge between 1 and 1
Edge between 2 and 2
B. Edge between 0 and 1
Edge between 1 and 0
Edge between 1 and 2
Edge between 2 and 1
C. Edge between 0 and 1
Edge between 1 and 2
D. No output
Solution
Step 1: Analyze the adjacency matrix
Matrix shows edges: (0,1), (1,0), (1,2), (2,1) as 1s.
Step 2: Trace nested loops and print statements
Loops check each cell; when value is 1, print edge between i and j.
Final Answer:
Edge between 0 and 1
Edge between 1 and 0
Edge between 1 and 2
Edge between 2 and 1 -> Option B
Quick Check:
Print edges where matrix cell is 1 [OK]
Hint: Print edges where matrix value is 1 [OK]
Common Mistakes:
Ignoring symmetric edges in undirected graph
Printing only one direction of edges
Confusing indices i and j
4. Find the error in the following code that tries to create an adjacency matrix for 4 nodes:
n = 4
matrix = [[0]*n]*n
matrix[0][1] = 1
print(matrix)
medium
A. The matrix initialization causes all rows to be the same list
B. The print statement syntax is incorrect
C. The index 1 is out of range for the matrix
D. The matrix should be initialized with ones, not zeros
Solution
Step 1: Understand matrix initialization
Using [[0]*n]*n creates shallow copies; all rows point to the same list.
Step 2: Effect of modifying one element
Changing matrix[0][1] changes that column in all rows because they share the same list.
Final Answer:
The matrix initialization causes all rows to be the same list -> Option A
Quick Check:
Use nested list comprehension to avoid shared rows [OK]
Hint: Avoid [[0]*n]*n; use nested list comprehension [OK]
Common Mistakes:
Assuming rows are independent lists
Ignoring shared reference problem
Thinking print syntax is wrong
5. Given a directed graph with 3 nodes and edges: 0->1, 1->2, and 2->0, which adjacency matrix correctly represents it?
hard
A. [[1,0,0],[0,1,0],[0,0,1]]
B. [[0,1,1],[1,0,1],[1,1,0]]
C. [[0,1,0],[0,0,1],[1,0,0]]
D. [[0,0,1],[1,0,0],[0,1,0]]
Solution
Step 1: Map edges to matrix positions
Edge 0->1 means matrix[0][1] = 1, 1->2 means matrix[1][2] = 1, 2->0 means matrix[2][0] = 1.
Step 2: Check each option against edges
[[0,1,0],[0,0,1],[1,0,0]] matches these exactly; others have extra or missing edges.
Final Answer:
[[0,1,0],[0,0,1],[1,0,0]] -> Option C
Quick Check:
Edges map to 1s in correct matrix cells [OK]
Hint: Set matrix[i][j] = 1 for edge i->j only [OK]
Common Mistakes:
Confusing directed and undirected edges
Setting symmetric edges for directed graph
Misplacing 1s in wrong matrix cells
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.