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 for an unweighted graph?
You represent no edge by putting 0 in the matrix cell corresponding to those two vertices.
Click to reveal answer
intermediate
What value is stored in the adjacency matrix for weighted graphs when there is an edge?
The weight of the edge is stored in the matrix cell corresponding to the two vertices connected by that edge.
Click to reveal answer
beginner
What is the size of the adjacency matrix for a graph with V vertices?
The adjacency matrix size is V x V, where V is the number of vertices in the graph.
Click to reveal answer
intermediate
What is a disadvantage of using adjacency matrix for sparse graphs?
It uses more memory than needed because it stores V x V cells even if there are few edges, leading to wasted space.
Click to reveal answer
In an adjacency matrix, what does a 1 typically represent in an unweighted graph?
AThere is an edge between two vertices
BThere is no edge between two vertices
CThe weight of the edge
DThe number of vertices
✗ Incorrect
A 1 indicates presence of an edge between the two vertices in an unweighted graph.
What is the time complexity to check if an edge exists between two vertices using adjacency matrix?
AO(V)
BO(V^2)
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?
ADense graphs
BSparse graphs
CComplete graphs
DDirected graphs
✗ Incorrect
Adjacency matrix wastes space for sparse graphs because most cells are zero.
What does the diagonal of an adjacency matrix represent in a simple graph?
ANumber of vertices
BEdges between different vertices
CEdges from a vertex to itself
DWeights of edges
✗ Incorrect
Diagonal cells represent self-loops, edges from a vertex to itself.
How do you represent a directed edge from vertex i to vertex j in adjacency matrix?
ASet matrix[i][j] to 1 or weight
BSet matrix[j][i] to 1 or weight
CSet matrix[i][i] to 1
DSet matrix[j][j] to 1
✗ Incorrect
For directed graphs, matrix[i][j] shows edge from i to j.
Explain how an adjacency matrix represents edges in both weighted and unweighted graphs.
Think about what each cell in the matrix means.
You got /4 concepts.
Describe the advantages and disadvantages of using adjacency matrix for graph representation.
Consider speed and memory usage.
You got /4 concepts.
Practice
(1/5)
1. What does an adjacency matrix represent in graph theory?
easy
A. A 2D array showing edges between nodes
B. A list of nodes in the graph
C. A sequence of visited 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 an edge exists between two nodes (1 for edge, 0 for no edge).
Final Answer:
A 2D array showing edges between nodes -> Option A
Quick Check:
Adjacency matrix = 2D array of edges [OK]
Hint: Adjacency matrix is always a 2D array showing connections [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 an adjacency matrix for a graph with 4 nodes in Java?
easy
A. int[] matrix = new int[4][4];
B. int matrix = new int[4];
C. int[][] matrix = new int[4][4];
D. int[][] matrix = new int[4];
Solution
Step 1: Recall Java 2D array declaration syntax
In Java, a 2D array is declared as type[][] name = new type[rows][columns];
Step 2: Match with graph nodes count
For 4 nodes, we need a 4x4 matrix, so new int[4][4] is correct.
Final Answer:
int[][] matrix = new int[4][4]; -> Option C
Quick Check:
2D array for 4 nodes = new int[4][4] [OK]
Hint: 2D array syntax: type[][] name = new type[size][size]; [OK]
Common Mistakes:
Using single brackets for 2D array
Declaring 1D array instead of 2D
Incorrect array initialization syntax
3. Given the following adjacency matrix for a graph with 3 nodes:
int[][] matrix = {
{0, 1, 0},
{1, 0, 1},
{0, 1, 0}
}; What is the output of the code snippet?
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (matrix[i][j] == 1) {
System.out.print(i + "->" + j + " ");
}
}
}