What is an adjacency list in graph representation?
An adjacency list stores each node and a list of its neighbors. It uses less space for sparse graphs and is efficient for iterating over neighbors.
Click to reveal answer
beginner
What is an adjacency matrix in graph representation?
An adjacency matrix is a 2D array where each cell shows if there is an edge between two nodes. It uses more space but allows quick edge checks.
Click to reveal answer
intermediate
When is it better to use an adjacency list over an adjacency matrix?
Use adjacency list when the graph is sparse (few edges) because it saves space and is faster to traverse neighbors.
Click to reveal answer
intermediate
When is an adjacency matrix preferred over an adjacency list?
Use adjacency matrix when the graph is dense (many edges) or when you need to quickly check if an edge exists between two nodes.
Click to reveal answer
intermediate
What is the space complexity difference between adjacency list and adjacency matrix?
Adjacency list uses O(V + E) space, where V is vertices and E is edges. Adjacency matrix uses O(V²) space regardless of edges.
Click to reveal answer
Which graph representation is more space efficient for a graph with 1000 nodes and 10 edges?
ANone of the above
BAdjacency Matrix
CBoth use the same space
DAdjacency List
✗ Incorrect
Adjacency list is more space efficient for sparse graphs like this one with few edges.
Which representation allows O(1) time to check if an edge exists between two nodes?
ABoth
BAdjacency List
CAdjacency Matrix
DNeither
✗ Incorrect
Adjacency matrix allows constant time edge existence check by direct indexing.
For a dense graph, which representation is generally better?
AAdjacency List
BAdjacency Matrix
CDepends on the programming language
DDepends on the number of nodes only
✗ Incorrect
Adjacency matrix is better for dense graphs because it stores all edges explicitly.
What is the space complexity of an adjacency matrix for a graph with V vertices?
AO(V²)
BO(V + E)
CO(E)
DO(log V)
✗ Incorrect
Adjacency matrix always uses O(V²) space regardless of edges.
Which representation is faster to iterate over all neighbors of a node?
AAdjacency List
BAdjacency Matrix
CBoth are equally fast
DDepends on the number of nodes
✗ Incorrect
Adjacency list stores neighbors directly, making iteration faster especially in sparse graphs.
Explain when you would choose an adjacency list over an adjacency matrix for graph representation.
Think about graphs with few edges compared to nodes.
You got /3 concepts.
Describe the advantages and disadvantages of using an adjacency matrix.
Consider time and space trade-offs.
You got /3 concepts.
Practice
(1/5)
1. Which data structure is generally more space-efficient for representing a graph with very few edges compared to the number of vertices?
easy
A. Adjacency List
B. Adjacency Matrix
C. Edge List
D. Incidence Matrix
Solution
Step 1: Understand graph density
Sparse graphs have few edges compared to vertices, so storing all possible edges wastes space.
Step 2: Compare space usage of adjacency list and matrix
Adjacency list stores only existing edges, using less space for sparse graphs. Adjacency matrix always uses space for all possible edges.
Final Answer:
Adjacency List -> Option A
Quick Check:
Sparse graph = Adjacency List [OK]
Hint: Sparse graph? Use adjacency list for less space [OK]
Common Mistakes:
Choosing adjacency matrix for sparse graphs
Confusing edge count with vertex count
Ignoring space complexity differences
2. Which of the following is the correct TypeScript syntax to declare an adjacency matrix for a graph with 3 vertices initialized with zeros?
easy
A. const matrix: number[][] = new Array(3).fill(new Array(3).fill(0));
B. const matrix: number[] = [0,0,0];
C. const matrix: number[][] = [[0,0,0],[0,0,0],[0,0,0]];
D. const matrix: number[][] = Array(3).map(() => Array(3).fill(0));
Solution
Step 1: Understand adjacency matrix type
An adjacency matrix is a 2D array (array of arrays) of numbers representing edges.
Step 2: Check initialization correctness
const matrix: number[][] = [[0,0,0],[0,0,0],[0,0,0]]; explicitly creates a 3x3 matrix with zeros. const matrix: number[][] = new Array(3).fill(new Array(3).fill(0)); uses fill with the same inner array reference, causing bugs. const matrix: number[][] = Array(3).map(() => Array(3).fill(0)); uses map on empty array, which won't work as expected. const matrix: number[] = [0,0,0]; is 1D array, not matrix.
Final Answer:
const matrix: number[][] = [[0,0,0],[0,0,0],[0,0,0]]; -> Option C
Hint: Avoid fill with nested arrays; use map to create separate arrays [OK]
Common Mistakes:
Thinking fill creates separate inner arrays
Ignoring shared reference problem
Assuming matrix is 1D array
5. You need to represent a dense graph with 1000 vertices where you frequently check if an edge exists between any two vertices. Which representation is best and why?
hard
A. Adjacency List, because it uses less space for dense graphs
B. Adjacency Matrix, because it allows O(1) edge existence checks
C. Edge List, because it is fastest for dense graphs
D. Incidence Matrix, because it uses less space than adjacency matrix
Solution
Step 1: Understand graph density and operation needs
Dense graph means many edges; checking edge existence frequently requires fast lookup.
Step 2: Compare adjacency list and matrix for edge checks
Adjacency matrix allows direct O(1) access to check if edge exists between two vertices. Adjacency list requires searching neighbors, which can be slower in dense graphs.
Step 3: Consider space tradeoff
Though adjacency matrix uses more space, for dense graphs this is acceptable and beneficial for speed.
Final Answer:
Adjacency Matrix, because it allows O(1) edge existence checks -> Option B