What is an adjacency list in graph representation?
An adjacency list stores each vertex's neighbors in a list or array. It shows which nodes are directly connected to each vertex.
Click to reveal answer
beginner
What is an adjacency matrix in graph representation?
An adjacency matrix is a 2D array where each cell (i, j) shows if there is an edge between vertex i and vertex j, usually with 1 for edge and 0 for no edge.
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). It saves space and is faster to iterate neighbors.
Click to reveal answer
intermediate
When is an adjacency matrix preferred?
Use adjacency matrix when the graph is dense (many edges) or when you need quick edge lookup between any two vertices.
Click to reveal answer
intermediate
What is the space complexity difference between adjacency list and matrix for a graph with V vertices and E edges?
Adjacency list uses O(V + E) space, while adjacency matrix uses O(V²) space regardless of edges.
Click to reveal answer
Which graph representation uses less space for a graph with very few edges?
ABoth use the same space
BAdjacency List
CAdjacency Matrix
DDepends on the number of vertices only
✗ Incorrect
Adjacency list uses space proportional to vertices plus edges, so it is more space efficient for sparse graphs.
Which representation allows O(1) time to check if an edge exists between two vertices?
AAdjacency Matrix
BAdjacency List
CBoth take O(V) time
DNeither can do it in O(1)
✗ Incorrect
Adjacency matrix stores edges in a 2D array, so checking an edge is a direct index lookup.
For a graph with 1000 vertices and 10,000 edges, which representation is generally better?
AAdjacency Matrix
BUse edge list instead
CEither is fine
DAdjacency List
✗ Incorrect
Since 10,000 edges is much less than 1,000² = 1,000,000 possible edges, adjacency list is more space efficient.
Which representation is easier to implement for dense graphs?
ABoth are equally easy
BAdjacency List
CAdjacency Matrix
DNeither is suitable
✗ Incorrect
Adjacency matrix is simpler for dense graphs because it uses a fixed-size 2D array.
What is the main disadvantage of adjacency matrix for large sparse graphs?
AHigh space usage
BCannot represent directed edges
CComplex implementation
DSlow edge lookup
✗ Incorrect
Adjacency matrix uses O(V²) space, which is large for sparse graphs with many vertices but few edges.
Explain when to choose adjacency list over adjacency matrix for graph representation.
Think about how many edges compared to vertices.
You got /3 concepts.
Describe the advantages and disadvantages of adjacency matrix.
Consider time and space trade-offs.
You got /3 concepts.
Practice
(1/5)
1. Which data structure is generally better 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
A graph with very few edges compared to vertices is called a sparse graph.
Step 2: Choose efficient representation for sparse graphs
Adjacency lists store only existing edges, saving space for sparse graphs, unlike adjacency matrices which use 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 space efficiency [OK]
Common Mistakes:
Choosing adjacency matrix for sparse graphs wastes space
Confusing edge list with adjacency matrix
Thinking adjacency matrix is always better
2. Which of the following is the correct way to declare an adjacency matrix for a graph with 4 vertices in C?
easy
A. int graph[4][4][4];
B. int graph[4];
C. int graph[][] = new int[4][4];
D. int graph[4][4];
Solution
Step 1: Recall C array declaration syntax
In C, a 2D array for 4 vertices is declared as int graph[4][4];
Step 2: Check options for correctness
int graph[4][4]; matches correct syntax; int graph[4]; is 1D array; int graph[][] = new int[4][4]; uses Java syntax; int graph[4][4][4]; is 3D array.
Final Answer:
int graph[4][4]; -> Option D
Quick Check:
C 2D array syntax = int graph[4][4]; [OK]
Hint: C 2D arrays use int name[rows][cols]; syntax [OK]
Common Mistakes:
Using Java or other language syntax in C
Declaring wrong array dimensions
Confusing 1D and 2D arrays
3. Given the following adjacency matrix for a graph with 3 vertices:
What is the output of the code snippet that prints all neighbors of vertex 1?
for (int i = 0; i < 3; i++) {
if (graph[1][i] == 1) {
printf("%d ", i);
}
}
medium
A. 0 2
B. 1 2
C. 0 1
D. 1 0 2
Solution
Step 1: Analyze adjacency matrix row for vertex 1
Row 1 is {1, 0, 1}, meaning edges to vertices 0 and 2.
Step 2: Trace loop printing neighbors
Loop checks graph[1][i] == 1; prints i when true. i=0 and i=2 satisfy condition.
Final Answer:
0 2 -> Option A
Quick Check:
Neighbors of vertex 1 = 0 and 2 [OK]
Hint: Check matrix row for 1s to find neighbors [OK]
Common Mistakes:
Confusing row and column indices
Printing vertex itself as neighbor
Misreading matrix values
4. The following code attempts to create an adjacency list for a graph with 3 vertices but has an error:
int* graph[3];
for (int i = 0; i < 3; i++) {
graph[i] = malloc(sizeof(int));
graph[i][0] = i;
}
What is the main issue?
medium
A. Array size should be 4 instead of 3
B. Using malloc instead of calloc
C. Memory allocated is too small for multiple neighbors
D. graph should be declared as int graph[3][3]
Solution
Step 1: Check memory allocation size
malloc(sizeof(int)) allocates space for only one int, but adjacency list needs space for multiple neighbors.
Step 2: Understand adjacency list needs dynamic size
Allocating only one int is insufficient; should allocate enough space or use dynamic structures like linked lists.
Final Answer:
Memory allocated is too small for multiple neighbors -> Option C
Quick Check:
Insufficient malloc size = Error [OK]
Hint: Allocate enough memory for all neighbors in adjacency list [OK]
Common Mistakes:
Confusing malloc and calloc
Wrong array declaration type
Assuming fixed size adjacency list
5. You have a graph with 1000 vertices and about 10,000 edges. You want to quickly check if an edge exists between any two vertices. Which representation is best and why?
hard
A. Adjacency List, because it uses less memory for sparse graphs
B. Adjacency Matrix, because it allows O(1) edge existence check
C. Edge List, because it is easiest to implement
D. Incidence Matrix, because it stores edges explicitly
Solution
Step 1: Analyze graph size and density
1000 vertices and 10,000 edges means the graph is moderately dense (max edges ~499,500).
Step 2: Consider edge existence check speed
Adjacency matrix allows checking edge existence in O(1) time by direct index access.
Step 3: Compare memory vs speed tradeoff
Though adjacency matrix uses more memory, fast edge check is priority here.
Final Answer:
Adjacency Matrix, because it allows O(1) edge existence check -> Option B
Quick Check:
Fast edge check = Adjacency Matrix [OK]
Hint: Need fast edge check? Use adjacency matrix despite memory [OK]
Common Mistakes:
Choosing adjacency list only for memory without speed need
Ignoring graph density impact
Confusing edge list with adjacency matrix
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.