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 an undirected graph using an adjacency matrix?
In an undirected graph, the adjacency matrix is symmetric. If there is an edge between vertex i and j, both matrix[i][j] and matrix[j][i] are set to 1.
Click to reveal answer
beginner
What is the space complexity of an adjacency matrix for a graph with V vertices?
The space complexity is O(V²) because the matrix stores information for every possible pair of vertices.
Click to reveal answer
beginner
How do you check if there is an edge between two vertices using an adjacency matrix?
Check the value at matrix[i][j]. If it is 1 (or the edge weight), there is an edge; if 0, there is no edge.
Click to reveal answer
intermediate
What is a disadvantage of using adjacency matrix for sparse graphs?
Adjacency matrix uses O(V²) space even if the graph has very few edges, which wastes memory for sparse graphs.
Click to reveal answer
What does a 0 in an adjacency matrix cell usually represent?
ANo edge between vertices
BThere is an edge between vertices
CThe graph is directed
DThe graph is weighted
✗ Incorrect
A 0 means no edge exists between the two vertices in the matrix.
In an undirected graph, what is true about the adjacency matrix?
AIt is always diagonal
BIt is symmetric
CIt has only zeros
DIt is always sparse
✗ Incorrect
The adjacency matrix of an undirected graph is symmetric because edges go both ways.
What is the time complexity to check if an edge exists between two vertices using adjacency matrix?
AO(V²)
BO(V)
CO(E)
DO(1)
✗ Incorrect
Checking an edge is direct access in the matrix, so it takes constant time O(1).
Which graph type is adjacency matrix less efficient for?
ASparse graphs
BComplete graphs
CSmall graphs
DDirected graphs
✗ Incorrect
Adjacency matrix wastes space for sparse graphs because it stores all possible edges.
How is a weighted edge represented in an adjacency matrix?
ABy storing 1 in the matrix cell
BBy storing 0 in the matrix cell
CBy storing the weight value in the matrix cell
DBy storing -1 in the matrix cell
✗ Incorrect
Weighted graphs store the edge weight in the matrix cell instead of just 1.
Explain how an adjacency matrix represents a graph and how to check for an edge between two vertices.
Think of a table where rows and columns are vertices.
You got /5 concepts.
Describe the advantages and disadvantages of using adjacency matrix for graph representation.
Consider space and time trade-offs.
You got /4 concepts.
Practice
(1/5)
1. What does an adjacency matrix represent in graph theory?
easy
A. A sequence of visited nodes
B. A list of nodes in the graph
C. A 2D array showing connections between nodes
D. A stack used for graph traversal
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 C
Quick Check:
Adjacency matrix = 2D array of connections [OK]
Hint: Adjacency matrix = grid showing edges between nodes [OK]
Common Mistakes:
Confusing adjacency matrix with adjacency list
Thinking it stores node values instead of edges
Mixing it up with traversal order
2. Which of the following is the correct way to declare a 4x4 adjacency matrix in C++?
easy
A. int matrix(4,4);
B. int matrix[][] = new int[4][4];
C. int matrix[4];
D. int matrix[4][4];
Solution
Step 1: Recall C++ array declaration syntax
In C++, a 2D array is declared as type name[size1][size2].
Step 2: Check each option
int matrix[4][4]; uses correct syntax: int matrix[4][4]; int matrix(4,4); is invalid syntax, C is 1D array, D is invalid in C++.
Final Answer:
int matrix[4][4]; -> Option D
Quick Check:
2D array declaration = int matrix[4][4]; [OK]
Hint: Use int name[rows][cols] for 2D arrays in C++ [OK]
Common Mistakes:
Using parentheses instead of brackets
Declaring only one dimension
Trying Java-style new keyword in C++
3. Given the following adjacency matrix for a graph with 3 nodes:
Write a function in C++ that returns true if there is an edge from node u to node v, false otherwise. Which of these is correct?
hard
A. bool hasEdge(int u, int v) { return graph[v][u] == 1; }
B. bool hasEdge(int u, int v) { return graph[u][v] == 1; }
C. bool hasEdge(int u, int v) { return graph[u][v] == 0; }
D. bool hasEdge(int u, int v) { return graph[u] == graph[v]; }
Solution
Step 1: Understand adjacency matrix indexing for edges
graph[u][v] == 1 means edge from node u to node v exists.
Step 2: Check each function option
bool hasEdge(int u, int v) { return graph[u][v] == 1; } correctly returns true if edge exists. bool hasEdge(int u, int v) { return graph[v][u] == 1; } reverses indices, C checks for no edge, D compares rows incorrectly.
Final Answer:
bool hasEdge(int u, int v) { return graph[u][v] == 1; } -> Option B
Quick Check:
Check graph[u][v] == 1 for edge presence [OK]
Hint: Check graph[u][v] == 1 for edge from u to v [OK]
Common Mistakes:
Swapping u and v indices
Checking for zero instead of one
Comparing entire rows instead of single cell
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.