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 between vertex i and 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 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 is not necessarily symmetric because edges have direction. In an undirected graph, the matrix is symmetric because edges go both ways.
Click to reveal answer
beginner
What is the time complexity to check if an edge exists between two vertices using an adjacency matrix?
It is O(1) because you directly access the cell at row i and column j to check for an edge.
Click to reveal answer
What value is stored in an adjacency matrix cell if there is an edge between vertex 2 and vertex 3?
Anull
B1
C-1
D0
✗ Incorrect
A value of 1 indicates an edge exists between the two vertices.
What is the size of the adjacency matrix for a graph with 5 vertices?
A5 x 5
B5 x 4
C4 x 4
D1 x 5
✗ Incorrect
The adjacency matrix is always n x n where n is the number of vertices.
Which graph type has a symmetric adjacency matrix?
ADirected graph
BCyclic graph
CWeighted graph
DUndirected graph
✗ Incorrect
Undirected graphs have symmetric adjacency matrices because edges go both ways.
What is the time complexity to check if an edge exists between two vertices using an adjacency matrix?
AO(n)
BO(log n)
CO(1)
DO(n^2)
✗ Incorrect
Checking an edge is O(1) because it is a direct lookup in the matrix.
How is no edge represented in an adjacency matrix?
A0
B1
C-1
Dnull
✗ Incorrect
A 0 indicates no edge between the vertices.
Explain how an adjacency matrix represents a graph and how to interpret its values.
Think of a table where rows and columns are vertices and cells show connections.
You got /5 concepts.
Describe the difference between adjacency matrices of directed and undirected graphs.
Consider if edges have direction or go both ways.
You got /4 concepts.
Practice
(1/5)
1. What does an adjacency matrix represent in a graph? Adjacency matrix is a way to show connections between nodes. What does a 1 in the matrix mean?
easy
A. There is no edge between two nodes
B. The graph is weighted
C. The nodes are the same
D. There is a direct edge between two nodes
Solution
Step 1: Understand adjacency matrix basics
An adjacency matrix uses rows and columns to represent nodes. Each cell shows if two nodes connect.
Step 2: Interpret the value 1 in the matrix
A value of 1 means there is a direct edge between the nodes represented by that row and column.
Final Answer:
There is a direct edge between two nodes -> Option D
Quick Check:
1 means edge exists [OK]
Hint: 1 means connected, 0 means no connection [OK]
Common Mistakes:
Thinking 1 means no edge
Confusing node identity with connection
Assuming matrix shows weights always
2. Which of the following is the correct TypeScript syntax to declare a 3x3 adjacency matrix initialized with zeros without shared row references?
easy
A. const matrix: number[][] = [[0,0,0],[0,0,0],[0,0,0]];
B. const matrix: number[3][3] = new Array(3).fill(new Array(3).fill(0));
C. const matrix = Array(3).map(Array(3).fill(0));
D. const matrix: number[][] = new Array(3).fill(new Array(3).fill(0));
Solution
Step 1: Check TypeScript array declaration syntax
const matrix: number[][] = [[0,0,0],[0,0,0],[0,0,0]]; uses explicit 2D array with nested arrays and zeros, which is valid syntax and creates independent rows.
Step 2: Identify issues in other options
const matrix: number[3][3] = new Array(3).fill(new Array(3).fill(0)); uses invalid TypeScript syntax for fixed-size arrays. const matrix = Array(3).map(Array(3).fill(0)); passes an array to map() instead of a function, causing a runtime error. const matrix: number[][] = new Array(3).fill(new Array(3).fill(0)); uses fill with the same inner array reference, causing shared rows.
Final Answer:
const matrix: number[][] = [[0,0,0],[0,0,0],[0,0,0]]; -> Option A
Quick Check:
Explicit nested arrays with zeros [OK]
Hint: Use explicit nested arrays to avoid shared references [OK]
Common Mistakes:
Using fill with same inner array causing shared rows
Passing non-function to map()
Wrong type annotations
3. Given the following adjacency matrix for a directed graph:
C. All rows point to the same inner array, so all rows change when one changes
D. It throws a runtime error
Solution
Step 1: Understand fill with reference types
Using fill with new Array(size).fill(0) creates one inner array shared by all rows.
Step 2: Effect of modifying matrix[0][1]
Changing matrix[0][1] changes the shared inner array, so all rows show the change.
Final Answer:
All rows point to the same inner array, so all rows change when one changes -> Option C
Quick Check:
Shared inner array causes unexpected updates [OK]
Hint: Avoid fill with objects/arrays; use map for unique rows [OK]
Common Mistakes:
Assuming fill creates independent arrays
Ignoring shared reference behavior
Expecting only one row to change
5. You have an undirected graph with 4 nodes. You want to represent it using an adjacency matrix in TypeScript. Which of the following code snippets correctly initializes the matrix and sets an edge between node 1 and node 3?
hard
A. const size = 4;
const matrix = new Array(size).fill(new Array(size).fill(0));
matrix[1][3] = 1;
matrix[3][1] = 1;
Step 1: Check matrix initialization for unique rows
const size = 4;
const matrix = Array.from({ length: size }, () => Array(size).fill(0));
matrix[1][3] = 1;
matrix[3][1] = 1; uses Array.from with a function to create unique inner arrays, avoiding shared references.
Step 2: Verify setting edges for undirected graph
Setting matrix[1][3] and matrix[3][1] to 1 correctly represents an undirected edge.
Step 3: Identify issues in other options
Options B and C use fill causing shared inner arrays. const size = 4;
const matrix = Array(size).map(() => Array(size).fill(0));
matrix[1][3] = 1; initializes correctly but only sets matrix[1][3] = 1, missing matrix[3][1] = 1 for undirected graph.